Article ID: 131462
Article Last Modified on 11/21/2006
extern "C"
{
WNDPROC* g_lpfnDialogProc =NULL;
unsigned int CALLBACK DialogHook(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam)
{
static HWND hwndParentDialog;
LPOFNOTIFY lpofn;
int cbLength;
static LPTSTR lpsz;
static int LastLen;
switch (uMsg)
{
case WM_INITDIALOG:
// You need to use a copy of the OPENFILENAME struct used to
// create this dialog. You can store a pointer to the
// OPENFILENAME struct in the ofn.lCustData so you can
// retrieve it here in the lParam. Once you have it, you
// need to hang on to it. Using window properties provides a
// good thread safe solution to using a global variable.
if(!SetProp(GetParent(hwnd), "OFN", (void *) lParam))
MessageBox(NULL, "SET PRop Failed", "ERROR", MB_OK);
return (0);
case WM_COMMAND:
{
OutputDebugString("command\n");
}
break;
case WM_NOTIFY:
// The OFNOTIFY struct is passed in the lParam of this
// message.
lpofn = (LPOFNOTIFY) lParam;
switch (lpofn->hdr.code)
{
case CDN_SELCHANGE:
LPOPENFILENAME lpofn;
cbLength = CommDlg_OpenSave_GetSpec(GetParent(hwnd), NULL, 0);
cbLength += _MAX_PATH;
// The OFN struct is stored in a property of dialog window
lpofn = (LPOPENFILENAME) GetProp(GetParent(hwnd),
"OFN");
if (lpofn->nMaxFile < cbLength)
{
// Free the previously allocated buffer.
if(lpsz)
HeapFree(GetProcessHeap(),
0,
lpsz);
// Allocate a new buffer
lpsz = (LPTSTR) HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
cbLength);
if (lpsz)
{
lpofn->lpstrFile = lpsz;
lpofn->nMaxFile = cbLength;
}
}
break;
}
return (0);
case WM_DESTROY:
// Also need to free the property with the OPENFILENAME
// struct.
RemoveProp(GetParent(hwnd), "OFN");
return (0);
}
return (0);
}
Finally, this technique only works for 32-bit applications that are using the Explorer-type common dialogs.
Keywords: kbcmndlgfileo kbhowto KB131462