Is there an easy way to change the default printer from mfc or Win32

Joined
Sep 22, 2006
Messages
1
Reaction score
0
I was wondering someone could tell me how to change the default printer on my Windows XP system. I have a MFC program that I created and a custom printer dialog derived from CPrintDialog that I added an Apply button to. I would like to be able to change the default printer to the one that I have selected when I click on the apply button. I saw several examples on the Microsoft website using the GetPrinterDeviceDefaults fuction and the hDevMode variable, but I was unable to get them to work. Any help would be appreciated. This is what I have though it does not work because the dmDeviceName is expecting a WCHAR instead of a CString. It may not work anyway, but at least it almost compiles. If someone could give me a couple of pointers on how to fix this, that would be great.

:dontknow:


CString printerName;


/////////Convert CString to WCHAR///////////////////////////////////////////
m_SelectPrinterCombo.GetWindowText(printerName);

LPWSTR lpszW_PrinterName = new WCHAR[255];

LPTSTR lpStr_PrinterName = printerName.GetBuffer(printerName.GetLength());
int nLen = MultiByteToWideChar(CP_ACP, 0, lpStr_PrinterName,-1,NULL,NULL);
MultiByteToWideChar(CP_ACP,0,lpStr_PrinterName,-1,lpszW_PrinterName,nLen);

////////////////////////////////////////////////////////////////////////////



PRINTDLG pd;
pd.lStructSize=(DWORD)sizeof(PRINTDLG);
BOOL bRet=theApp.GetPrinterDeviceDefaults(&pd);

if(bRet)
{
// protect memory handle with ::GlobalLock and ::GlobalUnlock
DEVMODE FAR *pDevMode=(DEVMODE FAR *)::GlobalLock(pd.hDevMode);
// set orientation to landscape

pDevMode->dmDeviceName = lpszW_PrinterName;
::GlobalUnlock(pd.hDevMode);
}

delete[] lpszW_PrinterName;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top