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
    Likes Received:
    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;
     
    Ace_12, Sep 22, 2006
    #1
    1. Advertisements

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,955
    Chris Taylor
    Feb 12, 2006
  2. cyberco
    Replies:
    0
    Views:
    4,113
    cyberco
    Nov 11, 2004
  3. ringos75
    Replies:
    0
    Views:
    1,191
    ringos75
    Apr 14, 2005
  4. yopwojtek

    Derive from MFC DLL to MFC APP

    yopwojtek, Aug 6, 2005, in forum: C++
    Replies:
    1
    Views:
    568
    Alf P. Steinbach
    Aug 6, 2005
  5. Ron Garret
    Replies:
    4
    Views:
    451
    Peter Otten
    Aug 21, 2007
  6. Anders
    Replies:
    3
    Views:
    232
    rpardee@gmail.com
    Dec 8, 2006
  7. Domenico Discepola

    How to change default printer in Windows using Perl

    Domenico Discepola, Jul 16, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    584
    Domenico Discepola
    Jul 16, 2004
  8. 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:
    398
    diavolo-verde@libero.it
    Jul 26, 2006
Loading...