Win32-API-BUG OR WHAT?? 
Author Message
 Win32-API-BUG OR WHAT??

Microsoft Windows SDK documentation specifically states that you should not
use GetWindow in this manner. I quote from the Windows 3.1 SDK help for
EnumChildWindows (Win32 has similar):

'A child window that is moved or repositioned in the Z-order during the
enumeration process will be properly enumerated. The function will not
enumerate a child window that is destroyed before it is enumerated or that is
created during the enumeration process. These measures ensure that the
EnumChildWindows function is reliable even when the application causes odd
side effects, whereas an application that uses a GetWindow loop risks being
caught in an infinite loop or referencing a handle to a window that has been
destroyed.'

To loop through each child window of the dialog box, the recommended function
is EnumChildWindows. Try this (I have not tested this code):

BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
{
    GetClassName(hWnd, sz, 250);
    if (!stricmp(sz,"Edit"))
    {    *((HWND *)lParam) = NULL;
        return FALSE;    // stop enumeration
    }
    return TRUE;        // continue enumeration

Quote:
}

....

hwndEdit = NULL;
EnumChildWindows(hwndDlg, EnumChildProc, (LPARAM) &hwndEdit);

Of course, you could always find the ID of the control using Spy or somesuch
and use GetDlgItem(), which is a considerably simpler approach.

Quote:

> After getting the HWND of the dialogbox, i loop through the controls with
> GetWindow(hwnd,GW_CHILD) and GW_NEXT.

> Now the very STRANGE thing: Sometimes GetWindow(hwnd,G_CHILD) returns
> ZERO allthough the hwnd is the handle of the dialog (and the dialog DOES
> contain childs/controls). When i call GetLastError() the error code is
> ERROR_PROC_NOT_FOUND.!!!!!!!! This happens approximately every tenth time
> my function is called. ??????????????

> ....
> ....
> ....

> SetWindowText(hwndDlg,"found you"); //just to be sure i got the right hwnd

> dwError=0;
> hwndEdit=GetWindow(hwndDlg,GW_CHILD);

> if (hwndEdit==NULL){
>    dwError=GetLastError();
>    itoa(dwError,szError,10);// here i get 127 (ERROR_PROC_NOT_FOUND)!!!
>    MessageBox(NULL,szError,"error code",MB_OK);
>    return (FALSE);
> }

> //see if i got the editcontrol
> GetClassName(hwndEdit,sz,250);
> if (strcmp(sz,"Edit")){
>    do
>    {
>       hwndEdit=GetWindow(hwndEdit,GW_HWNDNEXT);
>       GetClassName(hwndEdit,sz,250);
>       if (strcmp(sz,"Edit"))
>          hwndEdit=NULL;
>    }
>    while (hwndEdit==NULL);
> }



Mon, 07 Aug 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Win32-API-BUG OR WHAT??

2. Calling ToAscii(Ex) twice affects TranslateMessage - Win32 API bug ?

3. ::PostMessage Win32 API equivalent??

4. .NET classes win32 api reference

5. Apply attributes to Win32 API

6. using a win32 api in managed code

7. Problem marshalling complex structures (invoking Win32 APIs)

8. How printing in landscape mode using win32 api?

9. How to call Win32 API??

10. Win32 API, MFC and ATL are DEAD

11. struct pointers, how? (importing functions from win32 api)

12. Win32 API Call

 

 
Powered by phpBB® Forum Software