How can I select/change the printer for CPrintDialog in my programm ??? Please Help

I am new in Visual C++. I need help about selecting a printer for CPrintDialog.

I would like to select a special printer when I call the CPrintDialog-box for my programm.

The default printer is f.e. “Del AI0 Printer A920”

When I start the programm the CPrintDialog-box shows the default printer “Del AI0 Printer A920”
I would like to start the programm and the CPrintDialog-box should show f.e. “Epson EPL-3000”
and should use this printer for my programm.

I think you have to use DEVMODE …change the dmDeviceName…but I do not know how.
Please let me know

------------------------------------------------------------------
void CDrucken_Dlgbox_Drucker_einrichtenDlg::OnBnClickedButton1()
{

      CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);
      
   
      if(dlgPrint.DoModal()==IDOK)
      {
             
            //Verbinden des Drucker-DC mit einem CDC-Objekt
            CDC dcPrint;
            dcPrint.Attach(dlgPrint.GetPrinterDC());

            //--------- DOCINFO-Struktur erzeugen (wird für Druck benötigt)-----------
            DOCINFO Print;
            Print.cbSize = sizeof (Print);
            Print.lpszDocName = "Drucken im Dialog";
            Print.lpszOutput = NULL;
            Print.lpszDatatype = NULL;
            Print.fwType = NULL;
            //-----------------------------------------------------------------------


            //xxxxxxxxxxxxxxxxxxxx Ausdruck des Dokuments starten xxxxxxxxxxxxxxxxxxxx
            if(dcPrint.StartDoc(&Print)>=0)
            {
                          
                  //Eine Seite beginnen
                  dcPrint.StartPage();
           
                  
                  dcPrint.SetMapMode(MM_LOMETRIC);
                  int nLaenge = dcPrint.GetDeviceCaps(VERTSIZE);
                  int nBreite = dcPrint.GetDeviceCaps(HORZSIZE);
                  int nRechts = nBreite * 10 -50;
                  int nUnten = -nLaenge * 10 +50;
                  
                  //-------------------------------------------------------------------------
                  dcPrint.TextOut(100,-50, "Test....Test....Test...ect");
                  /-------------------------------------------------------------------------
                  
            
                  //Seite auswerfen
                  dcPrint.EndPage();
           
                     //Dokument schließem
                  dcPrint.EndDoc();

                  //Druckergerätekontext löschen
                  dcPrint.DeleteDC();
                  
            }
       //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
      }
}



Thomas StockbrueggerAsked:
Who is Participating?
 
lakshman_ceConnect With a Mentor Commented:
Use CreatePrinterDC() instead GetPrinterDC(). Like this

HDC hDCPrint = dlgPrint.CreatePrinterDC();
                  DOCINFO Print;
                  memset( &Print, 0, sizeof(DOCINFO) );
                  Print.cbSize = sizeof (DOCINFO);
                  Print.lpszDocName = "Drucken im Dialog";
                  Print.lpszOutput = (LPTSTR) NULL;
                  Print.lpszDatatype = (LPTSTR) NULL;
                  Print.fwType = 0;

                  if(StartDoc(hDCPrint,&Print)>=0)
                  {
                        StartPage(hDCPrint);                            
                        TextOut(hDCPrint,200,200,"My Test My Test.",25);
                        EndPage(hDCPrint);
                        EndDoc(hDCPrint);
                        DeleteDC(hDCPrint);
                  }
0
 
AlexFMCommented:
Try to call SetDefaultPrinter function before showing CPrintDialog.
0
 
lakshman_ceCommented:
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Thomas StockbrueggerAuthor Commented:
Try to call SetDefaultPrinter function before showing CPrintDialog.
Hi AlexFM,
please let me know how....I am very new in Visual C++
Thanks a lot.
Best regards
Thomas
0
 
Thomas StockbrueggerAuthor Commented:
Is there a very easy way?
0
 
lakshman_ceCommented:
Easy way is to do it in VB and launch the process from C++

Create a VB Console App and place the code
Dim net
net = CreateObject("WScript.Network")
net.SetDefaultPrinter("Printername")

Compile and launch the VB app from C++
using CreateProcess

Refer to this link for creating process
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/creating_processes.asp

Pass szCmdline = "C:\\yourexepath"
0
 
Thomas StockbrueggerAuthor Commented:
Hallo lakshman_ce,
thank you for your fast responce.
This is a short code but I think it is to difficult for me....to create a VB Console App
and using CreateProcess.

I have just a view month in Visual C++....so I am a beginner.
How does it works with DEVMODE???
-----------------------------------------------------------------------------------------------------------
CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);

      DEVMODE mode;
      memset(&mode,0,sizeof(mode));  //set DEVMODE to zero

  //   mode.dmDeviceName  = Printer Name
  //   how can I change here the Name of the Printer ???
  //   with DEVMODE I have a lot of members that I can change f.e. dmCopies ect.....
   
   
     if(dlgPrint.DoModal()==IDOK)
     {
           .....my code


  I am realy a beginner.....I will give you grade A 500 Points for a answer/code that I can understand....thanks
0
 
mahesh1402Commented:
Refer this may help :

Setting the default printer programmatically in an MFC application : http://www.codeproject.com/printing/printerselect.asp

Q126897: How to Change Default Printer Settings in an MFC Application :http://support.microsoft.com/kb/126897/

Q166129: Print Directly to a Non-Default Printer in MFC  : http://support.microsoft.com/support/kb/articles/Q166/1/29.asp?SD=MSDN

PrinterSettings - Changing, Storing and Loading Printer Settings : http://www.codeguru.com/cpp/w-p/printing/article.php/c2961/

-MAHESH
0
 
lakshman_ceCommented:
Copy these and it will work.

BOOL GetPrinterDevice(LPTSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode)
{
    // if NULL is passed, then assume we are setting app object's
    // devmode and devnames
    if (phDevMode == NULL || phDevNames == NULL)
        return FALSE;

    // Open printer
    HANDLE hPrinter;
    if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)
        return FALSE;

    // obtain PRINTER_INFO_2 structure and close printer
    DWORD dwBytesReturned, dwBytesNeeded;
    GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded);
    PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR,
        dwBytesNeeded);
    if (GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded,
       &dwBytesReturned) == 0) {
       GlobalFree(p2);
       ClosePrinter(hPrinter);
       return FALSE;
    }
    ClosePrinter(hPrinter);

    // Allocate a global handle for DEVMODE
    HGLOBAL  hDevMode = GlobalAlloc(GHND, sizeof(*p2->pDevMode) +
       p2->pDevMode->dmDriverExtra);
    ASSERT(hDevMode);
    DEVMODE* pDevMode = (DEVMODE*)GlobalLock(hDevMode);
    ASSERT(pDevMode);

    // copy DEVMODE data from PRINTER_INFO_2::pDevMode
    memcpy(pDevMode, p2->pDevMode, sizeof(*p2->pDevMode) +
       p2->pDevMode->dmDriverExtra);
    GlobalUnlock(hDevMode);

    // Compute size of DEVNAMES structure from PRINTER_INFO_2's data
    DWORD drvNameLen = lstrlen(p2->pDriverName)+1;  // driver name
    DWORD ptrNameLen = lstrlen(p2->pPrinterName)+1; // printer name
    DWORD porNameLen = lstrlen(p2->pPortName)+1;    // port name

    // Allocate a global handle big enough to hold DEVNAMES.
    HGLOBAL hDevNames = GlobalAlloc(GHND,
        sizeof(DEVNAMES) +
        (drvNameLen + ptrNameLen + porNameLen)*sizeof(TCHAR));
    ASSERT(hDevNames);
    DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames);
    ASSERT(pDevNames);

    // Copy the DEVNAMES information from PRINTER_INFO_2
    // tcOffset = TCHAR Offset into structure
    int tcOffset = sizeof(DEVNAMES)/sizeof(TCHAR);
    ASSERT(sizeof(DEVNAMES) == tcOffset*sizeof(TCHAR));

    pDevNames->wDriverOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pDriverName,
        drvNameLen*sizeof(TCHAR));
    tcOffset += drvNameLen;

    pDevNames->wDeviceOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPrinterName,
        ptrNameLen*sizeof(TCHAR));
    tcOffset += ptrNameLen;

    pDevNames->wOutputOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPortName,
        porNameLen*sizeof(TCHAR));
    pDevNames->wDefault = 0;

    GlobalUnlock(hDevNames);
    GlobalFree(p2);   // free PRINTER_INFO_2

    // set the new hDevMode and hDevNames
    *phDevMode = hDevMode;
    *phDevNames = hDevNames;
    return TRUE;
}

HANDLE CopyHandle(HANDLE h)
{
    // Return a handle to a copy of the data
    // that the passed handle was for.
    if (!h) return NULL;

    BYTE*   lpCopy;
    BYTE*   lp;
    HANDLE   hCopy;
    DWORD   dwLen = GlobalSize(h);
    if (hCopy = GlobalAlloc(GHND, dwLen))
    {
        lpCopy = (BYTE*)GlobalLock(hCopy);
        lp     = (BYTE*)GlobalLock(h);

        CopyMemory(lpCopy,lp,dwLen);

        GlobalUnlock(hCopy);
        GlobalUnlock(h);
    }

    return hCopy;
}


void CDrucken_Dlgbox_Drucker_einrichtenDlg::OnBnClickedButton1()
{
      // TODO: Add your command handler code here
      HANDLE hDevMode;
    HANDLE hDevNames;
      if (GetPrinterDevice(_T("Path to your printer"), &hDevNames, &hDevMode)) // See KB article Q166129
      {
            
            AfxGetApp()->SelectPrinter(hDevNames, hDevMode);

            CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);
            dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
            // Force the CPrintDialog to use our device mode & name.
            dlgPrint.m_pd.hDevMode  = CopyHandle(hDevMode);
            dlgPrint.m_pd.hDevNames = CopyHandle(hDevNames);
            if(dlgPrint.DoModal() == IDOK)
            {
                  GlobalFree(hDevMode);
                  GlobalFree(hDevNames);
                  hDevMode  = CopyHandle(dlgPrint.m_pd.hDevMode);  // Save new copies.
                  hDevNames = CopyHandle(dlgPrint.m_pd.hDevNames); // Save new copies.
            }
            GlobalFree(dlgPrint.m_pd.hDevMode);   // Because DoModal was called,
            GlobalFree(dlgPrint.m_pd.hDevNames);  // we need to free these.
      }
}
0
 
lakshman_ceCommented:
Also add to the top of your file
#include <winspool.h>
0
 
Thomas StockbrueggerAuthor Commented:
Hi lakshman ce,
thank you for your code. I will try this on Monday at my office computer.
I will let you know.
Have a great weekend
Best regards
Thomas
0
 
Thomas StockbrueggerAuthor Commented:
printerDlg.cpp

Hi lakshman ce,
thanks again for your code (even it is to difficult to understand for me)
I have add the following functions to printerDlg.cpp :
BOOL GetPrinterDevice(LPTSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode)

HANDLE CopyHandle(HANDLE h)
I also have encolsed #include <winspool.h>
at the top of my printerDlg.cpp Programm.

Here is the complete code:
//##############################################################################################
void CprinterDlg::OnBnClickedButton1()
{

      //-----------------------------------------------------------------------------------------
    // TODO: Add your command handler code here
     HANDLE hDevMode;
     HANDLE hDevNames;
     if (GetPrinterDevice(_T("Dell AIO Printer A920\\USB001"), &hDevNames, &hDevMode)) // See KB article Q166129
     {
         
            AfxGetApp()->SelectPrinter(hDevNames, hDevMode);

            CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);

            dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
            // Force the CPrintDialog to use our device mode & name.
            dlgPrint.m_pd.hDevMode  = CopyHandle(hDevMode);
            dlgPrint.m_pd.hDevNames = CopyHandle(hDevNames);
          //===============================================================================================
              if(dlgPrint.DoModal()==IDOK)
            {
               GlobalFree(hDevMode);
               GlobalFree(hDevNames);
               hDevMode  = CopyHandle(dlgPrint.m_pd.hDevMode);  // Save new copies.
               hDevNames = CopyHandle(dlgPrint.m_pd.hDevNames); // Save new copies.


               //Verbinden des Drucker-DC mit einem CDC-Objekt
                   CDC dcPrint;
                   dcPrint.Attach(dlgPrint.GetPrinterDC());

                   //--------- DOCINFO-Struktur erzeugen (wird für Druck benötigt)-----------
                   DOCINFO Print;
                   Print.cbSize = sizeof (Print);
                   Print.lpszDocName = "Drucken im Dialog";
                   Print.lpszOutput = NULL;
                   Print.lpszDatatype = NULL;
                   Print.fwType = NULL;
                   //-----------------------------------------------------------------------


                   //xxxxxxxxxxxxxxxxxxxx Ausdruck des Dokuments starten xxxxxxxxxxxxxxxxxxxx
                   if(dcPrint.StartDoc(&Print)>=0)
                    {
                    //Eine Seite beginnen
                          dcPrint.StartPage();
                               
                       //Masse des druckbaren Bereichs des Papiers ermittlen (in 0,1mm)
                         dcPrint.SetMapMode(MM_LOMETRIC);
                         int nLaenge = dcPrint.GetDeviceCaps(VERTSIZE);
                       int nBreite = dcPrint.GetDeviceCaps(HORZSIZE);
                       int nRechts = nBreite * 10 -50;
                       int nUnten = -nLaenge * 10 +50;
                        //----------------------------------------------------------------------
                         dcPrint.TextOut(100,-50, "My Test My Test….");
                  
                        //-----------------------------------------------------------------------
                              
                        //Seite auswerfen
                        dcPrint.EndPage();
           
                          //Dokument schließem
                        dcPrint.EndDoc();

                       //Druckergerätekontext löschen
                      dcPrint.DeleteDC();
                  
                  }
              //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
           }
            GlobalFree(dlgPrint.m_pd.hDevMode);   // Because DoModal was called,
          GlobalFree(dlgPrint.m_pd.hDevNames);  // we need to free these.
       }
}
//############################################################################################


//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bool CprinterDlg::GetPrinterDevice(LPSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode)
{
    // if NULL is passed, then assume we are setting app object's
    // devmode and devnames
    if (phDevMode == NULL || phDevNames == NULL)
        return FALSE;

    // Open printer
    HANDLE hPrinter;
    if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)
        return FALSE;

    // obtain PRINTER_INFO_2 structure and close printer
    DWORD dwBytesReturned, dwBytesNeeded;
    GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded);
    PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR,dwBytesNeeded);
    if (GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded,&dwBytesReturned) == 0)
      {
       GlobalFree(p2);
       ClosePrinter(hPrinter);
       return FALSE;
    }
     ClosePrinter(hPrinter);

    // Allocate a global handle for DEVMODE
    HGLOBAL  hDevMode = GlobalAlloc(GHND, sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra);
    ASSERT(hDevMode);
    DEVMODE* pDevMode = (DEVMODE*)GlobalLock(hDevMode);
    ASSERT(pDevMode);

    // copy DEVMODE data from PRINTER_INFO_2::pDevMode
    memcpy(pDevMode, p2->pDevMode, sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra);
    GlobalUnlock(hDevMode);

    // Compute size of DEVNAMES structure from PRINTER_INFO_2's data
    DWORD drvNameLen = lstrlen(p2->pDriverName)+1;  // driver name
    DWORD ptrNameLen = lstrlen(p2->pPrinterName)+1; // printer name
    DWORD porNameLen = lstrlen(p2->pPortName)+1;    // port name

    // Allocate a global handle big enough to hold DEVNAMES.
    HGLOBAL hDevNames = GlobalAlloc(GHND, sizeof(DEVNAMES) +(drvNameLen + ptrNameLen + porNameLen)*sizeof(TCHAR));
    ASSERT(hDevNames);
    DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames);
    ASSERT(pDevNames);

    // Copy the DEVNAMES information from PRINTER_INFO_2
    // tcOffset = TCHAR Offset into structure
    int tcOffset = sizeof(DEVNAMES)/sizeof(TCHAR);
    ASSERT(sizeof(DEVNAMES) == tcOffset*sizeof(TCHAR));

    pDevNames->wDriverOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pDriverName,drvNameLen*sizeof(TCHAR));
    tcOffset += drvNameLen;

    pDevNames->wDeviceOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPrinterName,ptrNameLen*sizeof(TCHAR));
    tcOffset += ptrNameLen;

    pDevNames->wOutputOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPortName,porNameLen*sizeof(TCHAR));
    pDevNames->wDefault = 0;

    GlobalUnlock(hDevNames);
    GlobalFree(p2);   // free PRINTER_INFO_2

    // set the new hDevMode and hDevNames
    *phDevMode = hDevMode;
    *phDevNames = hDevNames;
    return TRUE;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
HANDLE CprinterDlg::CopyHandle(HANDLE h)
{
      // Return a handle to a copy of the data
    // that the passed handle was for.
    if (!h) return NULL;

    BYTE*   lpCopy;
    BYTE*   lp;
    HANDLE   hCopy;
    DWORD   dwLen = GlobalSize(h);
    if (hCopy = GlobalAlloc(GHND, dwLen))
    {
        lpCopy = (BYTE*)GlobalLock(hCopy);
        lp     = (BYTE*)GlobalLock(h);

        CopyMemory(lpCopy,lp,dwLen);

        GlobalUnlock(hCopy);
        GlobalUnlock(h);
    }

    return hCopy;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

After debug I get no error. So I guess the code is fine.
The CPrintDialog shows the name of my printer....but it will not print.
 
if (GetPrinterDevice(_T("Path to your printer"), &hDevNames, &hDevMode)) // See KB article Q166129
---------------------------------------------------------------------------------------------------------------
-> this will not work   if (GetPrinterDevice(_T("Dell AIO Printer A920\\USB001"), &hDevNames, &hDevMode)) // See KB article Q166129


->this shows the printer but will not print (GetPrinterDevice(_T("Dell AIO Printer A920"), &hDevNames, &hDevMode)) // See KB article Q166129

My printer is Dell AIO A920  on USB001

Please be so kind to let me know what I have missed....want is wrong.

Thank you very much.
Best regards
Thomas





0
 
lakshman_ceCommented:
Try to debug by putting breakpoint in the GetPrinterDevice line.
What does this method return? If the printer is accessible from your system that should return true.
Are you passing it correctly. If the printer is on network server or machine, it need to be passed like this
GetPrinterDevice(_T("\\\\PRINTERSERVERNAME\\PRINTERNAME"), &hDevNames, &hDevMode)
If it is just local printer , you can pass only the name of the printer.
GetPrinterDevice(_T("PRINTERNAME"), &hDevNames, &hDevMode)
0
 
Thomas StockbrueggerAuthor Commented:
Hi lakshman_ce,
I have put a breakpoint at ->return TRUE:

GetPrinterDevice(_T("PRINTERNAME"), &hDevNames, &hDevMode)

// set the new hDevMode and hDevNames
                                  *phDevMode = hDevMode;
                                  *phDevNames = hDevNames;
 *debug breakpoint      return TRUE;                          
       TRUE is 1 and the debug stops here.

It is just a normal printer.
if (GetPrinterDevice(_T("Dell AIO Printer A920"), &hDevNames, &hDevMode))
The CPrintDialogbox  shows this name when I start...but no print.

I have also placed more breakpoints.....
     //xxxxxxxxxxxxxxxxxxxx Ausdruck des Dokuments starten xxxxxxxxxxxxxxxxxxxx
                 if(dcPrint.StartDoc(&Print)>=0)
                  {
                    //Eine Seite beginnen
                       dcPrint.StartPage();
                               
                     //Masse des druckbaren Bereichs des Papiers ermittlen (in 0,1mm)
                      dcPrint.SetMapMode(MM_LOMETRIC);
                      int nLaenge = dcPrint.GetDeviceCaps(VERTSIZE);
                     int nBreite = dcPrint.GetDeviceCaps(HORZSIZE);
                     int nRechts = nBreite * 10 -50;
                     int nUnten = -nLaenge * 10 +50;
                     //----------------------------------------------------------------------
                      dcPrint.TextOut(100,-50, "My Test My Test….");
                    //----------------------------------------------------------------------

I will not get to   ->dcPrint.StartPage(); when I set here a breakpoint
         
      if(dcPrint.StartDoc(&Print)>=0)

                  {  
 *debug breakpoint   dcPrint.StartPage();


Thank you so much for your help. Please let me know.
Best regards,
Thomas

                               

0
 
lakshman_ceCommented:
>>The CPrintDialogbox  shows this name when I start...but no print.

Means you have changed the default printer to your printer.

If I understand clearly the problem now is only with Printing. Did you click on the 'Print' button?

Try to see the print preview and find what happens
0
 
Thomas StockbrueggerAuthor Commented:
I have no "Print" button.... it is just a Dialogbox Application. I have clicked on OK from the CPrintDialogBox.
I don´t know how preview works...I think have no preview on an simple Dialogbox Appl.

Please take a look on my complete code above from start:
void CprinterDlg::OnBnClickedButton1()
{

Why do I don´t get to:  dcPrint.StartPage();

Please help. I very appreciate.
Best regards,
Thomas


0
 
Thomas StockbrueggerAuthor Commented:
Hi lakshman_ce,
Yes...this works TOP TOP TOP
Super......I can print !!!!! Thanks a lot !!!

I have just 2 more questions:
1)                  how can I select MM_LOMETRIC  so that I can count in 0,1mm
                      for my print

                      I tried something with hDCPrint but it will not work.
                     ----------------my old code--------------------------------
                      dcPrint.SetMapMode(MM_LOMETRIC);
                      int nLaenge = dcPrint.GetDeviceCaps(VERTSIZE);
                     int nBreite = dcPrint.GetDeviceCaps(HORZSIZE);
                     int nRechts = nBreite * 10 -50;
                     int nUnten = -nLaenge * 10 +50;
                     ----------------------------------------------------------------
                   
2) my programm works and prints on my computer that I use only for Visual C++.Net 2003
    Single computer, no network, single printer.
    We have a network in our office, and my programm should later run on this network.    

You wrote:
-> If the printer is on network server or machine, it need to be passed like this
GetPrinterDevice(_T("\\\\PRINTERSERVERNAME\\PRINTERNAME"), &hDevNames, &hDevMode)

I am sorry...(silly question) but I don´t know what is a PrinterServerName?
How can I find this name. I tried something before....but I always get a FALSE return at:

bool CprinterDlg::GetPrinterDevice(LPSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode)
{

// Open printer
    HANDLE hPrinter;
    if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)
        return FALSE;


Thanks again very much.
Without your help I would never come this far.
Have a great day.
Thank you.
Best regards,
Thomas

0
 
Thomas StockbrueggerAuthor Commented:
Hi lakshman_ce,

I found the answer to question 1)

                     if(StartDoc(hDCPrint,&Print)>=0)
                       {
                            StartPage(hDCPrint);                            

                      //MM_LOMETRIC   I can count in 0,1mm on my paper
                      //----------------------------------------------------------------
                     SetMapMode(hDCPrint, MM_LOMETRIC);
                      int nLaenge = GetDeviceCaps(hDCPrint, VERTSIZE);
                     int nBreite =   GetDeviceCaps(hDCPrint, HORZSIZE);
                     int nRechts = nBreite * 10 -50;
                     int nUnten = -nLaenge * 10 +50;

This works great....
But question 2) I don´t know.
Thank you.
Best regards
Thomas
tsp2002

0
 
Thomas StockbrueggerAuthor Commented:
Hi lakshman_ce,

I now found the answer to question 2)...happy....happy...

I have tried the single printer name (even it is a network printer) and it works just fine.
Thank you so much for your great help. I never could done this alone.
I will give your all 500 points with a grade A....I would give you more if I could.
Thanks again. Keep up the good work at www.experts-exchange.com

I will also post a new guestion for this programm. Now I would like to find out how I can select the paper size and more.
It would be great if you can help with that also. (500 points and grade A)

My question titel: How can I select the paper size CPrintDialog
Thank you and have a great day.
Best regards from
Thomas

0
 
lakshman_ceCommented:
PRINTERSERVERNAME is the name of the machine in your network that manages the printer PRINTERNAME.

So for e.g if my printer name is HP Laserjet 500 and it is connected to the network computer COMP01

you can pass \\\\COMP01\\HP Laserjet 500

If no machine manages the printer, you can just try the printer name.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.