【问题标题】:Exception message trying to obtain the Window handle of a process尝试获取进程的窗口句柄的异常消息
【发布时间】:2013-10-15 04:58:54
【问题描述】:

我正在尝试制作一个通用函数来获取进程的主窗口句柄,给出进程句柄, 我想使用 LINQ(避免使用 FOR),但它会在“Where”子句中引发“拒绝访问”异常。

我做错了什么?

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)

【问题讨论】:

    标签: .net vb.net winforms linq process


    【解决方案1】:

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

    您的GetProcesses() LINQ 查询将尝试访问每个进程的句柄,以确定它们是否符合包含条件。如果您想获取您确实有权访问的进程列表,您可以执行以下操作。抱歉,这是 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 进行比较。请改用进程 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(我有点生疏了)。

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多