![](/img/trans.png)
[英]'Cannot marshal 'return value': Invalid managed/unmanaged type combination.' while marshalling delegate with string array
[英]Calling C++ dll from C#. “Cannot marshal 'return value': Invalid managed/unmanaged type combination.”
我的頭文件。
extern "C" class MyFuncs
{
public:
__declspec(dllexport) unsigned char PassImage(unsigned char buffer, int size);
};
CPP文件。
unsigned char MyFuncs::PassImage(unsigned char buffer, int size)
{
return buffer;
}
一切正常,除非我將緩沖區返回到主應用程序。
[DllImport("ExampleDLL.dll", EntryPoint = "?PassImage@MyFuncs@Funcs@@QAEXEH@Z")]
public static extern byte[] PassImage(byte[] a, int count);
當我將無符號字符返回到byte []時發生錯誤。
如果我將byte []更改為byte,我會得到一個沒有錯誤的值。
這是確切的錯誤:
無法封送“返回值”:無效的托管/非托管類型組合。
如何接受將未簽名的char返回給byte []?
unsigned char
應該是unsigned char *
:)
編輯:您還需要傳遞數組的長度並將其作為C#中的字節指針進行處理,因為.NET不知道其長度。 本文中描述的內容: https : //stackoverflow.com/questions/8268625/get-pointer-on-byte-array-from-unmanaged-c-dll-in-c-sharp
在C ++中,您返回一個unsigned char
,該字符長一個字節。 在C#中,您需要一個字節數組 。 您可能想從C ++返回一個unsigned char *
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.