CSharp如何调用dll中带指针参数.docx
《CSharp如何调用dll中带指针参数.docx》由会员分享,可在线阅读,更多相关《CSharp如何调用dll中带指针参数.docx(7页珍藏版)》请在冰豆网上搜索。
![CSharp如何调用dll中带指针参数.docx](https://file1.bdocx.com/fileroot1/2022-11/19/b71e20b3-7c92-4090-bab8-23e75f1dd070/b71e20b3-7c92-4090-bab8-23e75f1dd0701.gif)
CSharp如何调用dll中带指针参数
CSharp如何调用dll中带指针参数
C#调用C++DLL的方法,不同参数类型的调用方法
1.参数为基本类型,例如int,float,char等
[C++]
voidfun(intvalue);
voidfun(floatvaue);
voidfun(charch);
[C#]
[DllImport("xxx.dll")]
publicstaticexternvoidfun(Int32value);
[DllImport("xxx.dll")]
publicstaticexternvoidfun(floatvalue);
[DllImport("xxx.dll")]
publicstaticexternvoidfun(charch);
2.参数为基本类型+指针,例如int*,float*,char*等
[C++]
voidfun(int*value);
voidfun(float*vaue);
voidfun(char*ch);
[C#]
[DllImport("xxx.dll")]
publicstaticexternvoidfun(refInt32value);
[DllImport("xxx.dll")]
publicstaticexternvoidfun(reffloatvalue);
参数为char*,在C#中有几种实现方式
A.publicstaticexternvoidfun(stringch);//ch内容不会改变
B.publicstaticexternvoidfun(StringBuilderch);//ch内容会改变
3.参数为结构体
[C++]
structpoint
{
intvalue;//基本类型
charch;//基本类型
intnumber[100];//数组
charbuffer[100];//字符串数组
};
voidfun(pointpt);
[C#]
[StructLayout(LayoutKind.Sequential)]
publicstructpoint
{
publicInt32value;
publiccharch;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=100)]
publicInt32[]number;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]
publicchar[]buffer;
}
[DllImport("xxx.dll")]
publicstaticexternvoidfun(pointpt);
4.参数为结构体指针
[C++]
voidfun(point*pt);
[C#]
[DllImport("xxx.dll")]
publicstaticexternvoidfun(refpointpt);
5.参数为结构体,并且结构体还嵌套结构体
[C++]
structpoint
{
intvalue;//基本类型
charch;//基本类型
intnumber[100];//数组
charbuffer[100];//字符串数组
structpointpt;//嵌套结构体
};
voidfun(pointpt);
[C#]
[StructLayout(LayoutKind.Sequential)]
publicstructpoint
{
publicInt32value;
publiccharch;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=100)]
publicInt32[]number;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]
publicchar[]buffer;
publicpointpt;
}
[DllImport("xxx.dll")]
publicstaticexternvoidfun(pointpt);
6.参数为结构体,并且结构体还嵌套结构体指针或者双指针
[C++]
structpoint
{
intvalue;//基本类型
charch;//基本类型
intnumber[100];//数组
charbuffer[100];//字符串数组
structpoint*p1;//嵌套结构体指针
structpoint**p2;//嵌套结构体双指针
};
voidfun(pointpt);
[C#]
[StructLayout(LayoutKind.Sequential)]
publicstructpoint
{
publicInt32value;
publiccharch;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=100)]
publicInt32[]number;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]
publicchar[]buffer;
publicIntPtrp1;//使用IntPtr替代嵌套指针
publicIntPtrp2;//使用IntPtr替代嵌套指针
}
[DllImport("xxx.dll")]
publicstaticexternvoidfun(pointpt);
由于嵌套指针的使用比较复杂,需要借助一些手段才能够确保正常调用DLL方法
[C#]
classProgram
{
[StructLayout(LayoutKind.Sequential)]
publicstructpoint
{
publicInt32value;
publiccharch;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=100)]
publicInt32[]number;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]
publicchar[]buffer;
publicIntPtrp1;//使用IntPtr替代嵌套指针
publicIntPtrp2;//使用IntPtr替代嵌套指针
}
[DllImport("xxx.dll")]
publicstaticexternvoidfun(pointpt);
staticvoidMain(string[]args)
{
pointpt=newpoint();
pt.p1=Marshal.AllocHGlobal(Marshal.Sizeof(typeof(point)));
pt.p2=Marshal.AllocHGlobal((Marshal.SizeOf(typeof(point)))*2);
try
{
pt.value=1;
//实现给p1赋值
pointptt=newpoint();
ptt.value=1;
Marshal.StructureToPtr(ptt,pt.p1,false);
//实现给p2赋值
//由于双指针不能够直接传值,需要用到中间结构的数组指针
IntPtr[]ptr=newIntPtr[2];
ptr[0]=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(point)));
ptr[1]=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(point)));
try
{
Marshal.StructureToPtr(ptt,ptr[0],false);
Marshal.Copy(ptr,0,pt.p2,2);
fun(pt);
//测试返回的指针值是否正确
ptt=(point)Marshal.PtrToStructure(pt.p1,typeof(point));
Marshal.Copy(pt.p2,ptr,0,2);
ptt=(point)Marshal.PtrToStructure(ptr[0],typeof(point));
ptt=(point)Marshal.PtrToStructure(ptr[1],typeof(point));
}
catch(System.Exceptione)
{
stringstr=e.Message;
}
finally
{
Marshal.FreeHGlobal(ptr[0]);
Marshal.FreeHGlobal(ptr[1]);
}
}
catch(System.Exceptione)
{
stringstr=e.Message;
}
finally
{
Marshal.FreeHGlobal(pt.p1);
Marshal.FreeHGlobal(pt.p2);
}
}
}