首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StrPLCopy上的PChar指针操作无效

StrPLCopy上的PChar指针操作无效
EN

Stack Overflow用户
提问于 2011-05-22 01:37:52
回答 1查看 1K关注 0票数 3

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

代码语言:javascript
复制
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;

在调试过程中,我遇到了一个“访问冲突...地址写入...”在线路上

代码语言:javascript
复制
StrPLCopy(params, PChar(copy(temp, 7, StrToInt( copy(temp, 3, 4) ) )), 16372);

请帮助我解决这个问题,并了解为什么会发生这种情况。蒂娅!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-22 01:47:14

在使用StrPLCopy之前,必须为params变量分配内存( usernamepasswordtempytempn也是如此)

检查此示例

代码语言:javascript
复制
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;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6083368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档