![]() |
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 18:46 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