'下面这个程序目的是要选中其他程序的listview的第一项
'问题见注释
Public Sub selectListView(hwndP As Long,index as integer)
'hwndp为listview控件句柄,index为要选中的项是第几项
On Error GoTo lbl    Dim processId As Long, processH As Long, pLv As Long
    Dim lv As LVITEM    If hwndP > 0 Then
        Call GetWindowThreadProcessId(hwndP, processId)
        If processId > 0 Then
            processH = OpenProcess(PROCESS_ALL_ACCESS, False, processId)
            debug.print processH 
            '这里每次打出来的processH都不一样不知对不对
            If processH > 0 Then
                pLv = VarPtr(VirtualAllocEx(processH, Null, LenB(lv), MEM_COMMIT, PAGE_READWRITE))
              
                If pLv > 0 Then
                    lv.state = LVIS_SELECTED
                    lv.stateMask = LVIS_SELECTED                    If WriteProcessMemory(processH, pLv, ByVal VarPtr(lv), LenB(lv), 0) <> 0 Then
'问题出在这句WriteProcessMemory打出来返回值为0
'用getlasterror打出来为0也没错,不知是那里错了请高手指点
                        
                        Call SendMessage(hwndP, LVM_SETITEMSTATE, index , pLv)
                
                    End If
                    MsgBox GetLastError
                    '上一句打出来出错是因为SendMessage出错,究其根本是WriteProcessMemory导致的
                    Call VirtualFreeEx(processH, pLv, 0, MEM_RELEASE)
                    Call CloseHandle(processH)
                    Call CloseHandle(processId)
                End If
            End If
            
        End If
        
    End If
    Exit Sub
lbl:
    MsgBox Err.Description
End Sub首先是希望高手帮我解决上面程序出错在哪里?
然后因为SendMessage(hwndP, LVM_SETITEMSTATE, index , pLv)
这条消息只有在listview选中一项后才能有校(用c++调试的),该怎么解决