[英]How to pass *& and **& parameter to C++ dll from C# code
I have C++ dll as par below我在下面有 C++ dll
Function prototype and structure definition Function原型及结构定义
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;
Implementation of DoOPeration function is as par below DoOPeration function 的实现如下
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);
}
Corresponding with the above I have added C# side code as par below与上述相对应,我添加了 C# 侧代码,如下所示
DLL import, structure(class) definition 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);
After calling C++ dll function from c# code, I have observed the values of the function parameter inside c++ dll. After calling C++ dll function from c# code, I have observed the values of the function parameter inside c++ dll.
Please let me know information regarding passing of 2nd parameter ie(**&) from c# code.请让我知道有关从 c# 代码传递第二个参数 ie(**&) 的信息。
Also please let me know, if I have done any other mistakes so that I can correct it.另外,如果我犯了任何其他错误,请告诉我,以便我纠正。
Note: I can not change the C++ dll code注意:我无法更改 C++ dll 代码
I got the solution with below approach.我通过以下方法得到了解决方案。
C# side code C#边码
DLL import, structure definition DLL 导入,结构定义
[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注意:要在 C# 端设置 byte[] 数据,我使用了 [IntPtr] 和以下方法
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.