0

我有一个 C#.NET 2.0 CF 应用程序,它从具有以下签名的本机 DLL 导入函数:

__declspec( dllexport ) void DLL_Foo( int count, ... );

我的 C# 应用程序 P/Invokes 该功能如下:

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

它没有问题,所以它与我的__arglist用法有关。

4

2 回答 2

1

我不确定(我从来没有做过)你是否可以params在 P/Invoke 中使用 args,但你可以试一试。

internal static extern void DLL_Foo(int count, params string[] args);
于 2010-12-15T23:30:56.453 回答
0

您应该使用 CallingConvention = CallingConvention.Cdecl您的 DllImport。描述说明了这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));
于 2012-08-31T10:05:33.270 回答