'大家试试下面的代码看看(为了测试方便请只开一个notepad.exe进程): Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As LongPrivate Const PROCESS_TERMINATE As Long = (&H1)
Private Const MAX_PATH As Integer = 260Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE As Long = &H8Private Type PROCESSENTRY32
    dwSize As Long
    cntusage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szExeFile As String * MAX_PATH
End TypePrivate Sub Command1_Click()
    Dim ProNum As Long, PidData() As Long, hProcess As Long, i As Long
    ProNum = FindPIDByName("notepad.exe", PidData()) '查找记事本的句柄,为了测试方便请只开一个notepad.exe进程
    If ProNum = 0 Then Exit Sub
    For i = 0 To ProNum - 1
        hProcess = OpenProcess(PROCESS_TERMINATE, False, PidData(i))  '获取进程句柄
        Print hProcess '打印句柄
    Next
End SubPrivate Function FindPIDByName(ProName As String, PidData() As Long) As Long '返回符合条件的进程ID,保存到数组PidData里
    Dim hProSnap As Long, hProID As Long, i As Long
    Dim ProInfo As PROCESSENTRY32
    ProName = LCase$(ProName)
    
    ProInfo.dwSize = Len(ProInfo)
    hProSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    hProID = Process32First(hProSnap, ProInfo)
    While hProID <> 0
        If LCase$(Left(ProInfo.szExeFile, InStr(1, ProInfo.szExeFile, vbNullChar) - 1)) = ProName Then
            ReDim Preserve PidData(i)
            PidData(i) = ProInfo.th32ProcessID
            i = i + 1
        End If
        hProID = Process32Next(hProSnap, ProInfo)
    Wend
    FindPIDByName = i
    CloseHandle hProSnap
End Function

解决方案 »

  1.   

    句柄是独一无二的,一个exe,进程编号PID 只有一个号, 但它可能有好几个子线程,每个子线程的句柄都不相同的.'添加 Command1 并请运行你的 计算器Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Const WM_SETTEXT = &HC
    Dim HwndVal&, ChildHwnd&
    Private Sub Command1_Click()
       HwndVal = FindWindow(vbNullString, "计算器")
       If HwndVal = 0 Then MsgBox "计算器没运行": Exit Sub
       Print "计算器的句柄是: " & CStr(HwndVal) & Space(3) & "进程号是: " & CStr(ProcIDFromWnd(HwndVal))
       SendMessage HwndVal, WM_SETTEXT, 0, ByVal "CBM666 的计算器"
       ChildHwnd = FindWindowEx(HwndVal, 0, "Static", vbNullString)
       If ChildHwnd <> 0 Then SendMessage ChildHwnd, WM_SETTEXT, 0, ByVal "123456789"
    End SubFunction ProcIDFromWnd(ByVal hwnd As Long) As Long
       GetWindowThreadProcessId hwnd, idProc
       ProcIDFromWnd = idProc
    End Function
      

  2.   

    应该可以吧,比如记事本,当起用 自动换行的时候与不用的时候,对应的就不是同一个窗口,当然可以对应不同的hwnd.