[英]PInvoke marshaling a struct pointer from ANSI C as an IntPtr in C#
[英]PInvoke error marshaling C++ dll into C#, passing struct into method
大家好,我试图将C ++项目编译为DLL,然后使用C#中的Structs和方法。 我做了一个openCV程序,我需要来回传递数据。 现在,我正在尝试一些简单的事情来熟悉该语言,因为这是我第一次接触C ++和C#。
所以在C ++中,我有:
#pragma pack(push, 2)
typedef struct
{
//char *plate;
unsigned int width;
unsigned int height;
//bool hasPlateOn;
//unsigned char *imgData;
} Sframe;
extern "C" Sframe MYCVAPI findPossiblePlates(Sframe& in)
我尝试像这样从C#使用它:
[StructLayout(LayoutKind.Sequential)]
struct Sframe
{
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
// char[] plate;
[MarshalAs(UnmanagedType.U4)]
int width;
[MarshalAs(UnmanagedType.U4)]
int height;
// bool hasPlateOn;
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
// char[] imgData;
}
[DllImport(@"ALPR_pwv.dll",CallingConvention=CallingConvention.Cdecl,EntryPoint = "findPossiblePlates")]
static extern Sframe findPossiblePlates(Sframe infoOut);
但是,无论我尝试什么,都会收到错误消息,指出该方法的类型签名与PInvoke不兼容。
有没有人可以帮助我了解这个问题?
甚至有可能在C ++和C#之间传递并获取自定义结构?
因为最终结构会变得更加复杂,因为我必须传递带有坐标等数据的图像...
正如@SaniSinghHuttunen所提到的,我错过了ref关键字,不得不使用MarshalAs.U4 ...再次感谢:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.