I have been trying to make a function that can send the output data or any text to a network printer, I read many articles/posts about it but nothing is working, have been looking for almost a week...
Whenever the program tries to use the printer function, it either hangs or gives the "failed to open file" message.

It would be really appreciated if someone can give me a hand here, oh and the OS I'm using is Windows XP. and I would prefer if I didn't have to use the MFC or the Windows API if possible, but if this is the only way then it's OK.

Here is a sample of the code that I tried:

#include <fstream.h>
#include <iostream.h>

int main()
  ofstream printer( "prn" ); //I also tried to replace prn with LPT1

  if (!printer) 
    cout << "Failed to open file\n";
    return 0;

  printer << "Hello world!\n";

  return 0;

If the network printer is selected as the default device, why not just print normally?

Printing (as far as I know using win32 api, which I know you don't want to use) involves the evokation of a standard dialogue box in which the use gets to choose the printing device as well as many other attributes.

Edited 6 Years Ago by Clinton Portis: n/a

Thanks for the quick reply.

I was just wondering if it was possible without the Win32 API, but right now I'm so desperate that I think I'm going with it.

So do you know anywhere to start? or is there a ready made code?
I'm going to keep googling till someone replies.

It's 1:12am and I am wicked tired, but here is a link that will give you an overview of what you are about to get into if you choose to pursue the win32 api route:


Other possible solutions may involve usb level programming (which I know just enough to be dangerous)

If you decide to go with win32 api, I'll break out the win32 bible tomorrow and come up with a simple code to print something.

Here is just a very simplified taste o' win32 printer basics.
(based on pg. #633 - 637, "Programming Windows, 5th Edition. Petzold.) Simplified and translasted to c++ by me.

This code is based on a flowchart describing typical print routine (pg. #639, Petzold):

  • SetAbortDoc
  • StartDoc
  • StartPage
  • Print 1 page
  • EndPage
  • User abort Printing?
  • Document Completed?
  • EndDoc

For simplicity I omitted the abort procedure:

int yChar = 0;
int iCharsPerLine = 0;
int iTotalPages = 0;

PTSTR ptstrBuffer;

//Set Doc Info 
di = {sizeof(DOCINFO), TEXT("My Print App, yo'.")};

//Set commong print dialog box attributes
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = hwnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL:
pd.hDC = NULL:
pd.nFromPage = 0;
pd.nToPage = 0;
pd.nMinPage = 0;
pd.nMaxPage = 0;
pd.nCopies = 1;
pd.hInstance = NULL;
pd.iCustData = 0;
pd.lpfnPrintHook = NULL;
pd.lpfnSetupHook = NULL;
pd.lpfnPrintTemplateName = NULL;
pd.lpfnSetupTemplateName = NULL:
pd.hPrintTemplate = NULL;
pd. hSetupTemplate = NULL;

//Display Standard Print Dialog 

//Calculate necessary metrics for file
GetTextMetrics(pd.hDC, &tm);
yChar = tm.tmHeight + tm. tmExternalLeading;
iCharsPerLine = GetDeviceCaps(pd.hDC, HORZRES) / tm.tmAveCharWidth;
iLnesPerPage = GetDeviceCaps(pd.hDC, VERTRES) / yChar;
iTotalPages = (iTotalLines + iLinesPerPage - 1) / iLinesPerPage;

//Allocate a buffer for each line of text
ptstrBuffer = new TCHAR[iCharsPerLine + 1];

// **Here is where the real printing work begins**

//Start Document
StartDoc(pd.hDC, &di);

//Print pages
for(int i=0; i<pd.nCopies; i++)
     //Start Page
     if(StartPage(pd.hDC) < 0)

     //Print lines of each page
     for(int i=0; i < iLinesPerPage; i++)
          iLineNum = iLinesPerPage * iPage + iLine;
          if(iLineNum > iTotalLines)

          static_cast<int*> pstrBuffer = iCharsPerLine;

          //Here is where the print command is sent to the printer.  
          //For this secenario, we are printing text from a multi-line edit control 
          //(like notepad for example) 
          //hwndEdit will be the ID handle for the edit control...  
          //Although not shown here, this handle was probably obtained using CreateWindow()
          TextOut(pd.hDC, 0, yChar * iLine, pstrBuffer, 
                  (int)SendMessage(hwndEdit, EM_GETLINE, 
                  (WPARAM) iLineNum, (LPARAM) ptstrBuffer));

          //End Page
          if(EndPage(pd.hDC) <  0)

delete ptstrBuffer;
DeleteDC(pd, hDC);

Edited 6 Years Ago by Clinton Portis: n/a

Hey Clinton, sorry couldn't reply earlier, just waned to thank you for your efforts, I really appreciate it.

Best Regards.

This article has been dead for over six months. Start a new discussion instead.