是否有一個託管的VB.net方式從HWND獲取進程ID,而不是使用此Windows API調用。如何使用託管VB.net代碼從HWND獲取進程ID?
Private Declare Auto Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, _
ByRef lpdwProcessId As Integer) As Integer
Sub GetProcessID()
'start the application
Dim xlApp As Object = CreateObject("Excel.Application")
'get the window handle
Dim xlHWND As Integer = xlApp.hwnd
'this will have the process ID after call to GetWindowThreadProcessId
Dim ProcIdXL As Integer = 0
'get the process ID
GetWindowThreadProcessId(xlHWND, ProcIdXL)
'get the process
Dim xproc As Process = Process.GetProcessById(ProcIdXL)
End Sub
簡短的回答,編號 –
這裏有一些事情引發紅旗。 1.爲什麼Office在提供PIA時使用CreateObject? 2.爲什麼你需要Excel的進程ID? –
@ Dan-o你的紅旗超出了這個問題的範圍。已經提出並回答了許多其他問題,以解決這些問題。我不想重複這些。 –