1

我正在尝试使用来自 ac# 程序的 Platform Invoke 访问 Newlands 扫描仪。它应该是相对直截了当的。我设置了我的 P/Invoke 并调用它。

[DllImport("NLcpfw.dll", EntryPoint = "cpfw_open", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr cpfw_open(string pwStrPort,
                                                     string pwStrParam,
                                                     int nMode);

public static void CallFromHere(){
            IntPtr hDev = cpfw_open("udp", "", CPFW_OM_NORMAL);
            //GCHandle handle = GCHandle.Alloc(HNLCPFW, GCHandleType.Pinned);
            System.Diagnostics.Debug.Assert(hDev != null);
            NewlandInterface.cpfw_close(hDev);

    }

一些它是如何不捡起来的。我得到一个未处理的 BadImageFormatException 确保它是一个有效的托管程序集 确保您为程序集提供了正确的路径

C++ 标头读取

__declspec(dllimport) HNLCPFW WINAPI cpfw_open(WCHAR *pwStrPort, WCHAR *pwStrParam, int nMode = CPFW_OM_NORMAL);


typedef struct
{
    void* hDev;
    int nMode;
    PNLCPFW_PLUG_API DevAPI;
    HINSTANCE hPlugDll;
    void *exData;
}NLCPFW,*HNLCPFW;

我假设在我的 p\Invoke 代码中使用 IntPtr 是可以的。

任何关于为什么会发生这种情况的想法都将不胜感激。我已经用exe复制了所有的dll。在这种情况下,这些是 NLcpfw.dll、cpfw_udp.dll、cpfw_tcp.dll、cpfw_hidpos.dll 等

谢谢leppie 我需要将平台更改为X86

4

0 回答 0