if (ptlvitem = nil)or(p_MyItemText = nil) then begin
    MessageBox(0,'error memory!','error!',0);//不怎么乍执行到这里了,请各位前辈指点。procedure SetListView(hwnd:THandle);
    var
        lvitem:TLVITEM;
        ptlvitem:PLVITEM;
PID:LongInt;
hProcess:THandle;
p_MyItemText:pchar;//目标程序中用来存放TEXT的地址
        str_MyItemText:Array[0..12] of char;
    begin
        //strcpy(str_MyItemText,"300");
        if (hwnd=0) then begin
            MessageBox(0,'error hwnd','error!',0);
        end else begin
            GetWindowThreadProcessId(hwnd, @PID);
            hProcess:=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
            if (hProcess=0) then begin
                MessageBox(0,'error hProcess!','error!',0);
            end else begin
                ptlvitem:=PLVITEM(VirtualAllocEx(hProcess, nil, SizeOf(TLVITEM), MEM_COMMIT, PAGE_READWRITE));
p_MyItemText:=pchar(VirtualAllocEx(hProcess, nil, 12, MEM_COMMIT, PAGE_READWRITE));
                if (ptlvitem = nil)or(p_MyItemText = nil) then begin
                   MessageBox(0,'error memory!','error!',0);//不怎么乍执行到这里了,请各位前辈指点。
                end else begin
                   str_MyItemText:=Form1.Edit10.Text;
                   lvitem.iSubItem:=1;
   lvitem.pszText:=p_MyItemText;
                   WriteProcessMemory(hProcess, p_MyItemText, Pointer(@str_MyItemText), 1024, nil);
   WriteProcessMemory(hProcess, ptlvitem, Pointer(@lvitem), SizeOf(TLVITEM), nil);
                   SendMessage(hwnd, LVM_SETITEMTEXT, 4, LongInt(ptlvitem));
                end;
            end;
        end;
    end;  

解决方案 »

  1.   

    我又另开了一个Demo工程,也还是同样的问题。请各位前辈帮忙看看哦。谢谢!
    implementation
      function VirtualAllocEx(hProcess:THANDLE;
                              lpAddress:LPVOID;
                              dwSize:DWORD ;
                              flAllocationType : DWORD;
                              flProtect : DWORD):POINTER;
                              external 'kernel32' name 'VirtualAllocEx';
      function VirtualFreeEx(hProcess:THANDLE;
                             lpAddress:LPVOID;
                             dwSize:DWORD ;
                             dwFreeType : DWORD):WINBOOL;
                             external 'kernel32' name 'VirtualFreeEx';
    { TForm1 }procedure TForm1.Button1Click(Sender: TObject);
    var
       {HWND hwnd;
    int iItem=0;
    LVITEM lvitem, *plvitem;
    DWORD PID;
    HANDLE hProcess;
    char *p_MyItemText;//目标程序中用来存放TEXT的地址
    }
        hwnd:THandle;
        livtem:TLVITEM;
        pivtem:PLVITEM;
        PID:LongWord;
        hProcess:THandle;
        p_MyItemText:pchar;
        str_MyItemText:Array[0..12] of char;
    begin
        hwnd:=THandle(395556);
        if (hwnd=0) then begin
    MessageBox(0,'Error hwnd','Error',0);
        end else begin
            GetWindowThreadProcessId(hwnd, @PID);
            hProcess:=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
            if (hProcess=0) then begin
       MessageBox(0,'Error hProcess','Error',0);
            end else begin
               pivtem:=VirtualAllocEx(hProcess,nil, sizeof(TLVITEM), $1000{MEM_COMMIT}, PAGE_READWRITE);
       p_MyItemText:=VirtualAllocEx(hProcess, nil, 12, $1000{MEM_COMMIT}, PAGE_READWRITE);
               if(pivtem=nil) or (p_MyItemText=nil) then begin
                  MessageBox(0,'Error Memory','Error',0);
               end else begin           end;
            end;
        end;
    end;    
      

  2.   

    pivtem
    p_MyItemText
    依然为nil,为何呢,我这是从一个C代码转换过来的。
    C代码如下
    /*
     * Send LVM_SETITEMTEXT
     * 版权所有 (C) 2005 天津 赵春生
     * 2005.10.28
     * http://timw.yeah.net
     * http://timw.126.com
     * 本程序适用于:Win2KP+SP4[Windows TaskManager(5.0.2195.6620)]
     *  WinXP+SP1[Windows TaskManager]
     * 代码在Win2000P+SP4 + VC6+SP6测试通过
    */
    #include<windows.h>
    #include<commctrl.h>int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
     
    {

    HWND hwnd;
    int iItem=0;
    LVITEM lvitem, *plvitem;
    DWORD PID;
    HANDLE hProcess;
    char *p_MyItemText;//目标程序中用来存放TEXT的地址 //The pszText member is the pointer to a null-terminated
    //  string containing the new text; it can also be NULL.
    //以上信息是从API手册中获得的,故在本例中字符串长度不能>=12,
    //以确保字符串后有NULL。
    char str_MyItemText[12]={0};
    strcpy(str_MyItemText,"300");


    //hwnd=FindWindow("#32770","Windows 任务管理器");
    //hwnd=FindWindow("Fiber Trace",NULL);
    //hwnd=FindWindowEx(hwnd,0,"Fiber Trace",0);
    hwnd=(HWND)132668; if (!hwnd)

    MessageBox(NULL,"[Windows 任务管理器] 尚未启动!","错误!",NULL);

    else
    { GetWindowThreadProcessId(hwnd, &PID);

    hProcess=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
    if (!hProcess)

    MessageBox(NULL,"获取进程句柄操作失败!","错误!",NULL);

    else
    {
    plvitem=(LVITEM*)VirtualAllocEx(hProcess, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
    p_MyItemText=(char*)VirtualAllocEx(hProcess, NULL, 12, MEM_COMMIT, PAGE_READWRITE); if ((!plvitem)||(!p_MyItemText))

    MessageBox(NULL,"无法分配内存!","错误!",NULL);

    else
    {
    MessageBox(NULL,"本演示程序将更改TaskManager中第6个项目中第1列的内容。","提示",NULL);

    iItem=5;//5在这里是第六个(从零开始)
    lvitem.iSubItem=1;//同上
    lvitem.pszText=p_MyItemText;

    WriteProcessMemory(hProcess, p_MyItemText, &str_MyItemText, 12, NULL);
    WriteProcessMemory(hProcess, plvitem, &lvitem, sizeof(LVITEM), NULL); //向目标程序发送LVM_SETITEMTEXT消息
    SendMessage(hwnd, LVM_SETITEMTEXT, (WPARAM)iItem, (LPARAM)plvitem);

    }
    }
    }




    //释放内存
    CloseHandle(hwnd);
    CloseHandle(hProcess);
    VirtualFreeEx(hProcess, plvitem, 0, MEM_RELEASE);
    VirtualFreeEx(hProcess, p_MyItemText, 0, MEM_RELEASE);

    return 0;
    }
      

  3.   

    终于解决了,原来问题在于参数传递规则上。如下解决,只加上stdcall就可以了。
    function VirtualAllocEx(hProcess:HANDLE;
                              lpAddress:LPVOID;
                              dwSize:DWORD ;
                              flAllocationType : DWORD;
                              flProtect : DWORD):POINTER; stdcall;
                              external 'kernel32' name 'VirtualAllocEx';
      function VirtualFreeEx(hProcess:HANDLE;
                             lpAddress:LPVOID;
                             dwSize:DWORD ;
                             dwFreeType : DWORD):WINBOOL;stdcall;
                             external 'kernel32' name 'VirtualFreeEx';