Imports System.Runtime.InteropServices Public Class Form1 ' Windows APIの関数を定義 <DllImport("user32.dll", SetLastError:=True)> Private Shared Function EnumWindows(ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As IntPtr) As Boolean End Function <DllImport("user32.dll", SetLastError:=True)> Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As UInteger) As UInteger End Function <DllImport("user32.dll", SetLastError:=True)> Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer End Function <DllImport("user32.dll", SetLastError:=True)> Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer End Function Public Delegate Function EnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' 開きたいURLを指定 Dim url As String = "https://www.umayadia.com/" ' ブラウザを起動 'Dim browserProcess As Process = Process.Start("notepad.exe") Dim browserProcess As Process = Process.Start("chrome.exe", url) 'Dim browserProcess As Process = Process.Start("msedge.exe", url) 'Dim browserProcess As Process = Process.Start(url) ' プロセスIDを取得 Dim processId As UInteger = Convert.ToUInt32(browserProcess.Id) Console.WriteLine("processId: " & processId.ToString()) Try Console.WriteLine("MainWindowHandle: " & browserProcess.MainWindowHandle.ToString()) Catch ex As Exception Console.WriteLine(ex.Message) End Try browserProcess.WaitForExit(10000) Console.WriteLine("MainWindowHandle: " & browserProcess.MainWindowHandle.ToString()) ' ウィンドウハンドルを列挙してプロセスIDに対応するウィンドウハンドルを検索 EnumWindows(AddressOf EnumWindowsCallback, IntPtr.Zero) End Sub Private Function EnumWindowsCallback(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean Dim getProcessId As UInteger GetWindowThreadProcessId(hWnd, getProcessId) Dim length As Integer = GetWindowTextLength(hWnd) If length > 0 Then Dim windowTitle As New System.Text.StringBuilder(length + 1) GetWindowText(hWnd, windowTitle, windowTitle.Capacity) Console.WriteLine("ウィンドウハンドル: " & hWnd.ToString() & ", ウィンドウタイトル: " & windowTitle.ToString() & ", プロセスID: " & getProcessId.ToString()) End If Return True ' 次のウィンドウを列挙 End Function End Class