![]() |
Funktion aus DLL (in C++ geschrieben) nach Delphi importiere
Hallo,
Ich habe folgendes Problem: Ich habe eine DLL-Datei, welche in C++ geschrieben ist und folgende Funktion enthält
Code:
Hier noch die dafür benötigen Strukturen:
int WINAPI getmove(int b[8][8],int color, double maxtime, char str[255], int *playnow, int info, int unused, struct CBmove *move)
Code:
Und hier mein Versuch, das ganze zu importieren und zu übersetzen:
struct coor
{ int x; int y; }; struct CBmove { int jumps; int newpiece; int oldpiece; struct coor from,to; struct coor path[12]; struct coor del[12]; int delpiece[12]; } GCBmove;
Delphi-Quellcode:
Ging natürlich vollkommen nach hinten los, nach einem Aufruf gibts direkt ne schöne Fehlermeldung und da ich nicht glaube, dass die DLL fehlerhaft ist, denke ich mal, dass mir irgendwo beim deklarieren der Variablen ein Übersetzungsfehler unterlaufen muss sein.
type
TPoint = record x: Integer; y: Integer; end; type PMove = ^TMove; TMove = record nJumps: Integer; nNewPiece: Integer; nOldPiece: Integer; pfrom, pto: TPoint; pPath: array [0 .. 12] of TPoint; pdel: array [0 .. 12] of TPoint; nDelPiece: array [0 .. 12] of Integer; end; type TBoard = array [0 .. 8, 0 .. 8] of Integer; procedure TForm1.Button1Click(Sender: TObject); var Dllh: THandle; GetMove: function(Board: TBoard; color: Integer; MaxTime: DOUBLE; str: array of Char; var playnow: Integer; info: Integer; unused: Integer; Move: PMove): Integer; stdcall; begin Dllh := LoadLibrary(PChar(ExtractFilePath(Application.ExeName) + 'cakeM32.dll')); @GetMove := GetProcAddress(Dllh, 'getmove'); MfG, STaRDoGGCHaMP |
Re: Funktion aus DLL (in C++ geschrieben) nac Delphi importi
Hi!
Was für eine Fehlermeldung kommt denn? Grüße, Frederic |
Re: Funktion aus DLL (in C++ geschrieben) nac Delphi importi
"Zugriffsverletzung bei Adresse 10016ED1 in Modul 'cakeM32.dll'. Schreiben von Adresse 000000FF"
Villeicht noch ein paar Informationen die im ersten Beitrag net so klar wurden: Die DLL und die Funktion werden (scheinbar) richtig geladen, die Rückgabewerte stimmen. Des Weiteren läuft der oben genannte Code auch ab, der Fehler kommt erst bei einem Aufruf der Funktion. Ich bin mir beispielsweise sehr unsicher ob ich char str[255] in c++ mit array of Char nach Delphi übersetzen kann. |
Re: Funktion aus DLL (in C++ geschrieben) nac Delphi importi
arrays in c are not zero indexed so they start with 1 instead of 0 as in Delphi, so you should shorten your arrays by 1,eg:
Delphi-Quellcode:
pPath: array [0 .. 11] of TPoint;
|
Re: Funktion aus DLL (in C++ geschrieben) nac Delphi importi
Delphi-Quellcode:
Klappt nicht
type
TPoint = record x: Integer; y: Integer; end; type PMove = ^TMove; TMove = record nJumps: Integer; nNewPiece: Integer; nOldPiece: Integer; pfrom, pto: TPoint; pPath: array [0 .. 11] of TPoint; pdel: array [0 .. 11] of TPoint; nDelPiece: array [0 .. 11] of Integer; end; type TBoard = array [0 .. 7, 0 .. 7] of Integer; procedure TForm1.Button1Click(Sender: TObject); var Dllh: THandle; GetMove: function(Board: TBoard; color: Integer; MaxTime: DOUBLE; str: array of Char; var playnow: Integer; info: Integer; unused: Integer; Move: PMove): Integer; stdcall; begin Dllh := LoadLibrary(PChar(ExtractFilePath(Application.ExeName) + 'cakeM32.dll')); @GetMove := GetProcAddress(Dllh, 'getmove'); |
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Es knallt schon in deinem gezeigten Beispiel? Also ohne dass du die Funktion überhaupt aufrufst? ok.
Hast du mal versucht Packed Records zuverwenden? Edit: Und wieso ist ein char[255] ein array of char? Initialisierst du das auch entsprechend? |
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
die Adresse der Zugriffsverletzung deutet darauf hin das die Funktion in der DLL nicht gefunden wird. Das sollte sich auch ganz einfach prüfen lassen in dem man das ganze schöner strukturiert:
Delphi-Quellcode:
Unabhängig davon ob dies der Fehler ist sollte man es immer so schreiben um Fehler zu vermeiden bzw. ordentlich auf die Ergebnisse der Funktionsaufrufe zu reagieren.
dllhandle := loadlibrary(...);
if (dllhandle <> 0) then begin functionaddress := getprocaddress(...); if Assigned(functionaddress) then begin functionaddress(...); end; FreeLibrary(...); end; Zudem sind einige Dinge von dir falsch übersetzt. "array of irgendwas" ist zum Beispiel Delphieigen. |
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Wie bereits erwähnt, die Funktion wird gefunden und es knallt erst bei einem Aufruf der Funktion. Ich weiss halt nur nicht wie ich beispielsweise mit dem char array oder sonstigen Sachen umgehen soll.
|
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Hallo STaRDoGGCHaMP.
Probier einfach mal folgendes durch:
Delphi-Quellcode:
Dynamische arrays von Delphi nach C sind problematisch, wie soll C die länge wissen?type TPoint = packed record x: Integer; y: Integer; end; type PMove = ^TMove; TMove = packed record nJumps : Integer; nNewPiece : Integer; nOldPiece : Integer; pfrom : TPoint; pto : TPoint; pPath : array [0..11] of TPoint; pdel : array [0..11] of TPoint; nDelPiece : array [0..11] of Integer; end; TBoard = array [0..7, 0..7] of Integer; TStr = array [0..254] of Char; var GetMove: function(Board: TBoard; color: Integer; maxtime: Double; str: TStr; var playnow: Integer; info: Integer; unused: Integer; move: PMove): Integer stdcall; GetMoveEx: function(Board: TBoard; color: Integer; maxtime: Double; str: TStr; var playnow: Integer; info: Integer; unused: Integer; move: PMove): Integer cdecl; Womöglich spielt Alignment (hier 32 Bit) eine Rolle, wenn C z.B. über Offset (Index) auf die Struct Inhalte zugreift? Wenn man den C Source nicht kennt, immer annehmen das die Structs aligned sind (packed records verwenden). Womöglich ist die Dll nicht stdcall sondern cdecl? lg. Astat |
Re: Funktion aus DLL (in C++ geschrieben) nac Delphi importi
Zitat:
|
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Hallo erneut,
Erst einmal vielen Dank für die Antwort Astat, das mit dem Char array war wohl der entscheidende Punkt welcher für die Zugriffsverletzung sorgte. Wenn ich das Programm jetz so ausführe:
Delphi-Quellcode:
(Man entschuldige hierbei den großen Code wegen der aufwendigen Deklaration von nBoard, aber ich wollte einfach mal den kompletten Code reinhauen, damit man auch wirklich jede potentielle Fehlerquelle erkennen kann). Krieg ich als Fehler "Ungültige Gleitkommaoperation". Also anscheinend ein Problem in der Funktion selber. Trotzdem ist das sehr merkwürdig, weil dass als Open-Source vorhandene C++-Äquivalent fehlerfrei funktioniert:
program TestpadConsole;
{$APPTYPE CONSOLE} uses SysUtils, Windows; type TPoint = packed record x: Integer; y: Integer; end; type PMove = ^TMove; TMove = packed record nJumps : Integer; nNewPiece : Integer; nOldPiece : Integer; pfrom : TPoint; pto : TPoint; pPath : array [0..11] of TPoint; pdel : array [0..11] of TPoint; nDelPiece : array [0..11] of Integer; end; TBoard = array [0..7, 0..7] of Integer; TStr = array [0..1023] of Char; const PGC_WHITE = 1; PGC_BLACK = 2; PGC_MAN = 4; PGC_KING = 8; var hDll: THandle; nBoard: TBoard; playnow: Integer = 1; str: TStr; Move: TMove; GetMove: function(var Board: TBoard; color: Integer; maxtime: Double; str: TStr; var playnow: Integer; info: Integer; unused: Integer; move: PMove): Integer; stdcall; begin try hDll := LoadLibrary('C:\..\cakeM32.dll'); if hDll <> 0 then begin @GetMove := GetProcAddress(hDll, 'getmove'); if Assigned(@GetMove) then begin nBoard[0, 0] := PGC_BLACK OR PGC_MAN; nBoard[0, 1] := 0; nBoard[0, 2] := PGC_BLACK OR PGC_MAN; nBoard[0, 3] := 0; nBoard[0, 4] := 0; nBoard[0, 5] := 0; nBoard[0, 6] := PGC_WHITE OR PGC_MAN; nBoard[0, 7] := 0; nBoard[1, 0] := 0; nBoard[1, 1] := PGC_BLACK OR PGC_MAN; nBoard[1, 2] := 0; nBoard[1, 3] := 0; nBoard[1, 4] := 0; nBoard[1, 5] := PGC_WHITE OR PGC_MAN; nBoard[1, 6] := 0; nBoard[1, 7] := PGC_WHITE OR PGC_MAN; nBoard[2, 0] := PGC_BLACK OR PGC_MAN; nBoard[2, 1] := 0; nBoard[2, 2] := PGC_BLACK OR PGC_MAN; nBoard[2, 3] := 0; nBoard[2, 4] := 0; nBoard[2, 5] := 0; nBoard[2, 6] := PGC_WHITE OR PGC_MAN; nBoard[2, 7] := 0; nBoard[3, 0] := 0; nBoard[3, 1] := PGC_BLACK OR PGC_MAN; nBoard[3, 2] := 0; nBoard[3, 3] := 0; nBoard[3, 4] := 0; nBoard[3, 5] := PGC_WHITE OR PGC_MAN; nBoard[3, 6] := 0; nBoard[3, 7] := PGC_WHITE OR PGC_MAN; nBoard[4, 0] := PGC_BLACK OR PGC_MAN; nBoard[4, 1] := 0; nBoard[4, 2] := PGC_BLACK OR PGC_MAN; nBoard[4, 3] := 0; nBoard[4, 4] := 0; nBoard[4, 5] := 0; nBoard[4, 6] := PGC_WHITE OR PGC_MAN; nBoard[4, 7] := 0; nBoard[5, 0] := 0; nBoard[5, 1] := PGC_BLACK OR PGC_MAN; nBoard[5, 2] := 0; nBoard[5, 3] := 0; nBoard[5, 4] := 0; nBoard[5, 5] := PGC_WHITE OR PGC_MAN; nBoard[5, 6] := 0; nBoard[5, 7] := PGC_WHITE OR PGC_MAN; nBoard[6, 0] := PGC_BLACK OR PGC_MAN; nBoard[6, 1] := 0; nBoard[6, 2] := PGC_BLACK OR PGC_MAN; nBoard[6, 3] := 0; nBoard[6, 4] := 0; nBoard[6, 5] := 0; nBoard[6, 6] := PGC_WHITE OR PGC_MAN; nBoard[6, 7] := 0; nBoard[7, 0] := 0; nBoard[7, 1] := PGC_BLACK OR PGC_MAN; nBoard[7, 2] := 0; nBoard[7, 3] := 0; nBoard[7, 4] := 0; nBoard[7, 5] := PGC_WHITE OR PGC_MAN; nBoard[7, 6] := 0; nBoard[7, 7] := PGC_WHITE OR PGC_MAN; GetMove(nBoard, PGC_BLACK, 5, str, playnow, 0, 0, @Move); Sleep(6000); end; FreeLibrary(hDll); end; except on E: Exception do begin Writeln(E.ClassName, ': ', E.Message); Sleep(5000); end; end; end.
Code:
#define WHITE 1
#define BLACK 2 #define MAN 4 #define KING 8 #include <iostream> #include <windows.h> using namespace std; struct coor { int x; int y; }; struct CBmove { int jumps; int newpiece; int oldpiece; struct coor from,to; struct coor path[12]; struct coor del[12]; int delpiece[12]; } GCBmove; typedef INT (WINAPI* PROC1)(int *board, int color, double maxtime, char str[1024], int *playnow, int info, int unused, struct CBmove *move); int main() { PROC1 getmove1=0; HINSTANCE hinstLib1=0; char s[1024]; int playnow = 1; int nBoard[8][8]; struct CBmove LCBmove; int x, y; hinstLib1 = LoadLibraryA("C:\\..\\cakeM32.dll"); getmove1 = (PROC1)GetProcAddress(hinstLib1, "getmove"); nBoard[0][0] = BLACK | MAN; nBoard[0][1] = 0; nBoard[0][2] = BLACK | MAN; nBoard[0][3] = 0; nBoard[0][4] = 0; nBoard[0][5] = 0; nBoard[0][6] = WHITE | MAN; nBoard[0][7] = 0; nBoard[1][0] = 0; nBoard[1][1] = BLACK | MAN; nBoard[1][2] = 0; nBoard[1][3] = 0; nBoard[1][4] = 0; nBoard[1][5] = WHITE | MAN; nBoard[1][6] = 0; nBoard[1][7] = WHITE | MAN; nBoard[2][0] = BLACK | MAN; nBoard[2][1] = 0; nBoard[2][2] = BLACK | MAN; nBoard[2][3] = 0; nBoard[2][4] = 0; nBoard[2][5] = 0; nBoard[2][6] = WHITE | MAN; nBoard[2][7] = 0; nBoard[3][0] = 0; nBoard[3][1] = BLACK | MAN; nBoard[3][2] = 0; nBoard[3][3] = 0; nBoard[3][4] = 0; nBoard[3][5] = WHITE | MAN; nBoard[3][6] = 0; nBoard[3][7] = WHITE | MAN; nBoard[4][0] = BLACK | MAN; nBoard[4][1] = 0; nBoard[4][2] = BLACK | MAN; nBoard[4][3] = 0; nBoard[4][4] = 0; nBoard[4][5] = 0; nBoard[4][6] = WHITE | MAN; nBoard[4][7] = 0; nBoard[5][0] = 0; nBoard[5][1] = BLACK | MAN; nBoard[5][2] = 0; nBoard[5][3] = 0; nBoard[5][4] = 0; nBoard[5][5] = WHITE | MAN; nBoard[5][6] = 0; nBoard[5][7] = WHITE | MAN; nBoard[6][0] = BLACK | MAN; nBoard[6][1] = 0; nBoard[6][2] = BLACK | MAN; nBoard[6][3] = 0; nBoard[6][4] = 0; nBoard[6][5] = 0; nBoard[6][6] = WHITE | MAN; nBoard[6][7] = 0; nBoard[7][0] = 0; nBoard[7][1] = BLACK | MAN; nBoard[7][2] = 0; nBoard[7][3] = 0; nBoard[7][4] = 0; nBoard[7][5] = WHITE | MAN; nBoard[7][6] = 0; nBoard[7][7] = WHITE | MAN; (getmove1)((int*)nBoard,BLACK,5,s,&playnow,0,0,&LCBmove); for(y = 0; y <= 7; y++) for(x = 0; x <= 7; x++) cout << "nBoard[" << x << "][" << y << "] = " << nBoard[x][y] << endl; system("PAUSE"); return 0; } |
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Hallo STaRDoGGCHaMP.
Da ja move: PMove ein Pointer auf eine in der Dll referenzierten Memory (Heap oder Stack?) ist, vermute ich mal, dass mit FreeLibrary(hDll); du an deinem Eigenen Ast sägst. Hier stellt sich die Frage was macht die DLL mit dem allocierten Speicher?? Dies würde auch das unkonventionelle Sleep(6000) erklären. 1. Also, Lade die Dll 2. Mach deinen API Aufruf 3. Kopiere dier die notwendigen, von der DLL gelieferten Daten auf den Heap. 4. Entlade das Teil 5. Arbeite dann mit den kopierten Daten 6. Und lass das Sleep weg. lg. Astat |
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Das versteh ich nicht, was hat das Sleep und Freelibrary damit zu tun? Ich krieg als Fehlermeldung "Ungültige Gleitkommaoperation", das heisst in der Funktion findet ein Fehler statt, direkt nach dem Aufruf.
|
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Also wenns't schon alles probiert hast, versuch doch anstatt bei Double 5 Double 5.0 zu übergeben.
Ansonsten, lade mal die Dll hoch, so kann ich ja mal schaun wo es genau hak't. lg. Astat |
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Ich glaub ich kann mir mittlerweile ungefähr vorstellen woran die ungültige Gleitkommaoperation liegt. Wenn man die Funktion ausführt wird in der Konsole ein Text ausgegeben, und in meinem C-Beispiel von Oben sieht das so aus:
![]() Man beachte hierbei das rote Kästchen. Anscheinend handelt es sich um eine ungültige Gleitkommazahl, welche bei C einfach so ausgegeben wird (-1.$) und bei Delphi zum Absturz führt, was allerdings sinnlos ist, da die Funktion trotzdem funktioniert. Ich würd das gerne fixxen, aber besitze leider nicht den Source-Code der Dll. Kann man da sonst noch irgendwie Abhilfe schaffen? [EDIT] Ich denke mal ich habs hinbekommen. Hab jetzt in nem anderen Forum gelesen, dass Delphi anscheinend Probleme mit Gleitkomma/zahlen/prüfungen hat. Folgender Befehl hat Abhilfe geschafft:
Delphi-Quellcode:
Trotzdem vielen Dank an alle die, die geholfen haben :mrgreen:
Set8087CW($133F);
|
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Hallo STaRDoGGCHaMP.
Naja, womöglich Brutalmethode mit ausgeschalteter Bereichs und Überlaufprüfung und FPU control word (precision, exception masks) versuchen.
Delphi-Quellcode:
{$R-} {$Q-}
var CurErrorMode: UINT; FPUControlWord: Word; begin CurErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS); try asm FNSTCW FPUControlWord end; try GetMove(nBoard, PGC_BLACK, 5, str, playnow, 0, 0, @Move); finally asm FNCLEX; FLDCW FPUControlWord end; end; finally SetErrorMode(CurErrorMode); end; end; lg. Astat |
Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import
Hi!
Könntest du bitte das Bild anhängen, anstatt es einzubinden. Das reduziert die Ladezeit des Threads sicher erheblich und das Bild bleibt auch erhalten, falls der externe Hoster mal down ist... Grüße, Frederic |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:34 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz