读书人

如下代码错在哪?出现乱码大家伙帮忙

发布时间: 2012-03-21 13:33:15 作者: rapoo

如下代码错在哪?出现乱码,大家伙帮忙看看

Delphi(Pascal) code
procedure TForm1.btn1Click(Sender: TObject);var PID,t:LongWord;    hwnd,hProcess:Thandle;    iItem:Integer;    lvitem:TLVITEM;    plvitem:^tLVITEM;    p_MyItemText,str_MyItemText:PChar;begin  str_MyItemText:='北京欢迎你';  hwnd:=FindWindow('#32770','Windows 任务管理器');  hwnd:=FindWindowEx(hwnd,0,'#32770',0);  hwnd:=FindWindowEx(hwnd,0,'SysListView32',0);  if hwnd=0 then    ShowMessage('[Windows 任务管理器] 尚未启动!')  else  begin    GetWindowThreadProcessId(hwnd, @PID);    hProcess:=OpenProcess(PROCESS_ALL_ACCESS,false,PID);    if hProcess=0 then      ShowMessage('获取进程句柄操作失败')    else    begin      plvitem:=VirtualAllocEx(hProcess, nil, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);      p_MyItemText:=PChar(VirtualAllocEx(hProcess, nil, 12, MEM_COMMIT, PAGE_READWRITE));      if (plvitem=nil) or (p_MyItemText='') then        ShowMessage('无法分配内存')      else      begin        ShowMessage('本演示程序将更改TaskManager中第6个项目中第1列的内容。');        iItem:=5;        lvitem.iSubItem:=0;        lvitem.pszText:=p_MyItemText;        WriteProcessMemory(hProcess, p_MyItemText, @str_MyItemText, 12, t);        WriteProcessMemory(hProcess, plvitem, @lvitem, sizeof(LVITEM), t);        SendMessage(hwnd, LVM_SETITEMTEXT, iItem, Integer(plvitem));        //出来的不是“北京欢迎你” 是“h“E ”问题在哪?谢谢         end;    end;  end;end;


[解决办法]
WriteProcessMemory(hProcess, p_MyItemText, @str_MyItemText, 12, t);
改成
WriteProcessMemory(hProcess, p_MyItemText, PChar(str_MyItemText), 12, t);试试,另外楼主用的DELPHI那个版本。

读书人网 >.NET

热点推荐