简体   繁体   English

PInvoke错误将C ++ dll封送入C#,将结构传递给方法

[英]PInvoke error marshaling C++ dll into C#, passing struct into method

Hi all I am trying to compile a C++ project into a DLL and then use the Structs and methods from C#. 大家好,我试图将C ++项目编译为DLL,然后使用C#中的Structs和方法。 I made an openCV program and I need to pass data back and forth. 我做了一个openCV程序,我需要来回传递数据。 For now I am trying simple things to get familiar with the language since it is the first time I touch C++ and C# ... 现在,我正在尝试一些简单的事情来熟悉该语言,因为这是我第一次接触C ++和C#。

So in C++ I have : 所以在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)

And I try to use it from C# like this : 我尝试像这样从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);

But no matter what I try I get errors that the method's type signature is not PInvoke compatible. 但是,无论我尝试什么,都会收到错误消息,指出该方法的类型签名与PInvoke不兼容。

is there anyone that could help me understand this issue? 有没有人可以帮助我了解这个问题?

Is it even possible to pass and get custom structs among C++ and C# ? 甚至有可能在C ++和C#之间传递并获取自定义结构?

because the end stuct will be much more complex in the end since I have to pass images with data like coordinates etc... 因为最终结构会变得更加复杂,因为我必须传递带有坐标等数据的图像...

正如@SaniSinghHuttunen所提到的,我错过了ref关键字,不得不使用MarshalAs.U4 ...再次感谢:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM