CALLBACK EnumChildProc

#include <windows.h>
#include <iostream>

using namespace std;

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) {

int id = GetDlgCtrlID(hwnd);
if(id == 5680){
cout << "id = " << id << endl;
cout << "hwnd_Child = " << hwnd << endl;
*(reinterpret_cast<HWND*>(lParam)) = hwnd;
return false;
}

return TRUE;

}


int main()
{

int handleid = 5680;
HWND hFoundWnd = NULL;
EnumChildWindows(HWND(0x9563215), EnumChildProc, reinterpret_cast<LPARAM>(&hFoundWnd));
if (hFoundWnd != NULL)
{
cout << "hawnnd = " << hFoundWnd<<std::endl ;
}

return(0);
}

how can passing this value int handle id = 5680; to reinterpret_cast<LPARAM> ?
i want use it inside CALLBACK EnumChildProc

if(id == handleid ){ // <--- like this
cout << "id = " << id << endl;
cout << "hwnd_Child = " << hwnd << endl;
*(reinterpret_cast<HWND*>(lParam)) = hwnd;
return false;
}
EnumChildWindows() follows the typical C callback idiom. You can pass the third parameter a pointer to anything you want and EnumChildWindows() will forward that data to the callback without caring what it is. For example, you could pass a pointer to an object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct data{
    int handle_id;
    HWND result;
};

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam){
    auto &d = *(data *)lParam;
    int id = GetDlgCtrlID(hwnd);
    if (id == d.handle_id){
        cout << "id = " << id << endl;
        cout << "hwnd_Child = " << hwnd << endl;
        d.result = hwnd;
        return false;
    }
    return true;
}

data d = {5680, nullptr};
EnumChildWindows(HWND(0x9563215), EnumChildProc, (LPARAM)&d);
Thanks so much its working
1
2
3
4
5
6
    data d = {5680, nullptr};
	EnumChildWindows(hWindowHandle, EnumChildProc, (LPARAM)&d);
    if (d.result != nullptr)
    {
      cout << "hawnnd = " <<  d.result<<std::endl ;
    }

Please how can use more than once
like this
1
2
3
4
5
6
    data d = {5680, nullptr};
	EnumChildWindows(hWindowHandle, EnumChildProc, (LPARAM)&d);
    if (d.result != nullptr)
    {
      cout << "hawnnd = " <<  d.result<<std::endl ;
    }

1
2
3
4
5
6
    data d = {5685, nullptr};
	EnumChildWindows(hWindowHandle, EnumChildProc, (LPARAM)&d);
    if (d.result != nullptr)
    {
      cout << "hawnnd = " <<  d.result<<std::endl ;
    }

1
2
3
4
5
6
    data d = {5688, nullptr};
	EnumChildWindows(hWindowHandle, EnumChildProc, (LPARAM)&d);
    if (d.result != nullptr)
    {
      cout << "hawnnd = " <<  d.result<<std::endl ;
    }
Do you mean you want to execute those three statements in order, but with less code?
1
2
3
4
5
6
7
8
9
10
int pids[] = { 5680, 5685, 5688 };
for (int pid : pids)
{
    data d = {pid, nullptr};
    EnumChildWindows(hWindowHandle, EnumChildProc, (LPARAM)&d);
    if (d.result != nullptr)
    {
      cout << "hawnnd = " <<  d.result<<std::endl ;
    }
}
Last edited on
Thanks for answer,
I mean that I will use them every time with changing the variable name. Is this true or false?
like this ,

1
2
3
4
5
6
7
8

    data handle1 = {4000, nullptr};
	EnumChildWindows(hWindowHandle, EnumChildProc, (LPARAM)&handle1);
    if (handle1.result != nullptr)
    {
      cout << "hawnnd = " <<  handle1.result<<std::endl ;
    }


other handle

1
2
3
4
5
6
7
8

    data handle2 = {5000, nullptr};
	EnumChildWindows(hWindowHandle, EnumChildProc, (LPARAM)&handle2);
    if (handle2.result != nullptr)
    {
      cout << "hawnnd = " <<  handle2.result<<std::endl ;
    }


and Please can you explain this line *(data *)lParam
Last edited on
Topic archived. No new replies allowed.