AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi Funktion aus DLL (in C++ geschrieben) nach Delphi importiere
Thema durchsuchen
Ansicht
Themen-Optionen

Funktion aus DLL (in C++ geschrieben) nach Delphi importiere

Ein Thema von STaRDoGGCHaMP · begonnen am 13. Dez 2009 · letzter Beitrag vom 17. Dez 2009
Antwort Antwort
Seite 2 von 2     12   
STaRDoGGCHaMP

Registriert seit: 1. Okt 2007
34 Beiträge
 
#11

Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import

  Alt 15. Dez 2009, 15:35
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:
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.
(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:
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;
}
  Mit Zitat antworten Zitat
Astat

Registriert seit: 2. Dez 2009
Ort: München
320 Beiträge
 
Lazarus
 
#12

Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import

  Alt 15. Dez 2009, 17:33
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
Lanthan Astat
06810110811210410503210511511603209711003210010110 9032084097103
03211611111604403209711003210010110903210010510103 2108101116122
11610103209010110510810103206711110010103210511003 2068101108112
10410503210310111509910411410510109810111003211910 5114100046
  Mit Zitat antworten Zitat
STaRDoGGCHaMP

Registriert seit: 1. Okt 2007
34 Beiträge
 
#13

Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import

  Alt 15. Dez 2009, 18:50
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.
  Mit Zitat antworten Zitat
Astat

Registriert seit: 2. Dez 2009
Ort: München
320 Beiträge
 
Lazarus
 
#14

Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import

  Alt 15. Dez 2009, 19:24
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
Lanthan Astat
06810110811210410503210511511603209711003210010110 9032084097103
03211611111604403209711003210010110903210010510103 2108101116122
11610103209010110510810103206711110010103210511003 2068101108112
10410503210310111509910411410510109810111003211910 5114100046
  Mit Zitat antworten Zitat
STaRDoGGCHaMP

Registriert seit: 1. Okt 2007
34 Beiträge
 
#15

Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import

  Alt 17. Dez 2009, 13:07
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:
http://img14.imageshack.us/img14/9440/doublec.png
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: Set8087CW($133F); Trotzdem vielen Dank an alle die, die geholfen haben
  Mit Zitat antworten Zitat
Astat

Registriert seit: 2. Dez 2009
Ort: München
320 Beiträge
 
Lazarus
 
#16

Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import

  Alt 17. Dez 2009, 14:06
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
Lanthan Astat
06810110811210410503210511511603209711003210010110 9032084097103
03211611111604403209711003210010110903210010510103 2108101116122
11610103209010110510810103206711110010103210511003 2068101108112
10410503210310111509910411410510109810111003211910 5114100046
  Mit Zitat antworten Zitat
Benutzerbild von fkerber
fkerber
(CodeLib-Manager)

Registriert seit: 9. Jul 2003
Ort: Ensdorf
6.723 Beiträge
 
Delphi XE Professional
 
#17

Re: Funktion aus DLL (in C++ geschrieben) nach Delphi import

  Alt 17. Dez 2009, 15:54
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
Frederic Kerber
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:56 Uhr.
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