我在 Visual C++ 中有以下返回类型:
extern "C" __declspec(dllexport) unsigned char* _cdecl
getname(LPCTSTR Track1, int len)
我编写了以下代码以unsigned char*
在 C# 中获得正确的值:
[DllImport(_dllLocation, CallingConvention = CallingConvention.Cdecl)]
public static extern byte[] getname(string track1, int len);
我使用以下代码从另一个 .cs 文件中调用了上述方法:
string track = "hello12345";
byte[] name = UnsafeNativeMethods.getname(track, 160);
请告诉我在这里做错了什么,还请检查参数数据类型,即LPCTSTR
和string
。
unsigned char*
相当于byte[]
。_ 如果是这样,那么为什么我在 C# 文件中得到错误的值,而在 C++ 中它是正确的。
编辑:
在通过评论提出一些建议后,我将其更改为byte name
,byte[] name
但它显示了以下异常:
A first chance exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in DecryptionWS.dll
A first chance exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in DecryptionWS.dll
A first chance exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in System.ServiceModel.dll