C#调用C++提供的dll出现的问题

syntongs 2008-02-26 01:56:20
我使用c++提供的dll调用的时候出现这个问题
运行库遇到了错误。此错误的地址为 0x79e71bd7,在线程 0x11f0 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。
c++的结构为
function GetPosStatus(IP: PChar; Port: Integer; AQryGateWay: PQryGateWayInfoRet): Integer; export; stdcall;

PQryGateWayInfoRet = ^TQryGateWayInfoRet;
TQryGateWayInfoRet = packed record
BasicResponse: TBasicResponse;
RunInfo: TtagINFORUN;
DebugInfo: TtagINFODebug;
end;

TtagINFORUN = packed record
bLoginOK: Integer;
bGetFLowNoOK: Integer;
bDispInitOK: Integer;
PrivateSign: LongWord;
CtlStatus: Char;
ErrInfo: array[0..127] of char;
PosStatus: array[0..127] of Byte;
StatusBar485: Byte;
CurrDineNo: Byte;
end;
TtagINFODebug = packed record
FlowHead: LongWord;
FlowTail: LongWord;
CurrFlowNo: LongWord;
SNo: LongWord;
end;
C#为:
[DllImport("GateWayDll.dll")]
public static extern int GetPosStatus(byte[] ip,int port,ref TQryGateWayInfoRet ps);

[StructLayout(LayoutKind.Sequential)]
public struct TtagINFODebug
{
public UInt32 FlowHead;
public UInt32 FlowTail;
public UInt32 CurrFlowNo;
public UInt32 SNo;
}


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TQryGateWayInfoRet
{
public TBasicResponse tbRes;
public TtagINFORUN tgRun;
public TtagINFODebug tgDebug;
}
[StructLayout(LayoutKind.Sequential)]
public struct TtagINFORUN
{
public UInt16 bLoginOK;
public UInt16 bGetFLowNoOK;
public UInt16 bDispInitOK;
public UInt32 PrivateSign;
public byte CtlStatus;
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
public string ErrInfo;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] PosStatus;
public byte StatusBar485;
public byte CurrDineNo;
}
[StructLayout(LayoutKind.Sequential)]
public struct TBasicResponse
{
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 3)]
public string TranCode;
public UInt16 RetCode;
}

...全文
1184 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
syntongs 2008-02-27
  • 打赏
  • 举报
回复
还是出同样的问题
运行库遇到了错误。此错误的地址为 0x79e71bd7,在线程 0x11f0 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TQryGateWayInfoRet
{
public TBasicResponse tbRes;
public TtagINFORUN tgRun;
public TtagINFODebug tgDebug;
}
王集鹄 2008-02-27
  • 打赏
  • 举报
回复
* syntongs
* syntongs
* 等 级:
发表于:2008-02-27 10:12:2416楼 得分:0
zswang还在吗,那个嵌套结构是不是定义的有问题呀?

这种提问方式没有一点参考价值。

是不是有问题我怎么知道。-_-!!!“GateWayDll.dll”又不是我提供的。
什么现象?输入什么输出什么?你期望的结果是什么?什么才是正确的结果?确认是因为api封装导致?
你自己先分析清楚。
如果代码不是那么多,建议你装个Delphi直接用Delphi做一下测试。
beyondtkl 2008-02-27
  • 打赏
  • 举报
回复
伴水老大好。。

应该就是内存对齐的问题 packed就是按照1字节对齐

那个嵌套结构是不是定义的有问题呀?
你现在是怎么定义的。
syntongs 2008-02-27
  • 打赏
  • 举报
回复
zswang还在吗,那个嵌套结构是不是定义的有问题呀?
syntongs 2008-02-27
  • 打赏
  • 举报
回复
怎么没人了
syntongs 2008-02-26
  • 打赏
  • 举报
回复
谢谢!
王集鹄 2008-02-26
  • 打赏
  • 举报
回复
没有调试环境,就只能帮到这儿了。
syntongs 2008-02-26
  • 打赏
  • 举报
回复
仔细调了下,没问题了,但是结果好象有点问题,ErrInfo和PosStatus这两个里面全为null,好象不正常
syntongs 2008-02-26
  • 打赏
  • 举报
回复
哦,我搞错了,但是这样还是出现以前的错误
王集鹄 2008-02-26
  • 打赏
  • 举报
回复
看好,复制好。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TBasicResponse
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] TranCode;
public int RetCode;
}
syntongs 2008-02-26
  • 打赏
  • 举报
回复
C#
TBasicResponse=5
TtagINFORUN=275
TtagINFODebug=16
TQryGateWayInfoRet=296
你的那样写出来的结果是这样
syntongs 2008-02-26
  • 打赏
  • 举报
回复
他们的大小你是怎么计算出来的,就是上面的
syntongs 2008-02-26
  • 打赏
  • 举报
回复
GetPosStatus没有原始调用例子,按照了你的写法,还是会出现以前的错误
王集鹄 2008-02-26
  • 打赏
  • 举报
回复
通过Marshal.SizeOf()方法可以计算结构的大小。
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("TBasicResponse={0}", Marshal.SizeOf(typeof(TBasicResponse)));
Console.WriteLine("TtagINFORUN={0}", Marshal.SizeOf(typeof(TtagINFORUN)));
Console.WriteLine("TtagINFODebug={0}", Marshal.SizeOf(typeof(TtagINFODebug)));
Console.WriteLine("TQryGateWayInfoRet={0}", Marshal.SizeOf(typeof(TQryGateWayInfoRet)));
}


Delphi中也有SizeOf()函数。
王集鹄 2008-02-26
  • 打赏
  • 举报
回复
结构这样声明就一样大小了。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TtagINFODebug
{
public uint FlowHead;
public uint FlowTail;
public uint CurrFlowNo;
public uint SNo;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TQryGateWayInfoRet
{
public TBasicResponse tbRes;
public TtagINFORUN tgRun;
public TtagINFODebug tgDebug;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TtagINFORUN
{
public int bLoginOK;
public int bGetFLowNoOK;
public int bDispInitOK;
public uint PrivateSign;
public byte CtlStatus;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] ErrInfo;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] PosStatus;
public byte StatusBar485;
public byte CurrDineNo;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TBasicResponse
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] TranCode;
public int RetCode;
}


函数大概是这样:
[DllImport("GateWayDll.dll", CharSet = CharSet.Ansi)]
public static extern int GetPosStatus(string ip, int port,
ref TQryGateWayInfoRet ps);


“TtagINFORUN.ErrInfo”
获取文字信息可以通过:Encoding.Default.GetString()。
先调用不出异常再考虑吧

GetPosStatus有原始调用例子吗?
syntongs 2008-02-26
  • 打赏
  • 举报
回复
上面的你是怎么得出的
syntongs 2008-02-26
  • 打赏
  • 举报
回复
你帮我改改麻烦
王集鹄 2008-02-26
  • 打赏
  • 举报
回复
大小比较:
Delphi
TBasicResponse=7
TtagINFORUN=275
TtagINFODebug=16
TQryGateWayInfoRet=298

C#
TBasicResponse=8
TtagINFORUN=152
TtagINFODebug=16
TQryGateWayInfoRet=176

明显不一样,这个得改。。。
syntongs 2008-02-26
  • 打赏
  • 举报
回复
那可能就是delphi的
TBasicResponse = packed record
TranCode: array[0..2] of char;//交易码
RetCode: Integer;
TBasicResponse在这,麻烦你帮我看看,搞了几天了一直出现那样的错误
王集鹄 2008-02-26
  • 打赏
  • 举报
回复
C++写的DLL?这个明显是Delphi的声明。
没有看到TBasicResponse的原始声明?
你先比较一下两边结构的大小是否一致。

110,538

社区成员

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

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

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