从非托管DLL调用函数

时间:2014-06-27 04:51:16

标签: c# dll unmanaged

我有一个具有以下功能的非托管DLL:

ReadLatch( HANDLE cyHandle,
                LPWORD  lpLatch);


WriteLatch(HANDLE cyHandle,
                WORD    mask,
                WORD    latch);



GetPartNumber(HANDLE cyHandle,
                   LPBYTE   lpbPartNum);

 GetDeviceProductString(HANDLE  cyHandle,
                            LPVOID  lpProduct,
                            LPBYTE  lpbLength,
                            BOOL    bConvertToASCII = TRUE
                            );


GetDeviceSerialNumber(HANDLE   cyHandle,
                           LPVOID   lpSerialNumber,
                           LPBYTE   lpbLength,
                           BOOL     bConvertToASCII = TRUE
                           );


 GetDeviceInterfaceString(HANDLE    cyHandle,
                              LPVOID lpInterfaceString,
                              LPBYTE lpbLength,
                              BOOL bConvertToASCII);

我正在尝试导入这些函数,但没有太多可能找到正确的数据类型:

[DllImportAttribute("runtime.dll", EntryPoint = "ReadLatch", CallingConvention =     CallingConvention.Cdecl)]
    static extern int ReadLatch(HANDLE cyHandle, [MarshalAs(UnmanagedType. ??????)] ?????? lpLatch);

    [DllImportAttribute("runtime.dll", EntryPoint = "WriteLatch", CallingConvention = CallingConvention.Cdecl)]
    static extern int WriteLatch(HANDLE cyHandle,
                WORD mask,
                WORD latch);

    [DllImportAttribute("runtime.dll", EntryPoint = "GetPartNumber", CallingConvention = CallingConvention.Cdecl)]
    static extern int GetPartNumber(HANDLE cyHandle,
                   LPBYTE lpbPartNum);

    [DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceProductString", CallingConvention = CallingConvention.Cdecl)]
    static extern int GetDeviceProductString(HANDLE cyHandle,
                            LPVOID lpProduct,
                            LPBYTE lpbLength,
                            BOOL bConvertToASCII = TRUE
                            );

    [DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceSerialNumber", CallingConvention = CallingConvention.Cdecl)]
    static extern int GetDeviceSerialNumber(HANDLE cyHandle,
                           LPVOID lpSerialNumber,
                           LPBYTE lpbLength,
                           BOOL bConvertToASCII = TRUE
                           );

    [DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceInterfaceString", CallingConvention = CallingConvention.Cdecl)]
    static extern int GetDeviceInterfaceString(HANDLE cyHandle,
                              LPVOID lpInterfaceString,
                              LPBYTE lpbLength,
                              BOOL bConvertToASCII);

我在哪里可以找到有关如何代表HANDLE,LPWORD和其他人的信息,以便我可以调用这些函数?

1 个答案:

答案 0 :(得分:1)

非托管类型及其管理对应方:

  • HANDLE通常由IntPtr表示。
  • WORD - UInt16

对于其他人,我们可能需要更多地了解它们的使用方式。

希望您的API附带一些文档可以解释参数的作用,因为其中一些参数不是很明显。

对于这个功能,我们可以做一些假设:

ReadLatch(HANDLE cyHandle, LPWORD  lpLatch);

假设lpLatch真的是" out"参数(以及您的返回类型为int):

[DllImportAttribute("runtime.dll", EntryPoint = "ReadLatch", CallingConvention = CallingConvention.Cdecl)]
static extern int ReadLatch(IntPtr cyHandle, out UInt16 lpLatch);