-1
我有XgTunWrap.encapsulation()獲取字節數組字節元帥
UInt16 encapsulation(out IntPtr pkt, UInt32 src_ip, UInt32 dst_ip, UInt16 sport, UInt16 dport, UInt16 pktLen);
功能(在C#C++ DLL)這需要字節陣列指針,並封裝字節數組的該字節陣列,並返回長度。我試圖用編組,但得到的內存訪問衝突錯誤得到封裝的字節數組。
在下面我的源代碼這給錯誤。有沒有什麼辦法讓封裝的字節數組?
int lenghtAr = Marshal.SizeOf(msg[0]) * msg.Length;
IntPtr unmPont = Marshal.AllocHGlobal(lenghtAr);
try
{
Marshal.Copy(msg, 0, unmPont, msg.Length);
len = XgTunWrap.encapsulation(out unmPont, m_pList.m_DeviceHoA.IpAddress, item.m_VptAliasHoA.IpAddress, (ushort)taPort, (ushort)taPort, (short)msg.Length);
res = new byte[len];
Marshal.Copy(unmPont, res, 0, (int)len);
}
catch (Exception ex)
{throw; }
finally
{
Marshal.FreeHGlobal(unmPont);
}
爲什麼'XgTunWrap.encapsulation(出unmPont'的_out_參數很奇怪,因爲你只是在分配/分配mem。 –
' - >>> catch(Exception ex){throw;} <<< - '是無用的。將它移除,因爲finally總是被調用。 –
也'Marshal.Copy(msg,0,unmPont , - > msg.Length < - );'**應**'Marshal.Copy(MSG,0,unmPont, - > lenghtAr < - );' –