Function 从窗口标题获取进程ID,反之亦然

Function 从窗口标题获取进程ID,反之亦然,function,vbscript,pid,Function,Vbscript,Pid,我有两个函数可以在给定窗口标题时获取应用程序的PID,在给定PID时获取应用程序的窗口标题。这些函数工作得很好,但我觉得它们比其他函数慢一些,因为这两个函数都写入外部文本文件以获得CMD输出 职能: 获取给定窗口标题时的PID: 函数IntProcessID(StrWindowTitle) Dim FSO、WshShell、任务、TA、TList、TN、结果 如果StrComp(StrWindowTitle,“,1)0,则 出错时继续下一步 设置FSO=WScript.CreateObject(

我有两个函数可以在给定窗口标题时获取应用程序的PID,在给定PID时获取应用程序的窗口标题。这些函数工作得很好,但我觉得它们比其他函数慢一些,因为这两个函数都写入外部文本文件以获得CMD输出

职能:

获取给定窗口标题时的PID:

函数IntProcessID(StrWindowTitle)
Dim FSO、WshShell、任务、TA、TList、TN、结果
如果StrComp(StrWindowTitle,“,1)0,则
出错时继续下一步
设置FSO=WScript.CreateObject(“Scripting.FileSystemObject”)
设置WshShell=WScript.CreateObject(“WScript.Shell”)
TN=FSO.GetTempName
FSO.GetFolder(WshShell.ExpandEnvironmentStrings(“%LOCALAPPDATA%”)+“/Temp”).CreateTextFile(TN).关闭
Set TList=FSO.GetFile(WshShell.ExpandEnvironmentStrings(“%LOCALAPPDATA%”)+“/Temp/”+TN)
结果=WshShell.Run(“CMD/C TASKLIST/V/FI”“WINDOWTITLE eq”+StrWindowTitle+”/FO CSV>“+TList.Path+”,0,True)
如果结果=0,则
TList.Attributes=22
Tasks=Split(FSO.OpenTextFile(TList.Path).ReadAll,VbCrLf)
对于任务中的每个任务
TA=拆分(修剪(任务),“,”)
如果UBound(TA)>0,则
如果StrComp(CStr(TA(UBound(TA)+0)),“”“+StrWindowTitle+“”,1)=0,则
IntProcessID=Replace(CStr(TA(LBound(TA)+1)),“”,“”)
如果结束
如果结束
下一个
其他的
MsgBox“任务列表命令失败,错误”+CStr(结果)
如果结束
FSO.DeleteFile TList.Path,True
如果错误为0,则
MsgBox“删除文件失败,错误”+CStr(错误)+“-”+CStr(错误描述)
呃,明白了
如果结束
其他的
MsgBox“窗口标题不能为空”
如果结束
端函数
获取给定PID时的窗口标题:

函数StrWindowTitle(IntProcessID)
Dim FSO、WshShell、任务、TA、TList、TN、结果
如果StrComp(CStr(IntProcessID),“”,1)0,则
出错时继续下一步
设置FSO=WScript.CreateObject(“Scripting.FileSystemObject”)
设置WshShell=WScript.CreateObject(“WScript.Shell”)
TN=FSO.GetTempName
FSO.GetFolder(WshShell.ExpandEnvironmentStrings(“%LOCALAPPDATA%”)+“/Temp”).CreateTextFile(TN).关闭
Set TList=FSO.GetFile(WshShell.ExpandEnvironmentStrings(“%LOCALAPPDATA%”)+“/Temp/”+TN)
结果=WshShell.Run(“CMD/C TASKLIST/V/FI”“PID eq”+CStr(IntProcessID)+”“/FO CSV>”“+TList.Path+”,0,True)
如果结果=0,则
TList.Attributes=22
Task=Split(FSO.OpenTextFile(TList.Path).ReadAll,VbCrLf)
TA=拆分(修剪(任务(1)),“,”)
如果UBound(TA)>1,则
如果StrComp(CStr(TA(LBound(TA)+1)),“”+CStr(IntProcessID)+“”,1)=0,则
StrWindowTitle=Replace(CStr(TA(UBound(TA)+0)),“”,“”)
如果结束
如果结束
其他的
MsgBox“任务列表命令失败,错误”+CStr(结果)
如果结束
FSO.DeleteFile TList.Path,True
如果错误为0,则
MsgBox“删除文件失败,错误”+CStr(错误)+“-”+CStr(错误描述)
呃,明白了
如果结束
其他的
MsgBox“进程ID不能为空”
如果结束
端函数
在这里,我使用带有
Run
方法的外部文本文件来隐藏控制台窗口。
请告诉我如何使这两个功能更简短、更快速、更可靠和更稳定,而不会在运行时出现任何CMD/控制台窗口闪烁。

你知道你可以只问一个问题,而不是删除并发布一个新问题,你昨天问了同样的问题。删除这个问题意味着任何相关的评论都会丢失,任何编辑的历史记录也会丢失。不过,编辑仍然是更好的方法。如果一个问题不清楚,也没有引起很多注意,试着修改它,使其更清楚,并提供更多信息。提供一个好的开始。删除和重新发布问题和在论坛上乱发帖子一样是一种坏习惯。不要这样做。现在,既然这样,你为什么认为你需要提高性能呢?我承认,一般来说,写入临时文件并不是最好的方法,然而,VBScript也不是最好的语言。对我来说,你所拥有的看起来是一个相当可靠的方法,所以只要它工作得足够好,我就不会改变它。另一个旁注(也只是为了将来使用),因为你基本上是在问如何改进工作代码:对于这类问题,代码审查可能是一个更好的地方。因此,最重要的是让代码首先工作。
Function IntProcessID(StrWindowTitle)
  Dim FSO, WshShell, Tasks, TA, TList, TN, Result

  If StrComp(StrWindowTitle, "", 1) <> 0 Then
    On Error Resume Next
    Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
    Set WshShell = WScript.CreateObject("WScript.Shell")
    TN = FSO.GetTempName
    FSO.GetFolder(WshShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") + "/Temp").CreateTextFile(TN).Close
    Set TList = FSO.GetFile(WshShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") + "/Temp/" + TN)

    Result = WshShell.Run("CMD /C TASKLIST /V /FI ""WINDOWTITLE eq " + StrWindowTitle + """ /FO CSV > """ + TList.Path + """", 0, True)

    If Result = 0 Then
      TList.Attributes = 22
      Tasks = Split(FSO.OpenTextFile(TList.Path).ReadAll, VbCrLf)

      For Each Task In Tasks
        TA = Split(Trim(Task), ",")
        If UBound(TA) > 0 Then
          If StrComp(CStr(TA(UBound(TA) + 0)), """" + StrWindowTitle + """", 1) = 0 Then
            IntProcessID = Replace(CStr(TA(LBound(TA) + 1)), """", "")
          End If
        End If
      Next
    Else
      MsgBox "TASKLIST command failed, Error " + CStr(Result)
    End If

    FSO.DeleteFile TList.Path, True

    If Err <> 0 Then
      MsgBox "DeleteFile failed, Error " + CStr(Err) + " - " + CStr(Err.Description)
      Err.Clear
    End If
  Else
    MsgBox "The Window Title cannot be empty"
  End If
End Function
Function StrWindowTitle(IntProcessID)
  Dim FSO, WshShell, Task, TA, TList, TN, Result

  If StrComp(CStr(IntProcessID), "", 1) <> 0 Then
    On Error Resume Next
    Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
    Set WshShell = WScript.CreateObject("WScript.Shell")
    TN = FSO.GetTempName
    FSO.GetFolder(WshShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") + "/Temp").CreateTextFile(TN).Close
    Set TList = FSO.GetFile(WshShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") + "/Temp/" + TN)

    Result = WshShell.Run("CMD /C TASKLIST /V /FI ""PID eq " + CStr(IntProcessID) + """ /FO CSV > """ + TList.Path + """", 0, True)

    If Result = 0 Then
      TList.Attributes = 22
      Task = Split(FSO.OpenTextFile(TList.Path).ReadAll, VbCrLf)
      TA = Split(Trim(Task(1)), ",")
      If UBound(TA) > 1 Then
        If StrComp(CStr(TA(LBound(TA) + 1)), """" + CStr(IntProcessID) + """", 1) = 0 Then
          StrWindowTitle = Replace(CStr(TA(UBound(TA) + 0)), """", "")
        End If
      End If
    Else
      MsgBox "TASKLIST command failed, Error " + CStr(Result)
    End If

    FSO.DeleteFile TList.Path, True

    If Err <> 0 Then
      MsgBox "DeleteFile failed, Error " + CStr(Err) + " - " + CStr(Err.Description)
      Err.Clear
    End If
  Else
    MsgBox "The Process ID cannot be empty"
  End If
End Function