Click here to Skip to main content
15,906,624 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Saving printer options Pin
PJ Arends6-May-05 5:47
professionalPJ Arends6-May-05 5:47 
GeneralRe: Saving printer options Pin
PJ Arends6-May-05 6:45
professionalPJ Arends6-May-05 6:45 
GeneralRe: Saving printer options Pin
crowbarcberg6-May-05 9:14
crowbarcberg6-May-05 9:14 
GeneralRe: Saving printer options Pin
PJ Arends6-May-05 10:59
professionalPJ Arends6-May-05 10:59 
GeneralRe: Saving printer options Pin
crowbarcberg6-May-05 11:15
crowbarcberg6-May-05 11:15 
GeneralRe: Saving printer options Pin
PJ Arends6-May-05 13:46
professionalPJ Arends6-May-05 13:46 
GeneralRe: Saving printer options Pin
crowbarcberg6-May-05 15:31
crowbarcberg6-May-05 15:31 
GeneralRe: Saving printer options Pin
crowbarcberg13-May-05 6:22
crowbarcberg13-May-05 6:22 
OK so the problem was saving the dmDriverExtra param, which contains the number of bytes of private driver-data that follow the structure. So adding dm->dmDriverExtra=0; before saving the DEVMODE structure fixed it. Here is the completed function, included for completeness:
<br />
BOOL CMainFrame::SetupPrinter(CString csPrinter, CPrintDialog* pDlg/*=NULL*/)<br />
{<br />
	// Pull the values out of the registry.<br />
	DEVMODE *hDevMode=NULL;<br />
	UINT nl;<br />
	CString csDeviceName, csPortName, csDriverName;<br />
	theApp.GetProfileBinary(csPrinter, "DevMode",  (LPBYTE*)&hDevMode, &nl);<br />
	csDeviceName=theApp.GetProfileString(csPrinter, "DeviceName", "");<br />
	csPortName=theApp.GetProfileString(csPrinter, "PortName", "");<br />
	csDriverName=theApp.GetProfileString(csPrinter, "DriverName", "");<br />
<br />
	//CPrintDialog dlg(TRUE);<br />
	BOOL bLoadedDialog=TRUE;<br />
	if(pDlg==NULL)<br />
	{<br />
		pDlg=new CPrintDialog(TRUE);<br />
		bLoadedDialog=FALSE;<br />
	}<br />
<br />
	LPDEVMODE pDM;<br />
	HGLOBAL hDEVNAMES;<br />
	HGLOBAL hDEVMODE;<br />
	BOOL bLoadedData=FALSE;<br />
	BOOL bCancel=FALSE;<br />
<br />
	if(!csDeviceName.IsEmpty()||!csPortName.IsEmpty()||!csDriverName.IsEmpty()||hDevMode!=NULL)<br />
	{<br />
		bLoadedData=TRUE;<br />
<br />
		// Setup the values for the print setup dialog.<br />
		pDM = hDevMode;<br />
		int size = sizeof(DEVMODE);<br />
		hDEVMODE = GlobalAlloc(GHND, size);<br />
		void *pV = GlobalLock(hDEVMODE);<br />
		memcpy(pV, pDM, size);<br />
		GlobalUnlock(pV);<br />
		delete pDM;<br />
		pDM = NULL;<br />
		<br />
		size = sizeof(DEVNAMES);<br />
		size += (csDeviceName.GetLength() + 1) * sizeof(TCHAR);<br />
		size += (csPortName.GetLength() + 1) * sizeof(TCHAR);<br />
		size += (csDriverName.GetLength() + 1) * sizeof(TCHAR);<br />
		<br />
		hDEVNAMES = GlobalAlloc(GHND, size);<br />
		LPDEVNAMES pDN = (LPDEVNAMES)GlobalLock(hDEVNAMES);<br />
		pDN->wDefault = 0;<br />
		pDN->wDriverOffset = sizeof(DEVNAMES);<br />
		pDN->wDeviceOffset = (WORD)(pDN->wDriverOffset + csDriverName.GetLength() + 1);<br />
		pDN->wOutputOffset = (WORD)(pDN->wDeviceOffset + csDeviceName.GetLength() + 1);<br />
		_tcsncpy((TCHAR *)((int)pDN + pDN->wDriverOffset), csDriverName, csDriverName.GetLength());<br />
		_tcsncpy((TCHAR *)((int)pDN + pDN->wDeviceOffset), csDeviceName, csDeviceName.GetLength());<br />
		_tcsncpy((TCHAR *)((int)pDN + pDN->wOutputOffset), csPortName, csPortName.GetLength());<br />
		GlobalUnlock(pDN);<br />
<br />
		pDlg->m_pd.hDevMode = hDEVMODE;<br />
		pDlg->m_pd.hDevNames = hDEVNAMES;<br />
	}<br />
	<br />
	if(pDlg->DoModal()==IDOK)<br />
	{<br />
		// Write the values to the registry.<br />
		DEVMODE *dm=pDlg->GetDevMode();<br />
		dm->dmDriverExtra=0; // Contains the number of bytes of private driver-data that follow this structure, so set it to 0 since we won't save it.<br />
		theApp.WriteProfileBinary(csPrinter, "DevMode",  (LPBYTE)dm, sizeof(DEVMODE));<br />
		theApp.WriteProfileString(csPrinter, "DeviceName",  (LPCTSTR) pDlg->GetDeviceName());<br />
		theApp.WriteProfileString(csPrinter, "PortName",  (LPCTSTR) pDlg->GetPortName());<br />
		theApp.WriteProfileString(csPrinter, "DriverName",  (LPCTSTR) pDlg->GetDriverName());<br />
<br />
		if(bLoadedData)<br />
			GlobalUnlock(pDM);<br />
	}<br />
	else<br />
	{<br />
		bCancel=TRUE;<br />
		if(bLoadedData)<br />
			GlobalFree(hDEVMODE);<br />
	}<br />
	<br />
	if(bLoadedData)<br />
		GlobalFree(hDEVNAMES);<br />
<br />
	if(!bLoadedDialog)<br />
		delete pDlg;<br />
<br />
	if(bCancel)<br />
		return FALSE;<br />
	else<br />
		return TRUE;<br />
}<br />

GeneralRe: Saving printer options Pin
crowbarcberg13-May-05 6:30
crowbarcberg13-May-05 6:30 
GeneralRe: Saving printer options Pin
PJ Arends13-May-05 9:11
professionalPJ Arends13-May-05 9:11 
GeneralVS 6.0 - service pack info Pin
john john mackey5-May-05 10:23
john john mackey5-May-05 10:23 
GeneralRe: VS 6.0 - service pack info Pin
Kevin McFarlane5-May-05 10:30
Kevin McFarlane5-May-05 10:30 
GeneralRe: VS 6.0 - service pack info Pin
Anonymous5-May-05 10:43
Anonymous5-May-05 10:43 
GeneralRe: VS 6.0 - service pack info Pin
Kevin McFarlane5-May-05 12:05
Kevin McFarlane5-May-05 12:05 
GeneralRe: VS 6.0 - service pack info Pin
David Crow6-May-05 4:25
David Crow6-May-05 4:25 
GeneralSound Led Pin
RickyC5-May-05 9:26
RickyC5-May-05 9:26 
GeneralRe: Sound Led Pin
Ravi Bhavnani5-May-05 10:07
professionalRavi Bhavnani5-May-05 10:07 
GeneralRe: Sound Led Pin
RickyC5-May-05 10:09
RickyC5-May-05 10:09 
QuestionHow to change the different view in the CSplitterWnd Pin
wwwht5-May-05 9:03
wwwht5-May-05 9:03 
AnswerRe: How to change the different view in the CSplitterWnd Pin
David Crow5-May-05 9:40
David Crow5-May-05 9:40 
GeneralUsing WinPCap Pin
sarmed5-May-05 8:04
sarmed5-May-05 8:04 
GeneralRe: Using WinPCap Pin
CodeBeetle26-May-05 6:50
CodeBeetle26-May-05 6:50 
Questiondo u know the differences between command.com and cmd.exe? Pin
includeh105-May-05 7:57
includeh105-May-05 7:57 
AnswerRe: do u know the differences between command.com and cmd.exe? Pin
David Crow5-May-05 8:06
David Crow5-May-05 8:06 
GeneralRe: do u know the differences between command.com and cmd.exe? Pin
includeh105-May-05 8:28
includeh105-May-05 8:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.