C#从C ++ DLL编组无符号char *数组(输入和输出) [英] C# Marshalling unsigned char* array from C++ DLL (in and out)
问题描述
我在将C#应用程序和现有的C ++ DLL之间的数据编组时遇到麻烦.需要注意的是,使它变得困难的是它是指向数组的无符号char指针,在C#中调用之后,我需要从所有字段访问数据.如果可能的话,我想避免使用不安全的代码.
I am having trouble marshalling data between a C# application and an existing C++ DLL. The caveats that are making this difficult are that it is an unsigned char pointer to an array, and I need access to the data (from all fields) after the call in C#. I would like to avoid using unsafe code, if at all possible.
这是C ++签名:
BYTE GetData(unsigned char *Data_Type, unsigned char *Data_Content, unsigned int *Data_Length);
我已经在C#中尝试了很多东西,但这就是我现在所拥有的:
I've tried a bunch of things in C#, but here's what I have now:
[DllImport("somecpp.dll")]
public static extern byte GetData([In][Out] ref byte Data_Type, [In][Out] ref byte[] Data_Content, [In][Out] ref int Data_Length);
然后调用它,我正在尝试:
Then calling it, I am attempting this:
byte retrievedData = GetData(ref data_type, ref data_content, ref data_length);
这绝对是行不通的,我不确定下一步该怎么做.有任何想法吗?谢谢!
This is definitely not working, and I'm not sure what to try next. Any ideas? Thanks!
推荐答案
您的ref byte[]
参数与unsigned char**
匹配.那是间接的太多层次了.
Your ref byte[]
parameter matches unsigned char**
. That's one level of indirection too many.
p/调用应该是
[DllImport("somecpp.dll")]
public static extern byte GetData(
ref byte Data_Type,
[In,Out] byte[] Data_Content,
ref uint Data_Length
);
该函数使用cdecl是合理的.我们不能从这里说出来.
It is plausible that the function uses cdecl. We can't tell that from here.
我也怀疑Data_Type
参数应该是out
而不是ref
.
I also suspect that the Data_Type
argument should be out
rather than ref
.
这篇关于C#从C ++ DLL编组无符号char *数组(输入和输出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!