(最高难度)C#调用c++ DLL(返回结构数组指针) ,顶者有分!

gyf19 2007-02-05 01:40:00
如何把返回c++结构数组指针,c#结构数组?? 请大侠帮助

c++定义:
QUERY* QueryTransactionHistory(DWORD dwIP,int nPort,TRANSACTION * ,int* outNum,int* nErrorCode);

TRANSACTION结构:
typedef struct TRANSACTION
{
char account[19];
int maxNum;
char startDate[16];
char endDate[16];
}TRANSACTION;

QUERY结构:
typedef struct QUERY
{
int id;
int itemAmount;
int gold;
int silver;
char itemName[30];
}QUERY;

...全文
3466 54 打赏 收藏 转发到动态 举报
写回复
用AI写文章
54 条回复
切换为时间正序
请发表友善的回复…
发表回复
laneagle 2010-05-08
  • 打赏
  • 举报
回复
楼主要说话算话啊!
tommir3 2010-05-08
  • 打赏
  • 举报
回复
为了看着方便,整理一下

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class TRANSACTION
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 19)]
public string account;

public int maxNum;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string startDate;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string endDate;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class QUERY
{
public int id;
public int itemAmount;
public int gold;
public int silver;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
public string itemName;
}

class Program
{
[DllImport( "CPPDLL.DLL ", CallingConvention = CallingConvention.StdCall)]
public extern static IntPtr QueryTransactionHistory(uint swIP, int nPort, TRANSACTION pTran, out int cnt, out int nErrCode);

static void Main(string[] args)
{
int cnt, errCode;
TRANSACTION tran = new TRANSACTION();

tran.account = "account ";
tran.endDate = "endDate ";
tran.startDate = "startDate ";
tran.maxNum = 50;

//get retrun value as IntPtr
IntPtr ptr = QueryTransactionHistory(0, 0, tran, out cnt, out errCode);
Console.WriteLine( "============ CS =============== ");
Console.WriteLine( "Count = {0}, errCode = {1} ", cnt, errCode);

IntPtr head = ptr;
for (int i = 0; i < cnt; ++i)
{
//extract struct from pointer
QUERY q = (QUERY)Marshal.PtrToStructure(ptr, typeof(QUERY));
Console.WriteLine( "id = {0}, gold = {1}, silver = {2}, itemAmount = {3}, itemName = {4} ",
q.id, q.gold, q.silver, q.itemAmount, q.itemName);

//move pointer by sizeof(QUERY) to the next one
//WARNING: assume 32 bit machine!!!!!!
ptr = (IntPtr)(ptr.ToInt32() + Marshal.SizeOf(typeof(QUERY)));
}
//TODO: need to free memory
}
}


typedef struct TRANSACTION
{
char account[19];
int maxNum;
char startDate[16];
char endDate[16];
} TRANSACTION;

typedef struct QUERY
{
int id;
int itemAmount;
int gold;
int silver;
char itemName[30];
}QUERY;

extern "C " _declspec(dllexport) QUERY* QueryTransactionHistory(DWORD dwIP,int nPort,TRANSACTION * pTran, int* outNum,int* nErrorCode) {
cout < < "dwIP = " < < dwIP < < ", nPort = " < < nPort < < ", pTran -> maxNum = " < < pTran -> maxNum < < endl;
cout < < "pTran -> account = " < < pTran -> account < < ", pTran -> startDate = " < < pTran -> startDate < < ", pTran -> endDate = " < < pTran -> endDate < < endl;

QUERY* ret = (QUERY* )malloc(sizeof(QUERY) * 2);
ret[0].id = 0;
ret[0].gold = ret[0].silver = ret[0].itemAmount = 1;
strcpy(ret[0].itemName, "Item0 ");

ret[1].id = 1;
ret[1].gold = ret[1].silver = ret[1].itemAmount = 2;
strcpy(ret[1].itemName, "Item1 ");

(*outNum) = 2;
(*nErrorCode) = 0;
return ret;
}

zj_newer 2010-04-29
  • 打赏
  • 举报
回复
帮忙顶,同时学习!
gyf19 2007-02-09
  • 打赏
  • 举报
回复
qqchen79(知秋一叶) 方法可以用,现在结帖
newskycity 2007-02-07
  • 打赏
  • 举报
回复
有趣!
angeldjd 2007-02-07
  • 打赏
  • 举报
回复
帮顶.
honkerhero 2007-02-07
  • 打赏
  • 举报
回复
结构要用引用来传,因为在C++中它其实是一个指针
huheng_0_0 2007-02-07
  • 打赏
  • 举报
回复
mark
gameboy766 2007-02-07
  • 打赏
  • 举报
回复
mark
jxf654 2007-02-07
  • 打赏
  • 举报
回复
up
OriesMap 2007-02-06
  • 打赏
  • 举报
回复
标记,留名。
顶一下,关注此问题。
vabug 2007-02-06
  • 打赏
  • 举报
回复
确实不好搞,我前两天才搞好了C#与com互相传递IStream
帮顶
penglewen 2007-02-06
  • 打赏
  • 举报
回复
up
leq_82 2007-02-06
  • 打赏
  • 举报
回复
帮顶
cengyidao 2007-02-06
  • 打赏
  • 举报
回复
呵呵,顶~~
lr21shiji 2007-02-06
  • 打赏
  • 举报
回复
佩服

blueice008 2007-02-06
  • 打赏
  • 举报
回复
yzqlee 2007-02-06
  • 打赏
  • 举报
回复
这种问题还是顶顶算了。
jiaguoli 2007-02-06
  • 打赏
  • 举报
回复
学习 顶!!
qqchen79 2007-02-06
  • 打赏
  • 举报
回复
这里是我的C++ DLL代码,和上面的C#测试过没有问题。
你得C++一边的代码是怎么实现的?

typedef struct TRANSACTION
{
char account[19];
int maxNum;
char startDate[16];
char endDate[16];
} TRANSACTION;

typedef struct QUERY
{
int id;
int itemAmount;
int gold;
int silver;
char itemName[30];
}QUERY;

extern "C" _declspec(dllexport) QUERY* QueryTransactionHistory(DWORD dwIP,int nPort,TRANSACTION * pTran, int* outNum,int* nErrorCode) {
cout << "dwIP = " << dwIP << ", nPort = " << nPort << ", pTran ->maxNum = " << pTran ->maxNum << endl;
cout << "pTran -> account = " << pTran ->account << ", pTran ->startDate = " << pTran ->startDate << ", pTran ->endDate = " << pTran ->endDate << endl;

QUERY* ret = (QUERY* )malloc(sizeof(QUERY) * 2);
ret[0].id = 0;
ret[0].gold = ret[0].silver = ret[0].itemAmount = 1;
strcpy(ret[0].itemName, "Item0");

ret[1].id = 1;
ret[1].gold = ret[1].silver = ret[1].itemAmount = 2;
strcpy(ret[1].itemName, "Item1");

(*outNum) = 2;
(*nErrorCode) = 0;
return ret;
}
加载更多回复(33)

110,536

社区成员

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

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

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