locked
Dialog Handle and Application Handle RRS feed

  • Question

  • Hi,

    I'm beginner id VC++, would anyone tell me how to get current dialog's handle and current allication's handle?(with and without MFC)

    with thanks.

    Saturday, June 17, 2006 8:41 AM

Answers

  •  mohammad Mir mostafa wrote:
    Would anyone tell me how to get current dialog's handle and current allication's handle?(with and without MFC)

    Application Handle:

    You can retrieve it from WinMain() or DllMain() (it's the first parameter passed). Pass it as a parameter to every function that requires it.

    If you are the EXE, you can retrieve your handle using GetModuleHandle(NULL). Otherwise, save off your handle to a global variable.

    HINSTANCE ourhInst;

    BOOL WINAPI DllMain(HINSTANCE hinst, DWORD, LPVOID)
    {
      ourhInst = hinst;
      return 0;

    }

    From MFC: you can retrieve it from AfxGetInstanceHandle();.


    Dialog Handle:

    The dialog window handle is passed to your callback function. You can save it off from there.

    HWND dlghWnd;
    #pragma deprecated(dlghWnd)/* Wrap it into a class or pass it as a parameter, even if that means you'll need to alter thousands functions. */

    INT_PTR CALLBACK DialogProc(HWND hwnd, UINT, WPARAM, LPARAM)
    {
      /** NOT RECOMMENDED if you can have more than one dialog at a time.
      *
    Pass it as a parameter instead.
      **/
      dlghWnd = hwnd;
      ...

    }

    What's wrong with this code? What happens when you get two of these dialogs? What will be the value of dlghWnd then?

     

    In MFC, you can cast your CDialog-derived object into a HWND (doesn't need to be explicit).

    class YourDialog : public CDialog
    {...} yourDialogObj;

    HWND dlghWndMFC = yourDialogObj; // cast

    Reminder: Global variables are EVIL!

    Saturday, June 17, 2006 11:23 AM

All replies

  •  mohammad Mir mostafa wrote:
    Would anyone tell me how to get current dialog's handle and current allication's handle?(with and without MFC)

    Application Handle:

    You can retrieve it from WinMain() or DllMain() (it's the first parameter passed). Pass it as a parameter to every function that requires it.

    If you are the EXE, you can retrieve your handle using GetModuleHandle(NULL). Otherwise, save off your handle to a global variable.

    HINSTANCE ourhInst;

    BOOL WINAPI DllMain(HINSTANCE hinst, DWORD, LPVOID)
    {
      ourhInst = hinst;
      return 0;

    }

    From MFC: you can retrieve it from AfxGetInstanceHandle();.


    Dialog Handle:

    The dialog window handle is passed to your callback function. You can save it off from there.

    HWND dlghWnd;
    #pragma deprecated(dlghWnd)/* Wrap it into a class or pass it as a parameter, even if that means you'll need to alter thousands functions. */

    INT_PTR CALLBACK DialogProc(HWND hwnd, UINT, WPARAM, LPARAM)
    {
      /** NOT RECOMMENDED if you can have more than one dialog at a time.
      *
    Pass it as a parameter instead.
      **/
      dlghWnd = hwnd;
      ...

    }

    What's wrong with this code? What happens when you get two of these dialogs? What will be the value of dlghWnd then?

     

    In MFC, you can cast your CDialog-derived object into a HWND (doesn't need to be explicit).

    class YourDialog : public CDialog
    {...} yourDialogObj;

    HWND dlghWndMFC = yourDialogObj; // cast

    Reminder: Global variables are EVIL!

    Saturday, June 17, 2006 11:23 AM
  • Thank you very much.
    Saturday, June 17, 2006 11:52 AM