PChar StrPLCopy 上的无效指针操作

发布于 2024-11-08 12:59:32 字数 2423 浏览 0 评论 0原文

我使用 WinSock 2 编写了一些 TCP 服务器,并且我有一个捕获 FD_READ 事件的程序。在此过程中,我需要解析收到的消息。代码在这里:

procedure TfrmMain.WndProc_OnWSANetEvent(var Msg: TMessage);
Var
  iCurrThread, n : Integer;
  i : Integer;
  temp : PChar;
  len : Integer;
  params : PChar;
  username : PChar; password : PChar;
  ind : Integer;
  tempy : PChar;
  tempn : PChar;
begin
  case WSAGetSelectEvent(Msg.LParam) of
    FD_READ :
      while True do
      begin
        if (FreeRThreads.GetCount <> 0) then
          begin
            iCurrThread := FreeRThreads.Pop;
            if (ReadThreads[iCurrThread].Terminated) then
              begin
                ReadThreads[iCurrThread].SetFSocket(Msg.WParam);
                ReadThreads[iCurrThread].Execute;

                temp := ReadThreads[iCurrThread].GetFText;
                meLog.Lines.Add(temp);

                if (copy(temp,1,2)='AU') then
                  begin
                    StrPLCopy(params, PChar(copy(temp, 7, StrToInt( copy(temp, 3, 4) ) )), 16372);
                    ind := pos(' ', params);
                    StrPLCopy(username, PChar(copy(params, 1, ind-1)), 16372);
                    StrPLCopy(password, PChar(copy(params, ind + 1, StrLen(params))), 16372);

                    StrPLCopy(tempy, PChar('AU0001y'), 14);
                    StrPLCopy(tempn, PChar('AU0001n'), 14);

                    if (username=PChar('dizpers')) then
                      if (password=PChar('admin')) then
                        send(Msg.WParam, tempy^, 14, 0)
                      else
                        send(Msg.WParam, tempn^, 14, 0)
                    else
                      send(Msg.WParam, tempn^, 14, 0);

                    meLog.Lines.Add('USER = '+username);
                    meLog.Lines.Add('PASSWORD = '+password);
                  end;



                FreeRThreads.Push(iCurrThread);
                break;
              end;
          end;
      end;
    FD_CLOSE :
      begin
        n := CSocketsCount - 1;
        for i := 0 to n do
          if (ClientSockets[i] = Msg.WParam) then
            begin
              closesocket(ClientSockets[i]);
              FreeSockets.Push(i);
              break;
            end;
      end;
  end;
end;

在调试时,我在行上出现“访问冲突...写入地址...”

StrPLCopy(params, PChar(copy(temp, 7, StrToInt( copy(temp, 3, 4) ) )), 16372);

,请帮助我解决这个问题并理解为什么会发生。蒂亚!

I write some TCP-server usin WinSock 2 and I hava procedure which catch FD_READ event. In this procedure I need to parse recieved message. The code is here:

procedure TfrmMain.WndProc_OnWSANetEvent(var Msg: TMessage);
Var
  iCurrThread, n : Integer;
  i : Integer;
  temp : PChar;
  len : Integer;
  params : PChar;
  username : PChar; password : PChar;
  ind : Integer;
  tempy : PChar;
  tempn : PChar;
begin
  case WSAGetSelectEvent(Msg.LParam) of
    FD_READ :
      while True do
      begin
        if (FreeRThreads.GetCount <> 0) then
          begin
            iCurrThread := FreeRThreads.Pop;
            if (ReadThreads[iCurrThread].Terminated) then
              begin
                ReadThreads[iCurrThread].SetFSocket(Msg.WParam);
                ReadThreads[iCurrThread].Execute;

                temp := ReadThreads[iCurrThread].GetFText;
                meLog.Lines.Add(temp);

                if (copy(temp,1,2)='AU') then
                  begin
                    StrPLCopy(params, PChar(copy(temp, 7, StrToInt( copy(temp, 3, 4) ) )), 16372);
                    ind := pos(' ', params);
                    StrPLCopy(username, PChar(copy(params, 1, ind-1)), 16372);
                    StrPLCopy(password, PChar(copy(params, ind + 1, StrLen(params))), 16372);

                    StrPLCopy(tempy, PChar('AU0001y'), 14);
                    StrPLCopy(tempn, PChar('AU0001n'), 14);

                    if (username=PChar('dizpers')) then
                      if (password=PChar('admin')) then
                        send(Msg.WParam, tempy^, 14, 0)
                      else
                        send(Msg.WParam, tempn^, 14, 0)
                    else
                      send(Msg.WParam, tempn^, 14, 0);

                    meLog.Lines.Add('USER = '+username);
                    meLog.Lines.Add('PASSWORD = '+password);
                  end;



                FreeRThreads.Push(iCurrThread);
                break;
              end;
          end;
      end;
    FD_CLOSE :
      begin
        n := CSocketsCount - 1;
        for i := 0 to n do
          if (ClientSockets[i] = Msg.WParam) then
            begin
              closesocket(ClientSockets[i]);
              FreeSockets.Push(i);
              break;
            end;
      end;
  end;
end;

While debugging I have an "Acces Violation ... write of address ..." on the line

StrPLCopy(params, PChar(copy(temp, 7, StrToInt( copy(temp, 3, 4) ) )), 16372);

Plz, help me to solve this problem and understand why it's happened. TIA!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2024-11-15 12:59:32

在使用 之前,您必须为 params 变量分配内存StrPLCopy用户名密码tempytempn也是如此)

检查这个样本

Var
  Dest   : PChar;
  Source : PChar;
begin
    Source:='This is a buffer to copy';
    //alloc a buffer of 1024 bytes 
    GetMem(Dest,1024);
    try
      //copy 
      StrPLCopy(Dest, Source, Length(Source));
      //do something
      Writeln(Dest);
    finally
      //free the memory
      FreeMem(Dest);
    end;
end;

You must allocate memory for the params variable before to use StrPLCopy (the same goes for username,password,tempy,tempn)

check this sample

Var
  Dest   : PChar;
  Source : PChar;
begin
    Source:='This is a buffer to copy';
    //alloc a buffer of 1024 bytes 
    GetMem(Dest,1024);
    try
      //copy 
      StrPLCopy(Dest, Source, Length(Source));
      //do something
      Writeln(Dest);
    finally
      //free the memory
      FreeMem(Dest);
    end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文