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

Discussion in 'C Programming' started by Ace_12, Sep 22, 2006.

  1. Ace_12

    Ace_12

    Joined:
    Sep 22, 2006
    Messages:
    1
    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;
    Ace_12, Sep 22, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThunderMusic
    Replies:
    1
    Views:
    4,419
    Chris Taylor
    Feb 12, 2006
  2. Scott McPhillips
    Replies:
    0
    Views:
    368
    Scott McPhillips
    Jul 5, 2003
  3. ringos75
    Replies:
    0
    Views:
    940
    ringos75
    Apr 14, 2005
  4. yopwojtek

    Derive from MFC DLL to MFC APP

    yopwojtek, Aug 6, 2005, in forum: C++
    Replies:
    1
    Views:
    373
    Alf P. Steinbach
    Aug 6, 2005
  5. diavolo-verde@libero.it

    Win32::Printer -> override the window printer window

    diavolo-verde@libero.it, Jul 26, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    132
    diavolo-verde@libero.it
    Jul 26, 2006
Loading...

Share This Page