我想按名称查找职业ID,并等待其完成继续... 现在,我只是尝试按名称获取此pross,它对我不起作用...
我尝试使用此示例
但是我从GetProcesses方法得到0
例如在C#中 我写这段代码
wims
此代码工作正常,并返回ID 87068,我现在在调试时同时运行了这两个程序,因此我确定PulserTester程序已运行...
class Program
{
static void Main(string[] args)
{
int x = Process.GetProcessesByName("PulserTester").First().Id;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
简而言之,我尝试编写与此代码等效的代码 从C#到VB6
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcId As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, _
ByVal cb As Long, _
ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, _
ByVal hModule As Long, _
ByVal ModuleName As String, _
ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, _
ByRef lphModule As Long, _
ByVal cb As Long, _
ByRef cbNeeded As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Public Function GetProcesses(strProcess As String) As Long
Dim lProcCount As Long
Dim lProcPoss As Long
Dim lNum As Long
Dim ProcessIDs() As Long
Dim cbNeeded2 As Long
Dim NumElements2 As Long
Dim arrModules(1 To 200) As Long
Dim lRet As Long
Dim sModName As String
Dim hwndProcess As Long
Dim iCounter As Long
Const SIZE = 500
Const PROCESS_QUERY_INFORMATION = 1024
Const PROCESS_VM_READ = 16
Const PROCESS_ALL_ACCESS = &H1F0FFF
'need to get the array containing the process id's for each process object
lProcCount = 8
lProcPoss = 96
Do While lProcCount <= lProcPoss
lProcCount = lProcCount * 2
ReDim ProcessIDs(lProcCount / 4) As Long
lRet = EnumProcesses(ProcessIDs(1), lProcCount, lProcPoss)
Loop
lNum = lProcPoss / 4
For iCounter = 1 To lNum
'Get a handle to the Process
hwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessIDs(iCounter))
'Got a Process handle
If hwndProcess <> 0 Then
'Get an array of the module handles for the specified
'process
lRet = EnumProcessModules(hwndProcess, arrModules(1), 200, cbNeeded2)
'If the Module Array is retrieved, Get the ModuleFileName
If lRet <> 0 Then
sModName = Space(255)
lRet = GetModuleFileNameExA(hwndProcess, arrModules(1), sModName, SIZE)
'Form1.lstProcesses.AddItem ProcessIDs(iCounter) & vbTab & Left(sModName, lRet)
If UCase(Left(sModName, lRet)) = UCase(strProcess) Then
GetProcesses = GetProcesses + 1
End If
End If
End If
'Close the handle
lRet = CloseHandle(hwndProcess)
Next
End Function
Private Sub Form_Load()
ShellExecute Me.hWnd, "open", "C:\Users\hed-b\Desktop\PulserTester.appref-ms", vbNullString, "C:\", ByVal 1&
MsgBox GetProcesses("PulserTester")
End Sub
答案 0 :(得分:1)
txtnote.seltext
也可以编写。这是一个编辑控件,但它可能是文件流。
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long ' This process
th32DefaultHeapID As Long
th32ModuleID As Long ' Associated exe
cntThreads As Long
th32ParentProcessID As Long ' This process's parent process
pcPriClassBase As Long ' Base priority of process threads
dwFlags As Long
szExeFile As String * 260 ' MAX_PATH
End Type
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Const MAX_PATH = 260
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const SYNCHRONIZE = &H100000
'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const TH32CS_SNAPPROCESS = &H2&
Private Const hNull = 0
Sub mnuInsertProcessList_Click()
Dim f As Long, sname As String, PList As String, Ret As Long
Dim hSnap As Long, proc As PROCESSENTRY32
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnap = hNull Then Exit Sub
proc.dwSize = LenB(proc)
' Iterate through the processes
txtNote.SelText = "PID:int" & vbTab & "ParentPID:int" & vbTab & "ExeName:string" & vbCrLf
f = Process32First(hSnap, proc)
Do
sname = StrZToStr(proc.szExeFile)
txtNote.SelText = proc.th32ProcessID
txtNote.SelText = vbTab
txtNote.SelText = proc.th32ParentProcessID
txtNote.SelText = vbTab
txtNote.SelText = sname
txtNote.SelText = vbCrLf
f = Process32Next(hSnap, proc)
Loop While f = 1
Ret = CloseHandle(hSnap)
If Ret = 0 Then MsgBox Err.LastDllError
End Sub
Function StrZToStr(S As String) As String
StrZToStr = Left$(S, Len(S) - 1)
End Function