Thread: Printer Works Unexpectedly

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    53

    Printer Works Unexpectedly

    Hi,
    I found some code on the net which showed how to print text on a page. I modified it for my use, but i encountered a problem: when the function printed sth, it wrote some weird characters at the end of each line. I can't solve the problem, i need help. Here is the code:

    Code:
    bool PrintTextPRN(char* outputText)
    {
    	PRINTDLG pd;
    	ZeroMemory(&pd, sizeof(PRINTDLG));
    
    	// populate it
    	pd.lStructSize	= sizeof(PRINTDLG);
    	pd.hwndOwner	= NULL;
    	pd.hDevMode		= NULL;
    	pd.hDevNames	= NULL;
    	pd.Flags		= PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
    	pd.nCopies		= 1;
    	pd.nFromPage	= 0xFFFF;
    	pd.nToPage		= 0xFFFF;
    	pd.nMinPage		= 1;
    	pd.nMaxPage		= 0xFFFF;
    
    	if (!PrintDlg(&pd)) {
    		MessageBox (NULL, "A printer error has occurred.\r\n\r\nPlease check the print cable and\r\nmake sure the printer is turned on.", "Print Error!", MB_OK);
    	}
    
    	// declare a DOCINFO structure and populate it
    	DOCINFO di;
    
    	di.cbSize		= sizeof(DOCINFO);
    	di.lpszDocName	= "Glowdot Port Scanner";
    	di.lpszOutput	= (LPTSTR)NULL;
    	di.fwType		= 0;
    
    
    	
    	int leftMargin  = 1 * GetDeviceCaps(pd.hDC, LOGPIXELSX);
    	int rightMargin = 1 * GetDeviceCaps(pd.hDC, LOGPIXELSX);
    	int topMargin = 1 * GetDeviceCaps(pd.hDC, LOGPIXELSY);
    	int bottomMargin = 1 * GetDeviceCaps(pd.hDC, LOGPIXELSY);
    
    	int leftOffset   = leftMargin  - GetDeviceCaps(pd.hDC, PHYSICALOFFSETX);
    	int rightOffset  = rightMargin - (GetDeviceCaps(pd.hDC, PHYSICALWIDTH) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETX)- GetDeviceCaps(pd.hDC, HORZRES));
    
    	int topOffset   = topMargin  - GetDeviceCaps(pd.hDC, PHYSICALOFFSETY);
    	int bottomOffset  = bottomMargin - (GetDeviceCaps(pd.hDC, PHYSICALHEIGHT) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETY)- GetDeviceCaps(pd.hDC, VERTRES));
    
    	int prnWidth  = GetDeviceCaps(pd.hDC, HORZRES) - (leftOffset + rightOffset);
    	int prnHeight = GetDeviceCaps(pd.hDC, VERTRES) - (topOffset  + bottomOffset);
    
    	TEXTMETRIC tm;
    HFONT hfont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
    int yChar;
    
    SetMapMode(pd.hDC, MM_TEXT);
    SelectObject(pd.hDC, hfont);
    
    
    GetTextMetrics(pd.hDC, &tm);
    yChar = tm.tmHeight;
    int xChar=tm.tmMaxCharWidth;
    
    int HeaderHeight   = 0;
    int LinesPerPage   = (prnHeight - HeaderHeight) / yChar;
    
    
    
    
    	StartDoc (pd.hDC, &di);
    	StartPage(pd.hDC);
    
    
    	char thisLine[80];
    int i,j;
    
    for (i = 0; i < strlen(outputText); i++) {
    	for (j = 0; j < 80; j++) {
    		if (*outputText == '\r') {
    			thisLine[j] = '\0';
    			outputText += 2;
    			break;
    		} else if (j == 80) {
    			thisLine[j] = '\0';
    		} else {
    			thisLine[j] = *outputText++;
    		}
    	}
    
    	if (((i - 1) % LinesPerPage == 0) && (i != 1)) {
    		// We should start a new page first
    		EndPage(pd.hDC);
    		StartPage(pd.hDC);
    		TextOut(pd.hDC,leftOffset, (yChar * i + HeaderHeight + topOffset)
                        - ((i - 1)/LinesPerPage)*prnHeight, thisLine, lstrlen(thisLine));
    	} else {
    		TextOut(pd.hDC,leftOffset, (yChar * i + HeaderHeight + topOffset)
                        - ((i - 1)/LinesPerPage)*prnHeight, thisLine, lstrlen(thisLine));
    	}
    }
    
    
    	EndPage(pd.hDC);
    	EndDoc(pd.hDC);
    
    	DeleteDC(pd.hDC);
    
    
    	return TRUE;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > outputText += 2;
    This will mess up your strlen() condition in the loop.
    Besides, calling strlen() every time is inefficient.

    > else if (j == 80)
    Given your loop condition of <80, this will never happen.

    Which suggests you're not putting a \0 in the right place, and your lstrlen(thisLine) calls are wrong as well.

    Oh, and you might do ZeroMemory on di and tm as well (perhaps)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop shutdowns unexpectedly
    By geek@02 in forum Tech Board
    Replies: 49
    Last Post: 03-06-2010, 01:24 PM
  2. Replies: 4
    Last Post: 10-12-2009, 10:16 AM
  3. My program unexpectedly quits
    By Unclejunebug in forum C Programming
    Replies: 13
    Last Post: 04-05-2009, 10:30 AM
  4. Program ends unexpectedly
    By keelhauled in forum C Programming
    Replies: 3
    Last Post: 10-12-2007, 05:41 PM
  5. My !@#$ing Printer
    By DeepFyre in forum Tech Board
    Replies: 2
    Last Post: 02-27-2005, 02:38 AM