怎么用C++调用API
最好说明调用的格式,例如定义变量那样,数据类型+变量名,我想要这种格式的说明,首先说明一个API的详细用法,然后给出调用格式,总之使我能明白就行如果我明白了我追加分数Te...
最好说明调用的格式,例如 定义变量那样, 数据类型+ 变量名, 我想要这种格式的说明,
首先说明一个API的详细用法,然后给出调用格式, 总之使我能明白就行
如果我明白了我追加分数
TextOut这个也是API啊?? 展开
首先说明一个API的详细用法,然后给出调用格式, 总之使我能明白就行
如果我明白了我追加分数
TextOut这个也是API啊?? 展开
5个回答
展开全部
c#在调用c++方法或者window api时不能象调用c#本身写的dll类库那样直接通过引用dll就可以调用相应的方法,
而是要把要引用的dll放到bin中,现通过[DllImport("um_web_client.dll")]引用
例子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;
}
而是要把要引用的dll放到bin中,现通过[DllImport("um_web_client.dll")]引用
例子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;
}
展开全部
首先搞清楚你要用的API在哪个库里,然后将这个头文件包进来。。有的还要用到lib..
比如:
#include "windows.h"
TextOut(hdc, x, y, psText, iLength) ;
TextOut为windows.h中的API函数
比如:
#include "windows.h"
TextOut(hdc, x, y, psText, iLength) ;
TextOut为windows.h中的API函数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
和调用你自己定义的函数一样的
比如
int Sleep(int minisecond);
in windows.h
====================
#include <windows.h>
void main()
{
Sleep(1000);//wait 1 second
}
比如
int Sleep(int minisecond);
in windows.h
====================
#include <windows.h>
void main()
{
Sleep(1000);//wait 1 second
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <windows.h>后就可以调用了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
include <windows.h>后就可以调用了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询