I am trying to print something using C++. However, I am running into a strange bug that has left me clueless, I use the following code:
PRINTDLG pd;
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.Flags = PD_RETURNDEFAULT;
PrintDlg(&pd);
// Set landscape
DEVMODE* pDevMode = (DEVMODE*)GlobalLock(pd.hDevMode);
pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
pd.hwndOwner = mainWindow;
pd.Flags = PD_RETURNDC | PD_NOSELECTION;
GlobalUnlock(pd.hDevMode);
if (PrintDlg(&pd))
{
DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Test Print";
di.lpszOutput = (LPTSTR)NULL;
di.fwType = 0;
//start printing
StartDoc(pd.hDC, &di);
int a;
int b;
int c;
int d;
int e;
int f;
// int g; // Uncomment this -> CRASH
EndDoc(pd.hDC);
DeleteDC(pd.hDC);
}
else
{
cout << "Did not print: " << CommDlgExtendedError() << endl;
}
The moment I uncomment 'int g;' I get a: "Program received signal SIGSEGV, Segmentation fault." I use codeblocks and the mingw compiler, both up to date. What could be causing this?