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!