The following compiles without errors or warnings but doesn’t snag the IEFrame window, or at least I know it fails to change that window’s print orientation to landscape:
PRINTDLGEX pd;
pd.lStructSize = (DWORD)sizeof(PRINTDLG);
BOOL GetPrinterDeviceDefaults(struct tagPDA* pd);
BOOL bRet=AfxGetApp()->GetPrinterDeviceDefaults(&pd);
pd.hwndOwner = FindWindow("IEFrame", NULL);
LPDEVMODE pDevMode = (LPDEVMODE)::GlobalLock(&pd.hDevMode);
pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
::GlobalUnlock(&pd.hDevMode);
I have evidence the code may have acquired the hWnd of the IEFrame window because the results of MessageBox(0,(LPCSTR)pd.hwndOwner,"[header]",MB_OK); displays some text gobbledygook in the msg. instead of nothing.
Thanks for any help.
I’m not at all familiar with the PRINTDLG API you’re trying to use, but your MessageBox code is all wrong.
The
MessageBoxfunction takes two pointers to null-terminated C strings (either ANSI strings or Unicode strings, depending on whetherUNICODEis defined by the preprocessor). You’re passing in a window handle instead, which is not a null-terminated C string of any type — it’s just an opaque data value that only has meaning to the kernel. So whenMessageBoxtries to interpret the string, it gets a pointer to who-knows-where, and it will either crash with an access violation or read random gobbledygook out of memory until it runs into a stray NUL byte.Ordinarily the compiler will give you an error here, but by inserting the cast you’re telling “it’s OK, I know what I’m doing, I promise this is really an
LPCTSTR“, which masks the error.The correct fix to this is to print the handle value into a character array. For example: