P / Invoke - 从C#调用非托管DLL [英] P/Invoke - call unmanaged DLL from C#

查看:159
本文介绍了P / Invoke - 从C#调用非托管DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从C#调用非托管DLL。我没有非托管代码和P \Invoke的经验,所以我希望有一双额外的眼睛。



我没有太多的DLL信息除了函数:

  int初始化(char * FileName,int Driver,int(* InfoLine)(char *),char *信息); 

Infoline可以为空。



这是我在C#中所做的。

导入调用:

  [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);

方法调用是:

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

Visual Studio在调试模式下给我的错误消息是:


调用PInvoke函数Initialize使堆栈失衡。
这可能是因为托管的PInvoke签名不符合
非托管目标签名。检查PInvoke签名的调用约定和
参数是否匹配目标非托管
签名。


哪个参数我不正确地传递了



我相当确定对DLL的调用是正确的,因为有另一个函数没有传递参数,类似的import和方法代码也可以工作。



感谢您的帮助。

解决方案

code> DllImport(...,CallingConvention = CallingConvention.Cdecl)



有四种方法将参数传递给native 功能。你必须知道哪一个是正确的(在哪里把它们放在内存中,谁必须在功能之后将它们从内存中删除...)。默认值为 StdCall 。您的功能可能使用Cdecl。请参阅: http://msdn.microsoft.com/ en-us / library / system.runtime.interopservices.callingconvention.aspx



如果/当您决定使用 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);

如果初始化将使用委托但赢不存储它:

 初始化(FileName,1,MyMethod,Message); 

但是,如果初始化将会将代理存储到稍后再使用它:

  //将其放在函数外面...作为属性/字段该类,例如
InfolineDelegate method = MyMethod;

初始化(FileName,1,方法,消息);

InfolineDelegate方法 / strong>的使用寿命大于您使用的方法。所以不是一个局部变量。通常是调用类的字段/属性。


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.

解决方案

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.

这篇关于P / Invoke - 从C#调用非托管DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆