如何使用process.start(程序)打开程序并返回程序的句柄
我使用process.start(程序),返回的Pid不对,使用getProcessByName(aa.ProcessName),查看到的Id,也不对,, Public aa As New Process
Public RunfilenName As String
Dim hwnd As IntPtr
aa.StartInfo.FileName = RunfilenName
aa.Start()
hwnd = aa.Id
Dim i As Int16 = 0
Do While 0 = hwnd And i < 500
Thread.Sleep(10)
hwnd = aa.Id
i += 1
Loop
'--------------------------------
Dim Myprocess() As Process = Process.GetProcessesByName(aa.ProcessName)
'----------------------------------
但是直接使用下面程序段就能正常列取进程的正常Id了,
ps = Process.GetProcesses()
With main.ListView1
For Each p In ps
Dim lvi As ListViewItem = New ListViewItem()
.Items.Add(p.ProcessName + " " + p.Id.ToString() + " " + p.BasePriority.ToString() + " " + Str(p.WorkingSet64 / 1000000.0))
Next
End With
如何处理
[解决办法]
Public Class Form1 Public aa As New Process Public RunfilenName As String = "D:\1.exe" Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim hwnd As IntPtr aa.StartInfo.FileName = RunfilenName aa.Start() hwnd = aa.Id 'Dim i As Int16 = 0 'Do While 0 = hwnd And i < 500 ' System.Threading.Thread.Sleep(10) ' hwnd = aa.Id ' i += 1 'Loop Me.Text = hwnd.ToString MsgBox(hwnd.ToString) '能取出值的。是你上面的代碼有問題。都不知道你上面的代碼用來做什麽。 End SubEnd Class
[解决办法]
你试下
dim p as Process=Process.start("文件")
dim pid=p.id