Importing Pchar Delphi DLL to C#? -
i have procedure in delphi:
procedure passworddll(month integer; password pchar); export;
the procedure should output password "password" pchar passed in.. google..and reading.... ref: here , here
i come with:
[dllimport( "delphipassword.dll", callingconvention = callingconvention.stdcall, charset = charset.ansi, entrypoint = "passworddll")] public static extern void passworddll( int month, [marshalas(unmanagedtype.lpstr)] string password );
then when call:
string pass = ""; passworddll(2, pass);
so password output "pass" string.
but badimageformatexception unhandled: attempt made load program incorrect format. (exception hresult: 0x8007000b)
sounds function format used wrong? wonder if used incorrect unmanagedtype pchar, reading, either lpwstr , lpstr.. did missed something?
thanks in advance...
first off since have not stated delphi version using answer assuming delphi 6 no other reason familiar it.
your delphi procedure not specify calling convention in declaration, won't using stdcall per import. use default delphi register convention places first few parameters in registers rather on stack. if can change delhpi dll add stdcall; after declaration , rebuild , calling conventions match.
the table below summarizes calling conventions.
directive parameter order clean-up passes parameters in registers? --------- --------------- -------- ------------------------------- register left-to-right routine yes pascal left-to-right routine no cdecl right-to-left caller no stdcall right-to-left routine no safecall right-to-left routine no
looking @ .net documentation there not seem calling convention matches delphi's register convention (see table below) think option may change convention in delphi dll.
member name description ----------- ------------------------ cdecl caller cleans stack. enables calling functions varargs, makes appropriate use methods accept variable number of parameters, such printf. fastcall calling convention not supported. stdcall callee cleans stack. default convention calling unmanaged functions platform invoke. thiscall first parameter pointer , stored in register ecx. other parameters pushed on stack. calling convention used call methods on classes exported unmanaged dll. winapi supported .net compact framework. member not calling convention, instead uses default platform calling convention. example, on windows default stdcall , on windows ce .net cdecl.
your delphi (6) pchar (pointer null terminated ansi string) marshalling looks correct.
Comments
Post a Comment