关于C#调用C++dll的问题

在c#调用dll中遇到了个巨坑,请大侠&大神帮忙
c++定义:
//! 长度为10的字符串
typedef char TAPISTR_10[11];
typedef char TAPICHAR;
typedef char TAPIDTSTAMP[24];

//! int 32
typedef int TAPIINT32;
//! unsigned 32
typedef unsigned int TAPIUINT32;
//! int 64
typedef long long TAPIINT64;
//! unsigned 64
typedef unsigned long long TAPIUINT64;
//! unsigned 16
typedef unsigned short TAPIUINT16;
//! unsigned 8
typedef unsigned char TAPIUINT8;
//! real 64
typedef double TAPIREAL64;

typedef TAPIREAL64 TAPIQPRICE;
typedef TAPIUINT64 TAPIQVOLUME;
typedef TAPIINT64 TAPIQDIFF;

struct TapAPICommodity
{
TAPISTR_10 ExchangeNo;

TAPICommodityType CommodityType;

TAPISTR_10 CommodityNo;

};

struct TapAPIContract
{
TapAPICommodity Commodity;

TAPISTR_10 ContractNo1;

TAPISTR_10 StrikePrice1;

TAPICallOrPutFlagType CallOrPutFlag1;

TAPISTR_10 ContractNo2;

TAPISTR_10 StrikePrice2;

TAPICallOrPutFlagType CallOrPutFlag2;

};

struct TapAPIQuoteWhole
{
TapAPIContract Contract;

TAPISTR_10 CurrencyNo;

TAPICHAR TradingState;

TAPIDTSTAMP DateTimeStamp;

TAPIQPRICE QPreClosingPrice;

TAPIQPRICE QPreSettlePrice;

TAPIQVOLUME QPrePositionQty;

TAPIQPRICE QOpeningPrice;

TAPIQPRICE QLastPrice;

TAPIQPRICE QHighPrice;

TAPIQPRICE QLowPrice;

TAPIQPRICE QHisHighPrice;

TAPIQPRICE QHisLowPrice;

TAPIQPRICE QLimitUpPrice;

TAPIQPRICE QLimitDownPrice;

TAPIQVOLUME QTotalQty;

TAPIQPRICE QTotalTurnover;

TAPIQVOLUME QPositionQty;

TAPIQPRICE QAveragePrice;

TAPIQPRICE QClosingPrice;

TAPIQPRICE QSettlePrice;

TAPIQVOLUME QLastQty;

TAPIQPRICE QBidPrice[20];

TAPIQVOLUME QBidQty[20];

TAPIQPRICE QAskPrice[20];

TAPIQVOLUME QAskQty[20];

TAPIQPRICE QImpliedBidPrice;

TAPIQVOLUME QImpliedBidQty;

TAPIQPRICE QImpliedAskPrice;

TAPIQVOLUME QImpliedAskQty;

TAPIQPRICE QPreDelta;

TAPIQPRICE QCurrDelta;

TAPIQVOLUME QInsideQty;

TAPIQVOLUME QOutsideQty;

TAPIQPRICE QTurnoverRate;

TAPIQVOLUME Q5DAvgQty;

TAPIQPRICE QPERatio;

TAPIQPRICE QTotalValue;

TAPIQPRICE QNegotiableValue;

TAPIQDIFF QPositionTrend;

TAPIQPRICE QChangeSpeed;

TAPIQPRICE QChangeRate;

TAPIQPRICE QChangeValue;

TAPIQPRICE QSwing;

TAPIQVOLUME QTotalBidQty;

TAPIQVOLUME QTotalAskQty;

};

请问TapAPIContract怎么转换成C#的对应类型,我转换的时候已经成功转换了char数组。字符显示正常,但是double和long等数值类的类型全都不对。急急急~

1

3个回答

同事遇到一个问题,C#调用c++的接口,返回值总是错误,但是c++的同事单步调试时正确的
经过半天调试才发现,是C++接口定义的问题

c++接口返回值定义为了bool。在x86平台c++中的bool占用1个字节,C#中占用4个字节

在编译器默认的对其方式下,调用出现了混乱

解决办法:c++接口不使用bool改为BOOL或者int等......
答案就在这里:C#调用C++的dll问题
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

0
LEOXI82
LEOXI82 这个不是我想要的答案。。。
接近 4 年之前 回复

先看下,c++下和c#下结构体大小是否有差异,c#下默认4字节对齐

0

已解决。。。。。。。。。(*^__^*) 嘻嘻……

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!