0

In an old C++ application, an error will occur at the last line below when Data Execution Prevention is enabled:

IPrintDlg *pPrintdlg = NULL;
CLSID clsidPrintDlg;
HRESULT hResult = ::CLSIDFromProgID(OLESTR("MyCompany.PrintDlg"), &clsidPrintDlg);
if (SUCCEEDED(hResult))
    hResult = ::CoCreateInstance(clsidPrintDlg, NULL, CLSCTX_INPROC_SERVER, IID_IPrintDlg, (LPVOID*) &pPrintDlg);
HRESULT hr = pPrintDlg->Create((long)GetSafeHwnd());

IPrintDlg is an interface to a separate C++ component that displays a printer dialog, and it implements IDispatch. The error has Event ID: 1000.

The client does not want to turn DEP off so I'm trying to find a way to fix the code so it does not violate the DEP restriction. Any ideas?

MamaCasc
  • 55
  • 7
  • I don't see why it *would* violate the DEP restriction. Perhaps the MyCompany.PrintDlg class is doing something weird? – user253751 Feb 23 '23 at 00:17
  • 1
    Check the data type returned by `GetSafeHwnd` and the size of `long`. I think `GetSafeHwnd` returns a `HWND` (which is an opaque pointer) and `long` is 32 bits so on a 64-bit build the pointer value will be truncated. – Richard Critten Feb 23 '23 at 00:21
  • but also are you sure pPrintDlg isn't null? And is pPrintDlg the same as pPrintdlg? – user253751 Feb 23 '23 at 02:16
  • Thanks for your replies. This is a 32-bit component, and pPrintdlg was a typo (in this post, not the actual code). After the CoCreateInstance call, pPrintDlg is valued (not NULL). So I added debug messageboxes in the Create method of the MyCompany.PrintDlg class and it gets as far as calling DoModal() of atlwin.h before the error occurs. But only when DEP is enabled. – MamaCasc Feb 27 '23 at 14:53

0 Answers0