GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Sep-2006, 05:00
35183 35183 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
35183 is on a distinguished road

How to access the printer from a console?


I found the following code and used it to access the printer and nothing happens, I tryed it on Windows XP and Windows 98 and still nothing happens.
I've compiled the program using and Microsoft Visuall C++ 6.0 and DEV - C++. I want it to print to a usb printer.

I found the code on this page, http://personal.stevens.edu/~ckintala/CPE-360/UsingVC.doc. And when reading the what it says again I think I know what the problem is, although I don't know what I should do! eg "It assumes that the printer is capable of accepting plain text. For example, a PostScript printer will not print unless the data comes through a PostScript printer driver. The code above does not make use of any drivers."

CPP / C++ / C Code:
      
 #include <fstream.h>

       int main()

    { 
       ofstream print; // stream variable declaration 
       print.open("LPT1"); // open stream

       // Print Text (the character ‘\f’ will produce a form feed) 
       print << "This text will print on the printer.\f"; 
       print.close(); // close stream 
       return 0; 
    } 
Last edited by LuciWiz : 10-Sep-2006 at 09:02. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 10-Sep-2006, 13:50
MichaelS-R MichaelS-R is offline
Junior Member
 
Join Date: Apr 2006
Location: Berkshire, UK
Posts: 65
MichaelS-R is on a distinguished road

Re: How to access the printer from a console?


I don't play with Microsoft stuff at all but there must be a library to handle printing... Have you had a Google for it?
__________________
Michael

Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++).
  #3  
Old 11-Sep-2006, 10:46
35183 35183 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
35183 is on a distinguished road

Re: How to access the printer from a console?


I found the following code, it does exactly what I want it to do, it even accesses usb printers! Although there is one thing I want it to do, so my program is finished completely. As you can see it is a demo program, and it prints out the same message repeateadly. Although when I try to modify, it want print what i want, I get no output.

CPP / C++ / C Code:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>

static HDC     Prn_hDC;
static long    Prn_FontMetrix;
static long    Prn_LineCtr;
static long    Prn_PrinterOn;
static HFONT   Prn_hFont;
static HFONT   Prn_hFontOld;
static DOCINFO Prn_di;
static LOGFONT Prn_Lf;
static TEXTMETRIC Prn_tm;
static char    Prn_Text[2048];
static char    Prn_Buffer[2048];

// prototypes
char *BCX_TmpStr(size_t);
char *mid (char*, int, int=-1);
char *left (char*, int);
char *extract (char*, char*);
char *str (double);
int  PrinterOpen  (void);
void PrinterWrite (char*);
void EjectPage    (void);
void PrinterClose (void);


//using namespace std;

int main()
{
        int k;

        cout << "Make sure your printer is connected and on!\n";

        PrinterOpen();

        // use five lines as top margin
        for(k = 0; k < 5; k++)
        PrinterWrite(" ");

        // print next 50 lines of goofy text
        for(k = 1; k <= 50; k++)
        {
					 sprintf(Prn_Buffer,"%s %2d %s","Printing line number", k,"    Zzzzzz ...");
					 PrinterWrite(Prn_Buffer);
        }
        //EjectPage();  // optional to clear out the printer

        PrinterClose();

        cin.get();  // wait
        return 0;
}


// circular storage to minimize memory leaks
char *BCX_TmpStr (size_t Bites)
{
        static int StrCnt;
        static char *StrFunc[2048];
        StrCnt = (StrCnt + 1) & 2047;
        if (StrFunc[StrCnt]) free (StrFunc[StrCnt]);
        return StrFunc[StrCnt] = (char*)calloc(Bites+128,sizeof(char));
}


// needed for word wrap feature
char *left (char *S, int length)
{
        register int tmplen = strlen(S);
        char *strtmp = BCX_TmpStr(tmplen);
        strcpy (strtmp,S);
        if (length > tmplen)
        strtmp[tmplen] = 0;
        else
        strtmp[length] = 0;
        return strtmp;
}


// needed for word wrap feature
char *mid (char *S, int start, int length)
{
        register int tmplen = strlen(S);
        char *strtmp;
        if (start > tmplen || start < 1) return BCX_TmpStr(1);
        if (length < 0) length = tmplen - start + 1;
        strtmp = BCX_TmpStr(length);
        strncpy(strtmp,&S[start-1],length);
        strtmp[length] = 0;
        return strtmp;
}


char *extract (char *mane, char *match)
{
        register char *a;
        register char *strtmp = BCX_TmpStr(strlen(mane));
        strcpy(strtmp,mane);
        a = strstr(mane,match);
		  if (a) strtmp[a-mane] = 0;
        return strtmp;
}


char *str (double d)
{
        register char *strtmp = BCX_TmpStr(16);
        sprintf(strtmp,"% .15G",d);
        return strtmp;
}


//
// set all the printer options including font
//
int PrinterOpen (void)
{
        int  PointSize = 12;
        char zPrinter[2048];

        GetProfileString("WINDOWS","DEVICE","",zPrinter,127);
        // extract up to the comma
        strcpy(zPrinter, (char*)extract(zPrinter,","));
        strcpy(Prn_Text,"Printing ...");
        Prn_hDC = CreateDC("",zPrinter,"",0);
        if (!Prn_hDC) return 0;
        Prn_di.cbSize = sizeof(Prn_di);
        Prn_di.lpszDocName = Prn_Text;
        StartDoc(Prn_hDC,&Prn_di);
        StartPage(Prn_hDC);
        SetTextAlign(Prn_hDC,TA_BASELINE | TA_NOUPDATECP | TA_LEFT);
        SetBkMode(Prn_hDC,TRANSPARENT);
        //
        // Prn_Lf deals with the font, got to play with this, if you
        // want a smaller or larger font size!  Don't forget to change
        // max lines and max char/line in PrinterWrite() accordingly
        //
        Prn_Lf.lfHeight = PointSize*GetDeviceCaps(Prn_hDC,LOGPIXELSY)/72;
        Prn_Lf.lfWidth = 0;
        Prn_Lf.lfEscapement = 0;
        Prn_Lf.lfOrientation = 0;
        Prn_Lf.lfWeight = FW_NORMAL;
        Prn_Lf.lfItalic = 0;
        Prn_Lf.lfUnderline = 0;
        Prn_Lf.lfStrikeOut = 0;
        Prn_Lf.lfCharSet = ANSI_CHARSET;
        Prn_Lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
        Prn_Lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
        Prn_Lf.lfQuality = PROOF_QUALITY;
        Prn_Lf.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
		  strcpy(Prn_Lf.lfFaceName,/*TEXT*/("Courier New"));
        Prn_hFont = CreateFontIndirect(&Prn_Lf);
        Prn_hFontOld = (HFONT)SelectObject(Prn_hDC,Prn_hFont);
        GetTextMetrics(Prn_hDC,&Prn_tm);
        Prn_FontMetrix = Prn_Lf.lfHeight;
        Prn_PrinterOn = 1;
        return 1;
}


void PrinterWrite (char *TextIn)
{
        int LPP = 60;  // max line numbers depending on font size
        int CPL = 80;  // max char/line depending on font size
        char sTemp[2048] = {0};
        if (!Prn_PrinterOn)
        {
                MessageBox (GetActiveWindow(),"Printer Problem!","",0);
                return;
        }
        strcpy(sTemp,TextIn);
        while(1)
        {
                // split text if it exceeds max line characters
                if (strlen(sTemp) > CPL)
                {
                        strcpy(Prn_Text, (char*)left(sTemp,CPL));
                        strcpy(sTemp, (char*)mid(sTemp,CPL+1));
                }
                else
                {
                        strcpy(Prn_Text,sTemp);
                        *sTemp = 0;
                }
                Prn_LineCtr++;
                if (Prn_LineCtr >= LPP)
                {
                        // max lines exceeded, eject this page for next page
                        EndPage(Prn_hDC);
                        Prn_LineCtr = 0;
                        StartPage(Prn_hDC);
                }
                TextOut(Prn_hDC,20,Prn_FontMetrix*Prn_LineCtr,Prn_Text,strlen(Prn_Text));
                if(sTemp[0] == 0) break;
        }
}


void PrinterClose (void)
{
        if (!Prn_PrinterOn) return;
        SelectObject(Prn_hDC,Prn_hFontOld);
        DeleteObject(Prn_hFont);
        EndPage(Prn_hDC);
        EndDoc(Prn_hDC);
        DeleteDC(Prn_hDC);
        Prn_LineCtr = 0;
        Prn_PrinterOn = 0;
}


void EjectPage(void)
{
        EndPage(Prn_hDC);
        Prn_LineCtr = 0;
        StartPage(Prn_hDC);
}

Although there is one thing I want it to do, so my program is finished completely. As you can see it is a demo program, and it prints out the same message repeateadly. Although when I try to modify, it want print what i want. The only thing I whant it to do print this. If anyone can help to print out the following, it would be much appreciated!

printf(" \t\t\t\t\t\t\t\t\t\t\t\t\t DATE: %.2d/%.2d/%.4d",gtm->tm_mday,1+gtm->tm_mon,1900+gtm->tm_year);
printf(" TIME: %2d:%02d\n", gtm->tm_hour, gtm->tm_min);

cout<<******************************************** "<< endl;
cout << "\n\t\t ::: MY SYSTEM :::\n";
cout << "\n***************************************";
cout << "\n\n\n" << endl;

cout << " \t\t\t\t\t ***********************************"<< endl;
cout << " \t\t\t\t\t * PRINT RECORD *"<< endl;
cout << " \t\t\t\t\t ***********************************"<< endl;

cout << "\n";
cout << "\n \t\t\t\t\t First Name: "<< firstName;
cout << "\n\n \t\t\t\t\t Surname: " << lastName ;
cout << "\n\n \t\t\t\t\t Parents: "<< parents ;
cout << "\n\n \t\t\t\t\t Address: " << address;
cout << "\n\n \t\t\t\t\t Telephone: "<< telephone;
cout << "\n\n \t\t\t\t\t Mobile: "<< mobile << endl;

The following firstName,lastName,parents,address,telephone and Mobile are all chars!
Last edited by LuciWiz : 11-Sep-2006 at 13:53. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #4  
Old 12-Sep-2006, 08:14
35183 35183 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
35183 is on a distinguished road

Re: How to access the printer from a console?


I've managed to print out the output I want, although there is one simple thimg I cannot figure out, hout to print in a new line with sprintf
CPP / C++ / C Code:
	sprintf(Prn_Buffer,"%s%s%s%s%s%s%s", firstName, lastName, dob, parents, address, telephone, mobile);
I get the output that I want although i get in one continous line!


CPP / C++ / C Code:
			PrinterOpen();

			PrinterWrite("**********************************************************************************************");
			PrinterWrite(" ");
			PrinterWrite("              ::: MY SYSTEM :::");
			PrinterWrite(" ");
			PrinterWrite("**********************************************************************************************");
			PrinterWrite(" ");
      
			PrinterWrite("   **********************************************");
     		PrinterWrite("   *                 PRINT RECORD               *");
			PrinterWrite("   **********************************************");   

			PrinterWrite(" ");
			PrinterWrite("        First Name: ");
			PrinterWrite("        Surname: ") ; 
			PrinterWrite("        D.O.B.: ") ; 
			PrinterWrite("        Parent / Guardian: ") ; 
			PrinterWrite("        Address: ");                            
			PrinterWrite("        Telephone:"); 
			PrinterWrite("        Mobile:");		        
			
			sprintf(Prn_Buffer,"%s%s%s%s%s%s%s", firstName, lastName, dob, parents, address, telephone, mobile);
			PrinterWrite(Prn_Buffer);

			PrinterClose();

			cin.get();  // wait
  #5  
Old 12-Sep-2006, 08:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,619
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to access the printer from a console?


[quote=35183] to print in a new line with sprintf
CPP / C++ / C Code:
	sprintf(Prn_Buffer,"%s%s%s%s%s%s%s", firstName, lastName, dob, parents, address, telephone, mobile);


You could try the same way as you would with printf to a console:
CPP / C++ / C Code:
sprintf(Prn_Buffer,"%s%s%s%s%s%s%s\n", firstName, lastName, dob, parents, address, telephone, mobile);

Some output devices may require an explicit '\r', so if the above doesn't work, you could try:

CPP / C++ / C Code:
sprintf(Prn_Buffer,"%s%s%s%s%s%s%s\r\n", firstName, lastName, dob, parents, address, telephone, mobile);

Regards,

Dave
  #6  
Old 12-Sep-2006, 09:04
35183 35183 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
35183 is on a distinguished road

Re: How to access the printer from a console?


I've tried them both, instead of moving to a new lineI just get 'sqaures', when I print it out in hardcopy!
  #7  
Old 12-Sep-2006, 09:10
35183 35183 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
35183 is on a distinguished road

Re: How to access the printer from a console?


I figured it out, I just declared another PRN_buffer, and it prints them out perfectly!
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Printer Anywhere (Beta) Free Download efi Computer Hardware Forum 0 05-Sep-2006 06:09
need help with a console menu system BullBuchanan CPP / C++ Forum 6 20-Aug-2006 14:46
printer / font color / windows programming nicolas_qc MS Visual C++ / MFC Forum 0 03-Jan-2006 23:13
Bloodshed Dev C++ Project Options JdS CPP / C++ Forum 6 11-Nov-2005 17:23
Which Printer Do You Recommend ? onauc Computer Hardware Forum 3 26-Oct-2005 21:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.