C++方法调用问题,高手请进

Jack2013tong 2009-02-11 10:48:03
//获取排行
int UMWEBCLIENT GetPlayerList(const int maxrows, int* rows, tagPlayerInfo** ppPlayerInfo);
这是c++写的一个方法,um_web_client.dll是供调用的dll
ppPlayerInfo 是结构体
// public struct tagPlayerInfo
// {
// public int id;
// public char[] 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")]
请问在c#中如何声明及调用这个方法,请高手们给出具体的写法,谢谢!
...全文
184 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙宜坡 2009-02-12
  • 打赏
  • 举报
回复
unsafe

也许可以,但最好不这么处理!
tweeger 2009-02-12
  • 打赏
  • 举报
回复
如果可以使用unsafe的话,可以使用如下的写法:
1、测试dll,用来模拟um_web_client.dll
struct tagPlayerInfo
{
public:
int id;
int Money;
};

extern "C" _declspec(dllexport) int GetPlayerList(tagPlayerInfo** ppPlayerInfo)
{
if(ppPlayerInfo != NULL && *ppPlayerInfo != NULL)
{
(*ppPlayerInfo)->id = 1;
(*ppPlayerInfo)->Money = 1;

MessageBox(NULL, (LPCWSTR)("Successed"), (LPCWSTR)("GetPlayerList"), MB_OK);

return 1;
}

return 0;
}
2、C#中调用
public struct tagPlayerInfo
{
public int id;
public int Money;
}

[DllImport("um_web_client.dll", EntryPoint = "GetPlayerList")]
private static extern unsafe int GetPlayerList(tagPlayerInfo** ppPlayerInfo);

private void button3_Click(object sender, EventArgs e)
{
tagPlayerInfo test = new tagPlayerInfo();
test.id = 0;
test.Money = 0;

int code = 0;
unsafe
{
tagPlayerInfo* tagPoint = &test;
code = GetPlayerList(&tagPoint);
}
Console.WriteLine(code);
}

经过测试可以实现lz的需求。
Jack2013tong 2009-02-12
  • 打赏
  • 举报
回复
GetPlayerList(maxrows, ref rows, ppPlayerInfo);
//ppPlayerInfo.id
你这样是取得一个结构体的信息?怎么取出下一个呢?原方法里是一个结构体的数据集,有多个,请问怎么写?
龙宜坡 2009-02-12
  • 打赏
  • 举报
回复
        public struct tagPlayerInfo
{
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;
};
[DllImport("um_web_client.dll")]
public static extern int GetPlayerList(int maxrows, ref int rows, [MarshalAs(UnmanagedType.LPStruct)]tagPlayerInfo ppPlayerInfo);

//使用
public void SomeMethod()
{
int maxrows = 10, rows = 0;
tagPlayerInfo ppPlayerInfo=new tagPlayerInfo();
GetPlayerList(maxrows, ref rows, ppPlayerInfo);
//ppPlayerInfo.id
}
Jack2013tong 2009-02-12
  • 打赏
  • 举报
回复
不行,out ppPlayerInfo出来的ppPlayerInfo为null,
龙宜坡 2009-02-12
  • 打赏
  • 举报
回复
public struct tagPlayerInfo
{
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;
};
[DllImport("um_web_client.dll")]
public static extern int GetPlayerList(int maxrows, ref int rows, out tagPlayerInfo[] ppPlayerInfo);

//使用
public void SomeMethod()
{
int maxrows=10, rows = 0;
tagPlayerInfo[] ppPlayerInfo;
GetPlayerList(maxrows, ref rows, out ppPlayerInfo);
}


看这样行不?
xiaxy1 2009-02-12
  • 打赏
  • 举报
回复
菜鸟帮顶
zjh222 2009-02-12
  • 打赏
  • 举报
回复
互操作是NET最麻烦的操作,为什么微软不把Win Api包装完整呀??
Jack2013tong 2009-02-12
  • 打赏
  • 举报
回复
还没有得到答案,哪位高手帮下忙?
Jack2013tong 2009-02-12
  • 打赏
  • 举报
回复
[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);

这样声明可以,测试通过
tweeger 2009-02-12
  • 打赏
  • 举报
回复
参照MSDN有如下写法:
[DllImport("um_web_client.dll", EntryPoint = "GetPlayerList")]
private static extern int GetPlayerList(int maxrows, ref int rows, IntPtr ppPlayerInfo);

tagPlayerInfo test = new tagPlayerInfo();
...
// Initialize unmanged memory to hold the struct.
IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(test));
try
{
// Copy the struct to unmanaged memory.
Marshal.StructureToPtr(test, pointer, false);

IntPtr[] pointArray = new IntPtr[1];
pointArray[0] = pointer;

IntPtr addrPointer = Marshal.UnsafeAddrOfPinnedArrayElement(pointArray, 0);
code = GetPlayerList(addrPointer);

// Set this tagPlayerInfo to the value of the
// tagPlayerInfo in unmanaged memory.
test = (tagPlayerInfo)Marshal.PtrToStructure(pointer, typeof(tagPlayerInfo));
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pointer);
}
ljyxr 2009-02-12
  • 打赏
  • 举报
回复
不会 帮顶
宝_爸 2009-02-11
  • 打赏
  • 举报
回复
two level指针tagPlayerInfo** ppPlayerInfo

估计够戗

可以参考

Passing Structures
http://msdn.microsoft.com/en-us/library/awbckfbz.aspx
龙宜坡 2009-02-11
  • 打赏
  • 举报
回复
public struct tagPlayerInfo
{
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;
};
[DllImport("um_web_client.dll")]
public static extern int GetPlayerList(int maxrows, ref int rows,out tagPlayerInfo[] ppPlayerInfo);
tweeger 2009-02-11
  • 打赏
  • 举报
回复
lz可以做如下尝试:
1、在C#中使用class来描述tagPlayerInfo结构;
2、在C#中描述GetPlayerList方法时使用类似(..., ref tagPlayerInfo ppPlayerInfo)的方法;
3、在调用GetPlayerList方法时传入tagPlayerInfo对象的引用。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧