C# Process.MainWindowHandle always returns IntPtr Zero

后端 未结 3 907
情歌与酒
情歌与酒 2020-12-01 14:24

this is my code:

            using (Process game = Process.Start(new ProcessStartInfo() { 
        FileName=\"DatabaseCheck.exe\",
        RedirectStandardOu         


        
3条回答
  •  误落风尘
    2020-12-01 15:03

    A workaround is to enumerate through all top-level windows and examine their process ids until you find a match...

    
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr parentWindow, IntPtr previousChildWindow, string windowClass, string windowTitle);
    
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowThreadProcessId(IntPtr window, out int process);
    
        private IntPtr[] GetProcessWindows(int process) {
            IntPtr[] apRet = (new IntPtr[256]);
            int iCount = 0;
            IntPtr pLast = IntPtr.Zero;
            do {
                pLast = FindWindowEx(IntPtr.Zero, pLast, null, null);
                int iProcess_;
                GetWindowThreadProcessId(pLast, out iProcess_);
                if(iProcess_ == process) apRet[iCount++] = pLast;
            } while(pLast != IntPtr.Zero);
            System.Array.Resize(ref apRet, iCount);
            return apRet;
        }
    

提交回复
热议问题