在调用PInvoke时,我一直在随机获取访问冲突异常,结构版本似乎有效。类版本将随机提供访问冲突异常。问题是我在类版本和结构版本中有额外的字段与合同期望的完全匹配吗?我认为额外的字段应该无关紧要,因为非托管代码应该只访问前3个UInt32。
我担心的是结构版本仍然存在访问冲突问题但不常见。
任何帮助将不胜感激 感谢
[StructLayout(LayoutKind.Sequential)]
public class TStat
{
private UInt32 bitfield;
public UInt32 cbInQue;
public UInt32 cbOutQue;
private readonly UInt32 fCtsHoldMask = 0x00000001;
private readonly Int32 fCtsHoldShift = 0;
private readonly UInt32 fDsrHoldMask = 0x00000002;
private readonly Int32 fDsrHoldShift = 1;
private readonly UInt32 fRlsdHoldMask = 0x00000004;
private readonly Int32 fRlsdHoldShift = 2;
private readonly UInt32 fXoffHoldMask = 0x00000008;
private readonly Int32 fXoffHoldShift = 3;
private readonly UInt32 fXoffSentMask = 0x00000010;
private readonly Int32 fXoffSentShift = 4;
private readonly UInt32 fEofMask = 0x00000020;
private readonly Int32 fEofShift = 5;
private readonly UInt32 fTximMask = 0x00000040;
private readonly Int32 fTximShift = 6;
public bool fCtsHold
{
get { return ((bitfield & fCtsHoldMask) != 0); }
set { bitfield |= (Convert.ToUInt32(value) << fCtsHoldShift); }
}
public bool fDsrHold
{
get { return ((bitfield & fDsrHoldMask) != 0); }
set { bitfield |= (Convert.ToUInt32(value) << fDsrHoldShift); }
}
public bool fRlsdHold
{
get { return ((bitfield & fRlsdHoldMask) != 0); }
set { bitfield |= (Convert.ToUInt32(value) << fRlsdHoldShift); }
}
public bool fXoffHold
{
get { return ((bitfield & fXoffHoldMask) != 0); }
set { bitfield |= (Convert.ToUInt32(value) << fXoffHoldShift); }
}
public bool fXoffSent
{
get { return ((bitfield & fXoffSentMask) != 0); }
set { bitfield |= (Convert.ToUInt32(value) << fXoffSentShift); }
}
public bool fEof
{
get { return ((bitfield & fEofMask) != 0); }
set { bitfield |= (Convert.ToUInt32(value) << fEofShift); }
}
public bool fTxim
{
get { return ((bitfield & fTximMask) != 0); }
set { bitfield |= (Convert.ToUInt32(value) << fTximShift); }
}
}
[StructLayout(LayoutKind.Sequential)]
public struct TStat
{
private UInt32 bitfield;
public UInt32 cbInQue;
public UInt32 cbOutQue;
}
[DllImport("coredll.dll", EntryPoint = "ClearCommError", SetLastError = true)]
private static extern Boolean ClearCommError(IntPtr hPort, out UInt32 Errors, out TStat Stat);
PInvoke链接:
_http://www.pinvoke.net/default.aspx/kernel32.clearcommerror _http://www.pinvoke.net/default.aspx/Structures/COMSTAT.html
来自msdn的本地签名
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363180(v=vs.85).aspx
BOOL WINAPI ClearCommError(
_In_ HANDLE hFile,
_Out_opt_ LPDWORD lpErrors,
_Out_opt_ LPCOMSTAT lpStat
);
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363200(v=vs.85).aspx
typedef struct _COMSTAT {
DWORD fCtsHold :1;
DWORD fDsrHold :1;
DWORD fRlsdHold :1;
DWORD fXoffHold :1;
DWORD fXoffSent :1;
DWORD fEof :1;
DWORD fTxim :1;
DWORD fReserved :25;
DWORD cbInQue;
DWORD cbOutQue;
} COMSTAT, *LPCOMSTAT;
答案 0 :(得分:3)
在PInvoke签名中使用class
类型时,它实际上是作为指向值的指针传递的。 ref / out
也是如此。因此,当out TStat
是TStat
时,class
通过双指针传递值。只有原始参数类型为TStat**
才有意义。
我猜测原生签名实际上是TStat*
。这就是为struct
传递的原因,因为out struct
在PInvoke中具有与TStat*
相同的数据语义