P/Invoke - call unmanaged DLL from C#

Mike Stone

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call an unmanaged dll always from same thread in C#

From Dev

How to Call [Unmanaged C++ DLL func] from [C# WP8.1 WRT/SL] project

From Dev

How to PInvoke C++ DLL Function from C#

From Dev

PInvoke C++ dll from C# throws Exception

From Dev

Returning a string from an unmanaged c++ dll to c#

From Dev

Return List/Array from unmanaged C# DLL

From Dev

C - properly importing stdcall functions from unmanaged DLL

From Dev

Converting mixed type return data from an unmanaged C DLL?

From Dev

Using unmanaged dll with c#

From Dev

Why C# PInvoke can't work with unmanaged DirectX

From Dev

Call unmanaged code from c#. Getting data using IntPtr

From Dev

Unable to call 32-bit unmanaged DLL from 64-bit managed code

From Dev

Call C dll from Fortran

From Dev

C# and PInvoke into 32/64 bit DLL

From Dev

Error calling a function from unmanaged dll

From Dev

How to send a function pointer using Unmanaged Exports from C++ to C# dll to be used as callback

From Dev

Subscribing to windows messages through unmanaged c++ dll from c# net core

From Dev

Passing byte array from C++ unmanaged dll to C# unity

From Dev

Calling convention for an unmanaged DLL in C#

From Dev

How to define unmanaged dll dependency in C#

From Dev

Returning a string from a C# DLL with Unmanaged Exports to Inno Setup script

From Dev

Call C dll function from C#

From Dev

Using managed dll (which uses unmanaged dll) in c# project

From Dev

Not able to call the function from C DLL

From Java

Call C# dll from a Java Application

From Dev

Call .dll function from C# Ironpython

From Dev

call c dll from delphi with strings

From Dev

Can not call a function from a C# DLL

From Dev

Call a method in program from dll in C#

Related Related

HotTag

Archive