简体   繁体   中英

FindWindowEx is not running

I have a problem with the API windows function FindWindowEx, in fact I obtain the handle of the MainWindow of the process, but when i try to retrieve the handle of one of its buttons with FindWindowEx, it's not running. I had verified the window and its buttons with spy++, and everything runs well, even the handle of the mainwindow returned by my program matches the spy++'s one. i have tested the error code returned by "Marshal.GetLastWin32Error()", i always obtain error 1008. i have searched in many old posts dealing with my problem, but i hadn't find any solution for it. here is my code :

DllImport("user32.dll" , CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
 // .... 
 IntPtr hwnd = proc.MainWindowHandle;
        string str = proc.MainWindowTitle;
        Console.WriteLine("Main window Title : " + str);
        Console.WriteLine("Main window Handle : " + hwnd.ToString());
        //Get a handle for the "suivant" button
        IntPtr hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "suivant" );
        int error = Marshal.GetLastWin32Error() ; 

as Mr Hans Passant say, the class name is unpredictable, so the solution is to not specify the class name in the FindWindowEx function so to obtain all the controls handles in the mainwindow we can use :

do {
           IntPtr hwndchild = FindWindowEx(hwndparent, hwndchild , null, null) ;
        }while( hwndchild != IntPtr.Zero );

and we can find the handle of the "suivant" button in the main window :

IntPtr hwndchild = FindWindowEx(hwnd, hwndchild , null, "suivant") ;

Thank you for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM