无法封送“返回值":无效的托管/非托管类型组合 [英] Cannot marshal 'return value': Invalid managed/unmanaged type combination
问题描述
我在非托管库中有此功能,我想在C#中调用它:
I have this function in an unmanaged library and I want to call it in C#:
无符号字符__stdcall InitDev(无符号字符comport,长BaudRate)
这是在C#中
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("*.dll", CallingConvention = CallingConvention.Cdecl,CharSet =CharSet.Auto)]
public static extern byte[] InitDev(byte[] comport, [Out]long BaudRate);
但是当我在C#中调用此函数时,出现以下错误
but when I call this function in C# I get the following error
无法封送'返回值':无效的托管/非托管类型组合."
"Cannot marshal 'return value': Invalid managed/unmanaged type combination."
string COM = "COM3";
byte[] ptr = new byte[1];
try
{
ptr = InitDev(Encoding.ASCII.GetBytes(COM), 9600);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
如果您提供一些指导,我是陌生的,我将能够解决此问题
I am new to this if a little guidance is provided I will be able to resolve this issue
推荐答案
假定您的本地调用类似于 unsigned char * InitDef(unsigned char *,long long& baudRate)
,那么您可能想要使用 IntPtr
代替 unsigned char *
.但是首先,由于它是托管/非托管混合,因此应该为输入和输出分配该 byte []
缓冲区:
Assuming your native call is something like unsigned char* InitDef(unsigned char*, long long &baudRate)
then you probably want to use IntPtr
in the place of your unsigned char*
. But first since it's managed/unmanaged mixing you should allocate that byte[]
buffer for input and output :
byte[] pInBuffer = Encoding.ASCII.GetBytes("COM3");
// allocate unmanaged memory
IntPtr inputBuffer = Marshal.AllocHGlobal(pInBuffer.Length * sizeof(byte));
Marshal.Copy(pInBuffer, 0, inputBuffer, pInBuffer.Length);
// now your inputBuffer contains a native pointer to the `unsigned char*`
// and since your function returns a new pointer to some `unsigned char*`
// just retrieve it to IntPtr
IntPtr result = InitDev(inputBuffer, 9600);
// free your allocated memory
Marshal.FreeHGlobal(inputBuffer);
在此阶段,您的结果
包含从 InitDev
返回的值.
At this stage your result
contains the value returned from InitDev
.
InitDev
函数的新定义
new definition of InitDev
function
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("*.dll", CallingConvention = CallingConvention.Cdecl,CharSet =CharSet.Auto)]
public static extern IntPtr InitDev(IntPtr comport, [Out] long BaudRate);
All of the necessary calls, you can find on this msdn page
由于您的本机呼叫看起来像这样: extern unsigned char __stdcall InitDev(unsigned char comport,long BaudRate);
我猜第一个参数只是您"COMn"
的最后一位数字(索引),因为 unsigned char
的最低可能值为0,而COM的最低索引可以为 0.
EDIT :
Since your native call looks like this :extern unsigned char __stdcall InitDev(unsigned char comport,long BaudRate);
I'm guessing that the first parameter is just the last digit ( index ) of your "COMn"
because unsigned char
's lowest possible value is 0 and COM's lowest index can be 0.
尝试使用此代码段:
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("*.dll", CallingConvention = CallingConvention.Cdecl,CharSet =CharSet.Auto)]
public static extern byte InitDev(byte comport, long BaudRate);
然后这样称呼它:
byte comIdx = 3;
byte result = InitDev(comIdx, 9600);
// byte should yield correct result now
这篇关于无法封送“返回值":无效的托管/非托管类型组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!