Sending handle out of bool callback
This callback function is called when I press a button, but I wanted to pass the handle out of it to use it in another button or something else, and use openprocess out of it. How?
Thanks.
Code:
static BOOL CALLBACK EnumWindowsProcb(HWND hwnd, LPARAM lParam)
{
if (hwnd)
{
DWORD pid;
char windowname[1024];
GetWindowText(hwnd, windowname, 100);
if (strstr(windowname, "Untitled - Notepad"))
{
DWORD pidx;
GetWindowThreadProcessId(hwnd, &pidx);
if(pidx == lParam)
{
HANDLE hProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_READ | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, (DWORD) pid);
}
}
return TRUE;
}
Re: Sending handle out of bool callback
Re: Sending handle out of bool callback
Ok but don't I need a function to send it out as I used EnumWindows() to send a lparam in?
Re: Sending handle out of bool callback
Quote:
Originally Posted by
lucastonon
This callback function is called when I press a button, but I wanted to pass the handle out of it to use it in another button or something else, and use openprocess out of it. How?
What is "EnumWindowsProcb"?
If you mean "EnumWindowsProc", then that's what the second parameter to EnumWindowsProc is for.
http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx
Code:
HANDLE myHandle = NULL;
EnumWindows(whatever, (LPARAM)&myHandle);
//....
Code:
static BOOL CALLBACK EnumWindowsProcb(HWND hwnd, LPARAM lParam)
{
//...
HANDLE *pHandle = (HANDLE*)lParam;
*pHandle = hProcess;
....
Regards,
Paul McKenzie
Re: Sending handle out of bool callback
Quote:
Ok but don't I need a function to send it out as I used EnumWindows() to send a lparam in?
No you don't need any. And Paul kindly showed how the whole thing should look like.
Re: Sending handle out of bool callback
I mean, I want to use hProcess out of the bool, the lparam I sent with enumwindows() is going to be used at the bool, but the variables I declare inside the bool can't be used out of it. Example:
Code:
EnumWindows(EnumWindowsProcb, NULL);
Code:
static BOOL CALLBACK EnumWindowsProcb(HWND hwnd, LPARAM lParam)
{
DWORD pidx;
GetWindowThreadProcessId(hwnd, &pidx);
}
Code:
void CAttachDlg::OnBnClickedButton1()
{
AfxMessageBox(pidx);
}
Then I'll have 'pidx' : undeclared identifier
Re: Sending handle out of bool callback
Quote:
Originally Posted by
lucastonon
I mean, I want to use hProcess out of the bool, the lparam I sent with enumwindows() is going to be used at the bool, but the variables I declare inside the bool can't be used out of it.
I have no idea what you are trying to say.
If you are saying that you want that variable set, then pass the entire object as the second parameter and store the value there in the EnumWindowProcb function.
Code:
class CAttachDlg
{
HANDLE pidx;
//...
public:
void SetMyHandle(HANDLE h) { pidx = h; }
//...
};
Code:
CAttachDlg dlg;
EnumWindows(EnumWindowsProcb, &dlg);
Code:
static BOOL CALLBACK EnumWindowsProcb(HWND hwnd, LPARAM lParam)
{
DWORD pidx;
GetWindowThreadProcessId(hwnd, &pidx);
CAttachDlg* pDlg = (CAttachDlg*)lParam;
pDlg->SetMyHandle( pidx );
//...
}
Code:
void CAttachDlg::OnBnClickedButton1()
{
AfxMessageBox(pidx);
}
Regards,
Paul McKenzie
Re: Sending handle out of bool callback
Quote:
but the variables I declare inside the bool can't be used out of it.
I doubt you are interesting in accessing precisely a variable of the callback context. Your interest is the value of the variable, and this is where lParam gives you the pointer to the variable from original function context:
From Paul's example:
Code:
static BOOL CALLBACK EnumWindowsProcb(HWND hwnd, LPARAM lParam)
{
//...
HANDLE *pHandle = (HANDLE*)lParam;
*pHandle = hProcess; // here you copy the value of local variable hProcess
....
Re: Sending handle out of bool callback