Ho una DLL compilata in Delphi 2007 e un esempio che la utilizza in un altro progetto Delphi. Ecco una parte di codice:Chiamare una funzione DLL delphi dal codice C#
TErrorCallback = function(Msg:PChar):byte of object;
TSaveEventCallback = function (Line:PChar; HiCode:PChar; LoCode:PChar; MobileNo:PChar):byte of object;
function InitModule(ErrorCallback:TErrorCallback; SaveEventCallback :TSaveEventCallback; MainWindowHandle:THandle; Sock_Event:integer):byte; stdcall; external 'My.dll' name 'InitModule';
function DLLSocketEvent(var msg: TMessage): byte; stdcall; external 'My.dll' name 'DLLSocketEvent';
function InitObjList(Objs: array of PChar; NumObjs: byte; Name: PChar):byte; stdcall; external 'My.dll' name 'InitObjList';
Ed ecco la mia C# analogica:
class Message
{
unsigned int msg;
int wParam;
int lParam;
int result;
};
delegate byte ErrorCallbackDelegate(string msg);
delegate byte SaveEventCallbackDelegate(string line, string hiCode, string loCode, string mobileNo);
[DllImport("My.dll")]
static extern byte InitModule(ErrorCallbackDelegate errorCallback, SaveEventCallbackDelegate saveEventCallback, IntPtr mainWindowsHandle, Int32 sockEvent);
[DllImport("My.dll")]
static extern byte DllSocketEvent(Message msg);
[DllImport("My.dll")]
static extern byte InitObjList(string[] objs, byte numObjs, string name);
Il punto è che ho provato unico metodo initmodule e gettò un'eccezione: Una chiamata alla funzione PInvoke 'ProjTest! ProjTest.MyClass :: InitModule' ha sbilanciato lo stack. Ciò è probabile perché la firma PInvoke gestita non corrisponde alla firma di destinazione non gestita. Verificare che la convenzione di chiamata ei parametri della firma PInvoke corrispondano alla firma non gestita di destinazione.
Per favore, aiutami con questo. Come dovrei descrivere queste funzioni DLL in C#?
Perché il voto negativo? –