簡體   English   中英

PInvoke錯誤將C ++ dll封送入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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM