P/Invoke - call unmanaged DLL from C#

2019-07-27 05:12发布

问题:

I'm attempting to call an unmanaged DLL from C#. I've very little experience with unmanaged code and P\Invoke so I was hoping for an extra pair of eyes.

I don't have much information of the DLL except the function:

int Initialize(char *FileName, int Driver, int(*InfoLine)(char*), char *Message);

Infoline can be null.

So this is what I did in C#.
The import call:

[DllImport(@"c:\Core\initialization.dll", EntryPoint="Initialize", CharSet = CharSet.Auto)]
private static extern int Initialize([MarshalAs(UnmanagedType.LPStr)] string FileName, int Driver, System.IntPtr InfoLine, [MarshalAs(UnmanagedType.LPStr)] string Message);

The method call is:

IntPtr infoLine = IntPtr.Zero;
string message = "";
int success = Initialize(@"c:\config.dat", -1, infoLine, message);

The error message that Visual Studio gives back to me in debug mode is:

A call to PInvoke function 'Initialize' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Which parameter have I passed incorrectly?

I am fairly certain that the call to the DLL is correct because there is another function that has no parameters passed and similar import and method code worked.

Thanks for the help.

回答1:

Try putting in the DllImport(... , CallingConvention=CallingConvention.Cdecl)

There are 4 methods of passing parameters to a "native" function. You have to know which one is the right one (where to put them in memory, who has to remove them from memory after the function...). The default one is the StdCall. Your function probably uses Cdecl. See here: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention.aspx

If/when you decide to use the Infoline callback,

[UnmanagedFunctionPointer(CallingConvenction.Cdecl)]
private delegate int InfolineDelegate(
        [MarshalAs(UnmanagedType.LPStr)] string str);

private static extern int Initialize(
        [MarshalAs(UnmanagedType.LPStr)] string fileName, 
        int driver, 
        [MarshalAs(UnmanagedType.FunctionPtr)] InfolineDelegate infoLine, 
        [MarshalAs(UnmanagedType.LPStr)] string message);

If Initialize will use the delegate but won't store it:

Initialize("FileName", 1, MyMethod, "Message");

But if Initialize will store the delegate to use it at a later time, after returning:

// Put it outside the function... As a property/field of the class, for example
InfolineDelegate method = MyMethod;

Initialize("FileName", 1, method, "Message");

The InfolineDelegate method MUST have a guaranteed lifetime greater than the method you are using. So not a local variable. Normally a field/property of the calling class.