简体   繁体   English

C# 和 C++,out 参数在方法中不返回任何内容

[英]C# and C++, out parameter does not return anything in a method

I need help with a method, it is a method imported from a dll, the method is in C++ and it is the following:我需要一个方法的帮助,它是从 dll 导入的方法,该方法在 C++ 中,如下所示:

DECLSPEC int WINAPI I15693_Inventorys(HID_DEVICE device, BYTE *pData, BYTE *pLen);

The correct translation to C# is this (Provided by the manufacturer): C# 的正确翻译是这样的(由制造商提供):

public static extern int I15693_Inventorys(IntPtr device, byte[] pData, ref byte pLen);

The problem is that the method has 2 OUT parameters (The pData and pLen ones), one of which has a ref and the other does not, I need the value of the second parameter's out but I can't get it, does not return anything, if I modify the method and add a ref for that parameter the program crashes, any ideas?问题是该方法有 2 个 OUT 参数(pData 和 pLen 参数),其中一个有 ref 而另一个没有,我需要第二个参数的值但我无法得到它,不返回什么,如果我修改方法并为该参数添加一个引用程序崩溃,有什么想法吗? This is my code:这是我的代码:

byte[] chip = Array.Empty<byte>();
byte longitud = 0;
_ = RFIDReader.Sys_SetAntenna(g_hDevice, 0);
_ = RFIDReader.Sys_InitType(g_hDevice, 1);
_ = RFIDReader.Sys_SetAntenna(g_hDevice, 1);
int resultado = RFIDReader.I15693_Inventorys(g_hDevice, chip, ref longitud);

Oh, and the method returns me an error code of 0, which executes correctly, but without returning anything from the second parameter.哦,该方法返回一个错误代码 0,它正确执行,但没有从第二个参数返回任何内容。 Also say that the method in c++ seems that both parameters have a pointer, so in its translation both should have a ref, right?还说c++中的方法好像两个参数都有一个指针,所以在它的翻译中都应该有一个ref,对吧?

EDIT:编辑:

方法的文档

You should pin the value for handling in unmanaged memory.您应该将值固定在非托管 memory 中进行处理。

byte longitud = 0;
GCHandle gch = GCHandle.Alloc(longitud, GCHandleType.Pinned);

// Call your function
int resultado = RFIDReader.I15693_Inventorys(g_hDevice, chip, ref longitud);

gch.Free();

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

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