时间:2021-05-20
例子1如下:
复制代码 代码如下:
//声明部分
[DllImport("um_web_client.dll")]
private static extern int CheckUserName(string UserName);
//
public static int AuthUserName(string username)
{
if(!IsConnection())
{
StartWebClient(UDBClientIP,UDBClientPort,UDBClientTimeout);
}
return CheckUserName(username);
}
但是如果c++方法中用了指针或者结构体等则引用就要注意,相应就要用到引用数组或者指针,例子2:
声明部分
复制代码 代码如下:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi )]
public class tagPlayerInfoOut
{
public int id;
public String PlayerName;
public int Country;
public int OfficerRace;
public int Level;
public int MilitaryRank;
public int Money;
public int ResourceCount;
public int CityCount;
public int GeneralCount;
public int Credit;
}
[ StructLayout( LayoutKind.Sequential )]
public struct tagPlayerInfoOutEx
{
public int id;
public IntPtr PlayerName;
public int Country;
public int OfficerRace;
public int Level;
public int MilitaryRank;
public int Money;
public int ResourceCount;
public int CityCount;
public int GeneralCount;
public int Credit;
}
[DllImport("um_web_client.dll")]
public static extern int GetPlayerListCs(int maxrows, out int rows, out IntPtr playerlist);
[DllImport("um_web_client.dll")]
public static extern int GetPlayerListCs(int maxrows, out int rows, tagPlayerInfoOutEx** playerlist);
方法
复制代码 代码如下:
public static DataTable UsingMarshal()
{
if(!IsConnection())
{
StartWebClient(UDBClientIP,UDBClientPort,UDBClientTimeout);
}
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("PlayerName");
dt.Columns.Add("Country");
dt.Columns.Add("OfficerRace");
dt.Columns.Add("Level");
dt.Columns.Add("MilitaryRank");
dt.Columns.Add("Money");
dt.Columns.Add("ResourceCount");
dt.Columns.Add("CityCount");
dt.Columns.Add("GeneralCount");
dt.Columns.Add("Credit");
int size = 0;
IntPtr outArray;
GetPlayerListCs(20, out size, out outArray);
tagPlayerInfoOut[] manArray = new tagPlayerInfoOut[size];
IntPtr current = outArray;
//Console.WriteLine("记录数{0}",size);
for( int i = 0; i < size; i++ )
{
manArray[ i ] = new tagPlayerInfoOut();
Marshal.PtrToStructure(current, manArray[i]);
Marshal.DestroyStructure(current, typeof(tagPlayerInfoOut) );
current = (IntPtr)((long)current + Marshal.SizeOf(manArray[i]));
dt.Rows.Add(new object[] {manArray[i].id,manArray[i].PlayerName,manArray[i].Country,manArray[i].OfficerRace,manArray[i].Level,manArray[i].MilitaryRank,manArray[i].Money,manArray[i].ResourceCount,manArray[i].CityCount,manArray[i].GeneralCount,manArray[i].Credit});
dt.AcceptChanges();
//Console.WriteLine( "Element {0}: {1} {2}", i, manArray[i].id, manArray[i].PlayerName);
}
Marshal.FreeCoTaskMem(outArray);
return dt;
}
public static unsafe DataTable UsingUnsafe()
{
int size;
if(!IsConnection())
{
StartWebClient(UDBClientIP,UDBClientPort,UDBClientTimeout);
}
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("PlayerName");
dt.Columns.Add("Country");
dt.Columns.Add("OfficerRace");
dt.Columns.Add("Level");
dt.Columns.Add("MilitaryRank");
dt.Columns.Add("Money");
dt.Columns.Add("ResourceCount");
dt.Columns.Add("CityCount");
dt.Columns.Add("GeneralCount");
dt.Columns.Add("Credit");
tagPlayerInfoOutEx* pResult;
GetPlayerListCs(20, out size, &pResult );
tagPlayerInfoOutEx* pCurrent = pResult;
//Console.WriteLine("记录数{0}",size);
for( int i = 0; i < size; i++, pCurrent++ )
{
//Console.WriteLine( "Element {0}: {1} {2}", i, pCurrent->id ,Marshal.PtrToStringAnsi(pCurrent->PlayerName));
//Marshal.FreeCoTaskMem(pCurrent->PlayerName );
dt.Rows.Add(new object[] {pCurrent->id,pCurrent->PlayerName,pCurrent->Country,pCurrent->OfficerRace,pCurrent->Level,pCurrent->MilitaryRank,pCurrent->Money,pCurrent->ResourceCount,pCurrent->CityCount,pCurrent->GeneralCount,pCurrent->Credit});
dt.AcceptChanges();
}
Marshal.FreeCoTaskMem((IntPtr)pResult );
return dt;
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例展示了C++与C#互调dll的实现步骤,在进行大型项目共享dll动态链接库中可以用到。具体方法如下:一、C#调用C++dll步骤(只能导出方法):1.c
项目中遇到C#调用C++算法库的情况,C++内部运算结果返回矩形坐标数组(事先长度未知且不可预计),下面方法适用于访问C++内部分配的任何结构体类型数组。当时想
最近研究C#相关的OCR技术,图像识别一般C和C++这种底层语言做的比较多,C#主要是依托一些封装好的组件进行调用,这里介绍一种身份证识别的方法。环境搭建下载地
我是在ubuntu系统进行实验的,所以和window可能会有区别。python调用C/C++有不少的方法,如boost.python,swig,ctypes,p
前言之前已经跟大家介绍了在ubuntu系统下C++调用matlab程序的方法,需要的朋友们可以参考这篇文章,本文将给大家介绍关于windows下C++调用mat