1

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.

xanatos
  • 109,618
  • 12
  • 197
  • 280
Mike Stone
  • 319
  • 9
  • 22

1 Answers1

3

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.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Xanatos, Thanks for your help. I've never dealt with this before so i'm like a deer staring at headlights. I followed your link to read the article and took the example that you provided. I managed to get a success value of -21. This tells me that the C# method is talking to the DLL. except that it still doesn't like something with the parameter. That's ok b/c at least i am not getting that VS debugging error. One more question. Another function requires parameters of infoLine and a struct. I'm going to assume that I should follow the help that you've provided. Thanks again for your help – Mike Stone Aug 14 '13 at 15:18
  • @MikeStone Yes. As a sidenote, you are sure that you `char*` are really `char*` and not Unicode char, right? – xanatos Aug 14 '13 at 15:42