我正在尝试使用DLLImport
来使用C ++ DLL中的函数,当我尝试调用该函数时遇到了这个错误,我看到了其他答案,但我仍然很困惑关于问题的出处,如果这很愚蠢,请您道歉。
谢谢。
这是C ++代码:
struct DocProcHandle {
unsigned long Size;
void* UserHandle;
const char* DeviceName;
unsigned long DeviceTypeId;
unsigned long DeviceState;
unsigned long OpenFlags;
void* Reserved1;
};
typedef struct DocProcHandle *DPHandle;
BPS_DP_API DPHandle CDECLCALL_CONV DPOpenHandleEx (
unsigned long deviceTypeId,
const char* trkBaseWrite,
const char* trkBaseConfig,
const unsigned long openFlags
);
这是C#代码:
[DllImport("DocProc.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern DPHandle DPOpenHandleEx(
uint deviceType,
[MarshalAs(UnmanagedType.LPStr)]string trkBaseWrite,
[MarshalAs(UnmanagedType.LPStr)]string trkBaseConfig,
uint openFlags
);
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)]
public struct DPHandle
{
public uint Size;
IntPtr UserHandle;
[MarshalAs(UnmanagedType.LPStr)] string DeviceName;
uint DeviceTypeId;
uint DeviceState;
uint OpenFlags;
IntPtr Reserved1;
};
它基本上显示“ C#方法的类型签名与P / Invoke不兼容”错误。我知道这与如何定义参数和结构有关,但是这里需要帮助。
答案 0 :(得分:-1)
由于@GSerg,我通过将返回值更改为IntPtr并使用Marshal.PtrToStructure来解决此问题。