Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Problem mit C++ DLL mit Stringfunktionen (https://www.delphipraxis.net/123369-problem-mit-c-dll-mit-stringfunktionen.html)

Blink 31. Okt 2008 17:09


Problem mit C++ DLL mit Stringfunktionen
 
Hallo DPler,

ich versuche schon seit ein paar Tagen Funktionen aus einer C++ DLL zu nutzen, leider hab ich das bis her noch nicht hinbekommen, da einige Funktionen in der DLL Strings als Argumente erwarten.
Ich hoffe ihr könnt mir sagen was ich falsch mache oder wie ich es machen muss damit es funktioniert.

Hier mein Delphi Code:

Delphi-Quellcode:
unit Unit1;

interface



uses
  Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShareMem;

  function ohs_set_password(PW: string): Double; stdcall;
  function ohs_get_status: Double; stdcall;
  function ohs_get_position: Double; stdcall;
  function ohs_table_clear: Double; stdcall;
  function ohs_table_update: Double; stdcall;
  function ohs_table_get(Spalte: Double; Reihe: Double): string; stdcall;
  function ohs_header_get(Spalte: Double): string; stdcall;
  function ohs_table_columns: Double; stdcall;
  function ohs_table_rows: Double; stdcall;
  function ohs_submit_add(Spalte: Double; Wert: string): Double; stdcall;
  function ohs_submit_clear: Double; stdcall;
  function ohs_set_url(URL: string; Pfad: string): Double; stdcall;
  function ohs_set_table(Tabelle: string): Double; stdcall;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }

  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function ohs_set_password(PW: string): Double; stdcall;
external 'ohs.dll';

function ohs_get_status: Double; stdcall;
external 'ohs.dll';

function ohs_get_position: Double; stdcall;
external 'ohs.dll';

function ohs_table_clear: Double; stdcall;
external 'ohs.dll';

function ohs_table_update: Double; stdcall;
external 'ohs.dll';

function ohs_table_get(Spalte: Double; Reihe: Double): string; stdcall;
external 'ohs.dll';

function ohs_header_get(Spalte: Double): string; stdcall;
external 'ohs.dll';

function ohs_table_columns: Double; stdcall;
external 'ohs.dll';

function ohs_table_rows: Double; stdcall;
external 'ohs.dll';

function ohs_submit_add(Spalte: Double; Wert: string): Double; stdcall;
external 'ohs.dll';

function ohs_submit_clear: Double; stdcall;
external 'ohs.dll';

function ohs_set_url(URL: string; Pfad: string): Double; stdcall;
external 'ohs.dll';

function ohs_set_table(Tabelle: string): Double; stdcall;
external 'ohs.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
  ohs_set_url(PChar('http://muti.mu.funpic.de'), PChar('/OHS'));
  ohs_set_table(PChar('TEST'));
  ohs_set_password(Pchar('12345'));

  Label1.Caption:= FloatToStr(ohs_table_update);

  ohs_submit_add(0, PChar('Test'));
  ohs_submit_add(1, PChar('300'));


  Label1.Caption:= FloatToStr(ohs_table_update);
  Label2.Caption:= FloatToStr(ohs_get_position);
end;
So deklariert der Autor der DLL die Funktionen im GM:

Code:
#define ohs_init
global.dll_ohs_set_password = external_define("ohs.dll","ohs_set_password",dll_stdcall,ty_real,1,ty_string);
global.dll_ohs_set_url = external_define("ohs.dll","ohs_set_url",dll_stdcall,ty_real,2,ty_string,ty_string);
global.dll_ohs_set_table = external_define("ohs.dll","ohs_set_table",dll_stdcall,ty_real,1,ty_string);
global.dll_ohs_get_status = external_define("ohs.dll","ohs_get_status",dll_stdcall,ty_real,0);
global.dll_ohs_get_position = external_define("ohs.dll","ohs_get_position",dll_stdcall,ty_real,0);
global.dll_ohs_table_clear = external_define("ohs.dll","ohs_table_clear",dll_stdcall,ty_real,0);
global.dll_ohs_table_update = external_define("ohs.dll","ohs_table_update",dll_stdcall,ty_real,0);
global.dll_ohs_table_get = external_define("ohs.dll","ohs_table_get",dll_stdcall,ty_string,2,ty_real,ty_real);
global.dll_ohs_header_get = external_define("ohs.dll","ohs_header_get",dll_stdcall,ty_string,1,ty_real);
global.dll_ohs_table_columns = external_define("ohs.dll","ohs_table_columns",dll_stdcall,ty_real,0);
global.dll_ohs_table_rows = external_define("ohs.dll","ohs_table_rows",dll_stdcall,ty_real,0);
global.dll_ohs_submit_add = external_define("ohs.dll","ohs_submit_add",dll_stdcall,ty_real,2,ty_real,ty_string);
global.dll_ohs_submit_clear = external_define("ohs.dll","ohs_submit_clear",dll_stdcall,ty_real,0);

turboPASCAL 31. Okt 2008 17:12

Re: Problem mit C++ DLL mit Stringfunktionen
 
C -Strings sind nicht Delphi Strings. Nutze PChars an deren Stelle.

hoika 31. Okt 2008 17:19

Re: Problem mit C++ DLL mit Stringfunktionen
 
Hallo,

1. sharemem raus
2. String durch PChar ersetzen


Ansonsten verstehe ich das ganze überhaupt nicht.
Die gmaker.pdf auf der Seite erzählt was von Einbinden fremder DLLs (auch in Delphi geschrieben).

Musst halt ein bissel probieren.


Heiko

Blink 31. Okt 2008 17:32

Re: Problem mit C++ DLL mit Stringfunktionen
 
Liste der Anhänge anzeigen (Anzahl: 1)
Leider Klapt es auch nicht mit PChars und ohne Sharemem ich bekomme immer komische werte zurück.

Die DLL ist eine schnitt stell zur einem Online Highscore system, was für den GM gemacht wurde. Da ich das System ganz gut fand wollte ich es auch nutzen und der Autor meinte das es eigentlich keine Probleme geben sollte wenn ich die DLL in Delphi nutzen will. Die Header Datei zu der DLL wollte der Autor nicht hergeben :(.

Ich hab mal im anhang die DLL + Readme + mein Test Programm beigefügt, die URL, Tabellenname und das PW sind richtig so. Habe zum testen das OHS system auf meinem Webspace Installiert. Nur die DLL bereitet mir noch Kopfzerbrechen.

Edit: Hier noch der Link zur Highscoreliste wie ihr seht habe ich es bis jetzt noch nicht geschafft ein Wert da einzutragen.

nicodex 31. Okt 2008 20:32

Re: Problem mit C++ DLL mit Stringfunktionen
 
In der ohs.gml steht zwar "dll_stdcall" (was man als __stdcall interpretieren könnte), aber im Disassembler sehen alle von der ohs.dll exportierten Funktionen nach __cdecl aus.

Edit: Und anstatt Double (C/C++ double: 8 Bytes) würde ich mal Single (C/C++ float: 4 Bytes) probieren.

Blink 1. Nov 2008 14:42

Re: Problem mit C++ DLL mit Stringfunktionen
 
Leider klappt es auch mit cdecl nicht und ich habe schon alles mögliche ausprobiert mit real, double, single, float, string, char und pchar es hat bis jetzt noch nicht geklappt. Mache ich vielleicht irgendwas beim Aufruf der DLL falsch?

Blink 8. Nov 2008 21:25

Re: Problem mit C++ DLL mit Stringfunktionen
 
Hallo alle zusammen,

So ich habe nun die Header Datei zu der DLL von dem Autor bekommen und Code dem entsprechend angepasst, leider scheint es noch immer nicht zu klappen. Könnte mir vielleicht irgendjemand helfen?

Hier mein Code:

Delphi-Quellcode:
  function ohs_set_password(const PW: PChar): Double; stdcall;
  function ohs_get_status: Double; stdcall;
  function ohs_get_position: Double; stdcall;
  function ohs_table_clear: Double; stdcall;
  function ohs_table_update: Double; stdcall;
  function ohs_table_get(Spalte: Double; Reihe: Double): PChar; stdcall;
  function ohs_header_get(Spalte: Double): PChar; stdcall;
  function ohs_table_columns: Double; stdcall;
  function ohs_table_rows: Double; stdcall;
  function ohs_submit_add(Spalte: Double; const Wert: PChar): Double; stdcall;
  function ohs_submit_clear: Double; stdcall;
  function ohs_set_url(const URL: PChar; const Pfad: PChar): Double; stdcall;
  function ohs_set_table(const Tabelle: PChar): Double; stdcall;
Hier die Header Datei zur DLL:

Code:
#define export extern "C" __declspec (dllexport)

export double ohs_set_password(const char * pw);
export double ohs_set_url(const char * h, const char * p);
export double ohs_set_table(const char * tab);
export double ohs_get_status();
export double ohs_get_position();
export double ohs_table_update();
export double ohs_table_clear();
export const char * ohs_table_get(double col, double row);
export const char * ohs_header_get(double col);
export double ohs_table_columns();
export double ohs_table_rows();
export double ohs_submit_add(double col, const char * value);
export double ohs_submit_clear();

turboPASCAL 8. Nov 2008 21:40

Re: Problem mit C++ DLL mit Stringfunktionen
 
Mal cdecl an Stelle von stdcall versucht ?

Blink 8. Nov 2008 21:45

Re: Problem mit C++ DLL mit Stringfunktionen
 
Zitat:

Zitat von turboPASCAL
Mal cdecl an Stelle von stdcall versucht ?

Ja, bringt leider auch nichts :(

turboPASCAL 8. Nov 2008 22:30

Re: Problem mit C++ DLL mit Stringfunktionen
 
Liste der Anhänge anzeigen (Anzahl: 1)
Mir kommt es so vor als ob es das macht was es machen soll.
Stimmt denn auch die Vorgehensweise wie eine Tabelle angelegt wird, das Passwort etc. ?


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:39 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz