用VB6,运行Notepad并且结束,应该怎么写?uExitCode应该如何获取?这是目前的代码(LastDllError返回值为6):Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
....
CurrentProcess = Shell("notepad.exe " & CurrentFile, vbNormalFocus)
MsgBox TerminateProcess(CurrentProcess, 0)

解决方案 »

  1.   

    shell返回的是pid你还要转换成句柄才能调用TerminateProcess
    用OpenProcess打开PID
      

  2.   

    用WMI吧,省时省力:Private Sub Command1_Click() '启动记事本
      Dim objP As Object, lngPID&, ret&  On Error Resume Next
      
      Set objP = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2:Win32_Process")
      ret = objP.Create("Notepad", Null, Null, lngPID)
      If lngPID = 0 Then MsgBox "发生错误,检查您有权限吗?"
    End SubPrivate Sub Command2_Click() '关闭记事本
      Dim objP As Object
      For Each objP In GetObject("winmgmts:{impersonationLevel=impersonate}!//.").ExecQuery("select * from Win32_Process where Name='notepad.exe'")
        objP.Terminate
      Next
      If Err <> 0 Then MsgBox "发生未知错误:" & Err.Description
    End Sub
      

  3.   

    找了一些资料,还是觉得用FindWindow+PostMessage比较方便,而且收获不少to toury:
    我对wmi不太了解,但是上面的代码显然没有给notepad设置参数Finally,thank you all!