Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   .NET-Framework (managed code) (https://www.delphipraxis.net/79-net-framework-managed-code/)
-   -   C# Delphi-Win32-DLL-Funktion mit Result Boolean wirft eine AccessViolationException (https://www.delphipraxis.net/167516-delphi-win32-dll-funktion-mit-result-boolean-wirft-eine-accessviolationexception.html)

MarioR 3. Apr 2012 12:21

Delphi-Win32-DLL-Funktion mit Result Boolean wirft eine AccessViolationException
 
Hallo,
ich nutze eine Delphi-Win32-Dll in C# (.Net 4.0 VS2010).
Alle Funktionen arbeiten einwandfrei, bis auf die, welche eine Delphi-Boolean als
Rückgabewert haben. Diese schmeißen eine "AccessViolationException".

hier etwas Code:

Delphi function-Typedef:
Delphi-Quellcode:
DbOpen = function: Boolean; stdcall;

C# function Typedef:
Code:
[return: MarshalAs(UnmanagedType.Bool)]
protected delegate Boolean TMatriKSDbOpen();
protected TMatriKSDbOpen matriKSDbOpen;
C#-Seite Delphi-Dll dynamisch laden
Code:
...LoadLibraryEx...
        public Delegate CreateDynamicDllDelphi(string functionName, Type delegateType)
        {
            IntPtr funcAddr = GetProcAddress(dllHandle, functionName);
            if (funcAddr == IntPtr.Zero)
                throw new EntryPointNotFoundException("Function: " + functionName);
            return Marshal.GetDelegateForFunctionPointer(funcAddr, delegateType);
        }
matriKSDbOpen = (TMatriKSDbOpen)CreateDynamicDllDelphi("DbOpen", typeof(TMatriKSDbOpen));
Benutzen der C#-Seite:
Code:
bool openOk = matriKSDbOpen(); // throw AccessViolationException
ebenfals habe ich getestet:
[return: MarshalAs(UnmanagedType.I1)]

... und nun bin ich mit meinem Latein am ende :cry:

mkinzler 3. Apr 2012 12:28

AW: Delphi-Win32-DLL-Funktion mit Result Boolean wirft eine AccessViolationException
 
Nimm mal einen COM-Boolean (WordBool oder LongBool)

himitsu 3. Apr 2012 12:58

AW: Delphi-Win32-DLL-Funktion mit Result Boolean wirft eine AccessViolationException
 
In C sind die Boolean meist 32/64 Bit (Registerbreite), während der Delphi-Boolean nur 8 Bit (1 byte) ist.

Aber das sollte beim Result nicht in einer Exception enden.
Höchstens in false-positives, wo das False dann fälschlicher Weise als true erkannt wird, da die zusätzlichen Bytes nicht unbedingt 0 sein müssen,

MarioR 4. Apr 2012 10:28

AW: Delphi-Win32-DLL-Funktion mit Result Boolean wirft eine AccessViolationException
 
Hallo,

wir (unser Delphi-Progger und ich) haben jetzt alles Mögliche probiert.
Diese besch... AccessViolationException bleibt, :evil:.

Lange Rede... das Boolean wird durch String (PChar '0' or '1') ersetzt,
das geht wenigstens.

Delphi Funktion-TypeDef
Delphi-Quellcode:
DbHelp = function: PChar; stdcall;


C# Function-TypeDef
Code:
[return: MarshalAs(UnmanagedType.AnsiBStr)]
protected delegate String TMatriKSDbHelp();
liebe Grüße
Mario

himitsu 4. Apr 2012 12:00

AW: Delphi-Win32-DLL-Funktion mit Result Boolean wirft eine AccessViolationException
 
Beim PChar als Result muß man aber auch etws aufpassen,
nicht daß der "verlinkte" Text plötzlich weg ist, nach dem Beenden der Funktion, und der PChar dann ins Nichts zeigt.

EWeiss 4. Apr 2012 14:28

AW: Delphi-Win32-DLL-Funktion mit Result Boolean wirft eine AccessViolationException
 
Schau mal hier vielleicht hilft es dir.
http://www.delphipraxis.net/1159521-post31.html

Allerdings lade ich die DLL nicht dynamisch und erstelle eine Wrapper_Class um die DLL.
Dieser Wrapper(DLL) wird dann als Reference ins Projekt eingebunden.

gruss

Horst0815 16. Apr 2012 14:08

AW: Delphi-Win32-DLL-Funktion mit Result Boolean wirft eine AccessViolationException
 
Ändere mal
Delphi-Quellcode:
DbOpen = function: Boolean; stdcall;
in

Delphi-Quellcode:
DbOpen = function: LongBool; cdecl;


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:38 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