【发布时间】:2012-12-09 09:21:41
【问题描述】:
我有一个带接口的 DLL
struct modeegPackage
{
uint8_t version; // = 2
uint8_t count; // packet counter. Increases by 1 each packet
uint16_t data[6]; // 10-bit sample (= 0 - 1023) in big endian (Motorola) format
uint8_t switches; // State of PD5 to PD2, in bits 3 to 0
};
__declspec(dllexport) void __cdecl initSerial();
__declspec(dllexport) void __cdecl closeSerialPort();
__declspec(dllexport) struct modeegPackage __cdecl getPackage();
和 C# 适配器
class EEGCommunication
{
[StructLayout(LayoutKind.Sequential)]
public struct modeegPackage
{
/// unsigned char
public byte version;
/// unsigned char
public byte count;
/// unsigned int[6]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.U2)]
public UInt16[] data;
/// unsigned char
public byte switches;
}
private const string DLL = "libneureader-lib.dll";
[DllImport(DLL, EntryPoint = "_Z10initSerialv")]
public static extern void InitSerial();
[DllImport(DLL, EntryPoint = "_Z15closeSerialPortv")]
internal static extern void CloseSerialPort();
[DllImport(DLL, EntryPoint = "_Z10getPackagev", CallingConvention = CallingConvention.Cdecl)]
public static extern modeegPackage GetPackage();
}
但是当我尝试调用GetPackage 方法时,我收到错误Method's type signature is not PInvoke compatible.
我的代码有什么问题?
更新:代码更新
【问题讨论】:
-
为什么会有c#、c++和c标签?
-
因为有C/C++和C#写的代码
-
@skayred 您的 P/Invoke 签名不正确,我在下面发布了正确答案。