尝试获取进程的Window句柄的异常消息

时间:2013-10-15 04:58:55

标签: .net vb.net winforms linq process

我正在尝试创建一个泛型函数来获取进程的主窗口句柄,给出进程句柄, 我想使用LINQ(避免使用FOR),但它会在'Where'clausule中抛出'拒绝访问'的例外。

我做错了什么?

Private Function Get_Process_MainWindowHandle(ByVal ProcessHandle As IntPtr) As IntPtr

    Try
        Return Process.GetProcesses _
               .Where(Function(p) p.Handle.Equals(ProcessHandle)) _
               .Cast(Of Process) _
               .First _
               .MainWindowHandle

    Catch ex As Exception
        MsgBox(ex.Message) ' ex Message: Access denied
        Return IntPtr.Zero
    End Try

End Function

用法:

Get_Process_MainWindowHandle(Process.GetProcessesByName("calc").First.Handle)
  

更新:

在我正在尝试做的另一个函数中,我得到了相同的异常,更重要的是,找不到主窗口句柄,我做错了什么?:

Private Sub Resize_Process_Window(ByVal ProcessHandle As IntPtr, _
                                  ByVal Weight As Integer, _
                                  ByVal Height As Integer)

    Dim rect As Rectangle = Nothing
    Dim procs As Process() = Nothing
    Dim hwnd As IntPtr = IntPtr.Zero

    Try
        ' Find the process
        procs = Process.GetProcesses

        For Each p As Process In procs
            Try
                If p.Handle.Equals(ProcessHandle) Then
                    MsgBox("Handle found!") ' Msgbox will never be displayed :(
                    hwnd = p.MainWindowHandle
                    Exit For
                End If
            Catch : End Try ' Catch for 'Denied acces' Win32Exception.
        Next

        Msgbox(hwnd) ' hwnd always is '0'   :(

        ' Store the Left, Right, Bottom and Top positions of Window, into the Rectangle.
        GetWindowRect(hwnd, rect)

        ' Resize the Main Window
        MoveWindow(hwnd, rect.Left, rect.Top, Weight, Height, True)

    Catch ex As InvalidOperationException
        'Throw New Exception("Process not found.")
        MessageBox.Show("Process not found.", Nothing, MessageBoxButtons.OK, MessageBoxIcon.Error)

    Finally
        rect = Nothing
        procs = Nothing
        hwnd = Nothing

    End Try

End Sub

用法:

Resize_Process_Window(Process.GetProcessesByName("notepad").First.Handle, 500, 500)

1 个答案:

答案 0 :(得分:2)

第一个问题:您正在尝试访问您没有必要权限的流程句柄。通常,这是系统和空闲进程,但取决于您正在运行的人,因为可能还有其他人。

您的GetProcesses() LINQ查询将尝试访问每个进程的句柄,以确定它们是否与包含条件匹配。如果要获取具有Handle访问权限的进程列表,可以执行以下操作。对不起,这是C#而不是VB,但你应该觉得转换它很简单:

        private void EnumeratePermittedProcesses()
        {

            Process[] Procs = Process.GetProcesses();

            foreach (Process P in Procs)
            {
                try
                {
                    IntPtr Ptr = P.Handle;
                    Debug.WriteLine("Processed Process " + P.ProcessName);
                }
                catch (Exception Ex)
                {
                    // Ignore forbidden processes so we can get a list of processes we do have access to
                }
            }
        }

其次,MSDN告诉我们进程句柄不是唯一的,所以你不应该使用.Equals来比较它们。请改用Process ID。此 是唯一的,并且具有额外的优势,即在请求Id属性时,您将不会收到拒绝访问错误。

以下是如何获取IntPtr,同时避免访问您无权访问的流程句柄:

        private IntPtr GetMainWindowHandle(int processId)
        {

            Process[] Procs = Process.GetProcesses();

            foreach (Process P in Procs)
            {              
                if (P.Id == processId )
                {                    
                    MessageBox.Show("Process Id Found!");

                    return P.MainWindowHandle;
                }                                       
            }

            return IntPtr.Zero;
        }

用法:

IntPtr P = GetMainWindowHandle(Process.GetProcessesByName("calc").First().Id);      

同样,你需要转换为VB(我有点生疏)。