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;
: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;