Sending handle out of bool callback
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Sending handle out of bool callback

  1. #1
    Join Date
    Oct 2010
    Posts
    67

    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;
    }

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    Re: Sending handle out of bool callback

    The answer is: lParam.
    Best regards,
    Igor

  3. #3
    Join Date
    Oct 2010
    Posts
    67

    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?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Sending handle out of bool callback

    Quote Originally Posted by lucastonon View Post
    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

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    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?
    No you don't need any. And Paul kindly showed how the whole thing should look like.
    Best regards,
    Igor

  6. #6
    Join Date
    Oct 2010
    Posts
    67

    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
    Last edited by lucastonon; October 27th, 2010 at 06:01 PM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Sending handle out of bool callback

    Quote Originally Posted by lucastonon View Post
    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

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,024

    Re: Sending handle out of bool callback

    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
    ....
    Last edited by Igor Vartanov; October 28th, 2010 at 12:37 AM.
    Best regards,
    Igor

  9. #9
    Join Date
    Oct 2010
    Posts
    67

    Re: Sending handle out of bool callback

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center