Import Pchar Delphi DLLs in C #?

I have a procedure in delphi:

procedure PasswordDLL(month integer; password pchar); export; 

The procedure should output the password to the "password" that I passed. From what I google..and read .... ref: HERE and HERE

I come up 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 I call:

 string pass = ""; PasswordDLL(2, pass); 

So, the password to output to the string "pass".

But I get BadImageFormatException was unhandled: an attempt was made to load a program with the wrong format. (Exception from HRESULT: 0x8007000B)

Does the function format I used is incorrect? I wonder if I used the wrong UnmanagedType for PChar, but from reading it is either LPWStr or LPStr .. Am I missing something?

Thanks in advance...

+4
source share
1 answer

Firstly, since you did not indicate which version of Delphi you are using, I will answer the assumption of Delphi 6 for some other reason than I am familiar with.

Your Delphi routine does not define a calling convention in its declaration, so it will not use stdcall according to your import. It will use the default Delphi register convention, which pushes the first few parameters into registers, rather than onto the stack. If you can modify the Delhpi DLL, add stdcall; after the announcement and adjustment, and your calling agreement will be consistent.

The table below summarizes the 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 

If you look at the .NET documentation, there does not seem to be a calling convention that matches the Delphi register convention (see table below), so I think the only option would be to change the convention in the Delphi DLL.

 Member name Description ----------- ------------------------ Cdecl The caller cleans the stack. This enables calling functions with varargs, which makes it appropriate to use for methods that accept a variable number of parameters, such as Printf. FastCall This calling convention is not supported. StdCall The callee cleans the stack. This is the default convention for calling unmanaged functions with platform invoke. ThisCall The first parameter is the this pointer and is stored in register ECX. Other parameters are pushed on the stack. This calling convention is used to call methods on classes exported from an unmanaged DLL. Winapi Supported by the .NET Compact Framework. This member is not actually a calling convention, but instead uses the default platform calling convention. For example, on Windows the default is StdCall and on Windows CE .NET it is Cdecl. 

Your Delphi (6) Pchar (null terminated ANSI string pointer) looks right.

+2
source

Source: https://habr.com/ru/post/1340993/


All Articles