c# 调用dll 报尝试读取或写入受保护的内存 急 在线等
delphi 中的声明dll
function Read_Guest_Card(Port, ReaderType, SectorNo: Byte; HotelPwd: PChar;
ClientID: Longword;var CardNo, GuestSN, GuestIdx: Integer; DoorID, SuitDoor,
PubDoor, BeginTime, EndTime: PChar): Integer; stdcall; far; external 'btlock73L.dll';
delphi 部分调用代码
var
tmpCardNo, tmpGuestSN, tmpGuestIdx, tmpGuestSex,
rt: Integer;
tmpDoorID, tmpSuitDoor, tmpPubDoor,
tmpBeginTime, tmpEndTime, tmpLiftData, tmpGuestName, tmpData: PChar;
tmpClientID: Longword;
begin
rt := 255
tmpClientID := StrToInt64(edtClientID.Text);
GetMem(tmpDoorID, 7);
tmpDoorID[6] := #0;
GetMem(tmpSuitDoor, 5);
tmpSuitDoor[4] := #0;
GetMem(tmpPubDoor, 9);
tmpPubDoor[8] := #0;
GetMem(tmpBeginTime, 13);
tmpBeginTime[12] := #0;
GetMem(tmpEndTime, 13);
tmpEndTime[12] := #0;
GetMem(tmpGuestName, 9);
tmpGuestName[8] := #0;
GetMem(tmpData, 33);
tmpData[32] :=#0
if StrToInt(Edit7.Text) - StrToInt(Edit6.Text) + 1 > 16 then
begin
GetMem(tmpLiftData, (StrToInt(Edit7.Text) - StrToInt(Edit6.Text) -15) * 2 + 1);
tmpLiftData[(StrToInt(Edit7.Text) - StrToInt(Edit6.Text) -15) * 2] := #0;
end else
begin
GetMem(tmpLiftData, (StrToInt(Edit7.Text) - StrToInt(Edit6.Text) + 1) * 2 + 1);
tmpLiftData[(StrToInt(Edit7.Text) - StrToInt(Edit6.Text) + 1) * 2] := #0;
end;
if (ckbCardContent.Checked = True) and (ckbLiftContent.Checked = False) and
(ckbPowerSwitchContent.Checked = False) and (ckbData.Checked = False) then
begin
rt := Read_Guest_Card(cmbDP.ItemIndex + 1, cmbDM.ItemIndex + 1, StrToInt(edtSectorNo.Text),
PChar(edtSP.Text), tmpClientID, tmpCardNo, tmpGuestSN, tmpGuestIdx, @tmpDoorID[0], @tmpSuitDoor[0],
@tmpPubDoor[0], @tmpBeginTime[0], @tmpEndTime[0]);
Delphi 运行成功
c#中的声明dll
[DllImport("btlock73L.dll", EntryPoint = "Read_Guest_Card", CharSet = CharSet.Ansi)]
public static extern int Read_Guest_Card(byte Prot, byte ReaderType, byte SectorNo, StringBuilder HotelPwd, long ClientID, ref int CardNo, ref int GuestSN, ref int GuestIdx, StringBuilder DoorID, StringBuilder SuitDoor, StringBuilder PubDoor, StringBuilder BeginTime, StringBuilder EndTime);
c#调用主要代码
StringBuilder m_SI, m_NC, m_MC, m_BT, m_ET, m_LC;
int m_GI = 0, m_GN = 0, m_CN = 0;
m_SI = new StringBuilder(6);
m_NC = new StringBuilder(4);
m_MC = new StringBuilder(8);
m_BT = new StringBuilder(12);
m_ET = new StringBuilder(12);
m_LC = new StringBuilder(6);
m_SI.Append”523323”);
rt = Read_Guest_Card(1, 5, 0, m_SI, Convert.ToInt64(SP), ref m_CN, ref m_GI, ref m_GN, m_LC, m_NC, m_MC, m_BT, m_ET);
int rt;
上述报错报尝试读取或写入受保护的内存
急 在线等