您好,登錄后才能下訂單哦!
這篇文章主要介紹“c#如何調用c++”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“c#如何調用c++”文章能幫助大家解決問題。
例子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;
}
關于“c#如何調用c++”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。