c# - 如何从 C# 代码将 *& 和 **& 参数传递给 C++ dll
问题描述
我有下面的 C++ dll
函数原型和结构定义
extern "C" int __declspec(dllexport) DoOPeration(DataStruct *&, DataStructInfo **&);
typedef struct
{
int count;
float CountData;
bool flg;
BYTE* data;
char info[100];
}DataStruct;
typedef struct
{
int count;
bool flg;
BYTE* data;
}DataStructInfo;
DoOPeration 功能的实现如下
int __declspec(dllexport) ImageAnalyze(DataStruct *&all, DataStructInfo **&part)
{
int ret = 0;
try
{
if(all->count == 0)
return(ERR_PARAMERROR);
for(cnt = 0 ; cnt < all->count ; cnt++)
{
if(part[cnt]->data== NULL)
return(ERR_NO_IMAGE);
}
}
catch(int err)
{
return(err);
}
catch(...)
{
return(-2);
}
return(ret);
}
与上述相对应,我添加了 C# 端代码,如下所示
DLL 导入,结构(类)定义
[DllImport(@"C:\TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int DoOperation(ref DataStruct all, ref DataStructInfo[] part);
[StructLayout(LayoutKind.Sequential)]
public class DataStruct
{
int count = 0;
float CountData = 0;
bool flg = 0;
byte[] data = null;
string info = 0;
}
[StructLayout(LayoutKind.Sequential)]
public class DataStructInfo
{
int count = 0;
bool flg = 0;
byte[] data;
}
// DoOperation function calling
DataStruct alls = new DataStruct(); // set all required parameter
DataStructInfo[] part= new DataStructInfo[3]; // set all required parameter
int ret = DoOperation(ref alls, ref part);
从 c# 代码调用 C++ dll 函数后,我观察了 c++ dll 中函数参数的值。
- 对于参数 [alls] 值正确传递
- 对于参数 [part] 值未正确传递(显示一些垃圾值)
请让我知道有关从 c# 代码传递第二个参数 ie(**&) 的信息。
另外,如果我犯了任何其他错误,请告诉我,以便我纠正。
注意:我无法更改 C++ dll 代码
解决方案
I got the solution with below approach.
C# side code
DLL import, structure definition
[DllImport(@"C:\TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int DoOperation(ref IntPtr all, ref IntPtr[] part);
[StructLayout(LayoutKind.Sequential)]
public struct DataStruct
{
public int count { get; set; }
public float CountData { get; set; }
public bool flg { get; set; }
public IntPtr data { get; set; }
public string info { get; set; }
}
[StructLayout(LayoutKind.Sequential)]
public struct DataStructInfo
{
public int count { get; set; }
public bool flg { get; set; }
public IntPtr data { get; set; }
}
// First parameter
DataStruct alls = new DataStruct(); // set all required parameter
IntPtr intPtrsAll = new IntPtr();
intPtrsAll = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(DataStruct)));
Marshal.StructureToPtr<DataStruct>(alls, intPtrsAll, false);
// Second parameter
DataStructInfo[] part= new DataStructInfo[3]; // set all required parameter
IntPtr[] intPtrsParts= new IntPtr[part.Length];
for (int i = 0; i < intPtrsParts.Length; i++)
{
intPtrsParts[i] = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(DataStructInfo)));
Marshal.StructureToPtr<DataStructInfo>(part[i], intPtrsParts[i], false);
}
int ret = DoOperation(ref intPtrsAll, ref intPtrsParts);
Note: To set byte[] data at C# side, I have used [IntPtr] with below approach
byte[] rowData = GetRawBytes(dataHandler); // function to read byte[] data
int size = Marshal.SizeOf(rowData[0]) * rowData.Length;
data = Marshal.AllocHGlobal(size); // IntPtr property of struct's
Marshal.Copy(rowData, 0, data, rowData.Length);
推荐阅读
- pattern-matching - 可匹配列表到模式转换(鸡方案)
- javascript - 如何在 JavaScript 中引用抽象对象键?
- ide - 如何在 WebStorm 中对 JSX 的单引号使用“重写源代码”更改双引号?
- react-native - 使用标题呈现时,React Native FlatList 超出屏幕范围
- ios - IOS中具有多个标签的动态高度表单元格
- javascript - js-yaml可以映射行号吗?
- javascript - 在js中组装元素后事件未触发
- pytorch - PyTorch 和 Google Colab:ImageFolder 不会加载所有图像
- c# - 如何使用 NON-NULL 默认值将 null 传递给 SQL Server 存储过程参数
- python - 如何在两个工作簿中的列中搜索匹配值并将来自同一匹配行的不同列的值写入其中一个工作簿?