I'm here with what might seem like a very basic question, but I cannot figure it out.

I am using EnumChildWindows winapi to identify a particular child window of google chrome browser.

I fail when trying to return the handle of that child window when found.

BOOL CALLBACK enum_wnd_proc(HWND h, LPARAM lp)
{
    wchar_t cls[1024] = {0};
    GetClassName(h, cls, 1024);

    if(std::wstring(cls) == L"Chrome_RenderWidgetHostHWND")
    {
        return false; // found, so end enumeration
    }

    return true;
}

Apart from using a global variable, I cannot figure a clean way to actually return the actual handle to the child window.

I'd appreciate any tips if someone has the time.

One way to solve my problem would be the ability to have the callback function as a class method so I could just do something like hChild = h; within the callback proc, I'm looking into that now, but without much success.

Sorry for bump, too late to edit.

Edited 3 Years Ago by Suzie999

How about using the LPARAM lp? (MSDN: An application-defined value to be passed to the callback function.)

Edited 3 Years Ago by mitrmkar: ?

How about using the LPARAM lp? (MSDN: An application-defined value to be passed to the callback function.)

Not a bad idea at all, but I don't know how I'd pass a typr of HWND as an argument that wants lparam.

Which incidentally is where I'm at now after having made enum_wnd_proc callback part of my class.
I tried like I thought would be fine hChild = h;
I thought with it being a class method might have been ok, but alas it is not, because the callback function needs to be static in order to work.

So now, I need a way to pass a reference to class instance to the callback function.

I tried like EnumChildWindows(child_handle,enum_wnd_proc,this);
and in callback function this->hChild = h;

None of which work. :(

Edited 3 Years Ago by Suzie999

Sorry, I thought CALLBACK only return bool, this not so?

Here I have new callback where I pass instance of clas to callback func, but no joy.

// For brevity this is class members
HWND hChild = NULL;
// this is called from class method
EnumChildWindows(child_handle,enum_wnd_proc,reinterpret_cast<LPARAM>(this));
if (hChild) {
     MessageBox(NULL,L"Success",L"EnumChildWindows",MB_OK); // does not occur

}
//###########################
BOOL CALLBACK Control::enum_wnd_proc(HWND h, LPARAM lp)
    {
        wchar_t cls[1024] = {0};
        GetClassName(h, cls, 1024);

        //MessageBox(NULL,cls,L"GetClassName",MB_OK);

        if(std::wstring(cls) == L"Chrome_RenderWidgetHostHWND\0")
        {
            MessageBox(NULL,L"Success",L"enum_wnd_proc",MB_OK); // occurs
            Control *tmp = reinterpret_cast<Control *>(lp);
            tmp->hChild = h;
            tmp->control_id = GetDlgCtrlID(h);
            return false;
        }

        return true;
    }

Can you please explain how to use return std::pair<bool,Handle>

(edit) no, not virtual function, I'm far to novice for that.

Edited 3 Years Ago by Suzie999

While pondering an answer, I wondered whether 'this' pointer is valid from within the constructor of my class.

Some searches resulted in people saying yes, and others no.

From withing the constuctor of my class, the callback function is being called, and trying to modify a public value in the class being constructed.

is this valid? could it be why the handle is not being changed in the class?

(edit) the answers respectively to those questions are No and Yes.

Seemingly solved.

(edit2) for reference I solved by removing the code mentioned from the constuctor into an Initialization method which needs to be called after the class construction.

Edited 3 Years Ago by Suzie999

This question has already been answered. Start a new discussion instead.