C# P/调用具有可变签名的函数
我有一个C#.NET 2.0 CF应用程序,它从本机DLL导入具有以下签名的函数:C# P/调用具有可变签名的函数,c#,compact-framework,pinvoke,variadic,C#,Compact Framework,Pinvoke,Variadic,我有一个C#.NET 2.0 CF应用程序,它从本机DLL导入具有以下签名的函数: __declspec( dllexport ) void DLL_Foo( int count, ... ); 我的C#应用程序p/调用该函数如下: public sealed class MyObject { public void Foo() { NativeMethods.DLL_Foo(2, __arglist("a","b")); } interna
__declspec( dllexport ) void DLL_Foo( int count, ... );
我的C#应用程序p/调用该函数如下:
public sealed class MyObject
{
public void Foo()
{
NativeMethods.DLL_Foo(2, __arglist("a","b"));
}
internal static class NativeMethods
{
[DllImport("My.dll")]
internal static extern void DLL_Foo(int count, __arglist);
}
}
但是,当我调用MyObject.Foo
时,我会得到一个System.MissingMethodException
我需要做什么改变才能让这一切顺利进行
谢谢,
保罗
编辑:如果我将导入定义更改为:
internal static extern void DLL_Foo(int count, [MarshalAs(UnmanagedType.LPWStr)]string a, [MarshalAs(UnmanagedType.LPWStr)]string b);
然后,致电:
NativeMethods.DLL_Foo(2, "a", "b");
它工作起来没有问题,所以这与我的\uu arglist
用法有关。我不确定(我从来没有这样做过)是否可以在p/Invoke中使用参数,但您可以尝试一下
internal static extern void DLL_Foo(int count, params string[] args);
您应该为DllImport使用CallingConvention=CallingConvention.Cdecl
。CallingConvention.Cdecl
说明中说明了这一点
using LPWORD = System.IntPtr;
using LPVOID = System.IntPtr;
[DllImport("foo.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern LPVOID extFunc(LPWORD lpdwMandatory,__arglist);
然后可以调用extFunc函数:
extFunc(lp1,__arglist( 0xFF,0x6A,0xAA));
那很有趣。一旦我进入NativeMethods.DLL_-Foo(2,新字符串[]{“a”,“b”})
方法,我就不再继续了。程序保持运行,但不会继续到下一行代码。我还尝试了coredll.dll中的wsprintf()。同样的结果。我应该看看设备屏幕。“本机异常发生在…”中,代码为0x8000002,我认为这是数据类型不对。@Paul我认为您可能必须有多个带多个字符串的重载,这应该可以解决问题,但无法达到您想要的效果。它似乎在非紧凑型框架中工作。