CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Apr 2004
    Posts
    204

    Printing to Default Printer

    In the simple program below, what do I have to do to send the output
    to the default printer rather than a message box?
    Code:
    #include <windows.h>
    
    
    int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow)
    {
        int i;
    
        for (int i=1; i < 6; i++)
        {
    	MessageBox(NULL, i, NULL, MB_ICONWARNING);
        }
    
    	return 0;
    }
    Printed page output like so:

    1
    2
    3
    4
    5

    Seems trivial I know, and yes I've searched, but I cannot find what I am looking for.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Printing to Default Printer

    Maybe the following articles can get you started: http://www.codeguru.com/cpp/w-p/printing/
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    Thanks for link.

    Those are a bit more complicated than I was hoping for, but if the only way,
    I will try to understand.

    In the mean time, I was hoping for a one line printer call

    I found these 2 simple snippets, but when I insert either into my program it won't compile.

    Any ideas what I am doing wrong?

    Code:
    fprintf(stdprn,"Test test test\f");
    Code:
    FILE *stdprn;
    stdprn = fopen("PRN","wb");
    fprintf(stdprn,"From Windows: Test test test\f");
    fclose(stdprn);

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Printing to Default Printer

    Define "won't compile". What are the errors?

    Viggy

  5. #5
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    OK, I finally got this to work (not to printer, but a file).

    I tested on two different computers.

    0 errors, 0 warnings on both computers.

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance ,
    			       PSTR lpCmdLine , int nCmdShow ) 
    {
        
        FILE *fp;
        char *str; 
     
        fp = fopen("sample.txt", "w");
    
        str = "line 1\n";
        fwrite(str, 1, strlen(str), fp);
     
        str = "line TWO\n";
        fwrite(str, 1, strlen(str), fp);
    
        MessageBox(NULL , TEXT("File Written") , lpCmdLine , MB_OK);
        
        return 0;
    }
    Contents of sample.txt for both computers.
    Code:
    line 1
    line TWO
    Contents of debug window on one computer
    Code:
    'Print.exe': Loaded 'C:\... ...\WriteFile00\Debug\Print.exe', Symbols loaded.
    'Print.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
    'Print.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
    'Print.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.
    'Print.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.
    'Print.exe': Loaded 'C:\WINDOWS\system32\uxtheme.dll', No symbols loaded.
    'Print.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
    'Print.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.
    'Print.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.
    The program '[1316] Print.exe: Native' has exited with code 0 (0x0).
    Contents of debug window on another computer
    Code:
    'Print.exe': Loaded 'C:\... ...\WriteFile00\Debug\Print.exe', Symbols loaded.
    'Print.exe': Loaded 'C:\WINNT\system32\NTDLL.DLL', Cannot find or open a required DBG file.
    'Print.exe': Loaded 'C:\WINNT\system32\USER32.DLL', Cannot find or open a required DBG file.
    'Print.exe': Loaded 'C:\WINNT\system32\KERNEL32.DLL', Cannot find or open a required DBG file.
    'Print.exe': Loaded 'C:\WINNT\system32\GDI32.DLL', Cannot find or open a required DBG file.
    The program '[624] Print.exe: Native' has exited with code 0 (0x0).
    Why the different output?

    What does Cannot find or open a required DBG file mean?

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Printing to Default Printer

    Quote Originally Posted by mmscg View Post
    What does Cannot find or open a required DBG file mean?
    It just means that those dll's don't have any debug information, i.e. nothing to be worried about.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    OK, Thanks!

    I can continue on this path then.

  8. #8
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    Moving forward, I have this:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance ,
    			       PSTR lpCmdLine , int nCmdShow )
    {
        
        FILE *fp;
        char *str;
        char buffer [33];
    
        fp = fopen("sample.txt", "w");
    
        str = "output:\n";
    	fwrite(str, 1, strlen(str), fp);
    
        for (int i=1; i < 6; i++)
        {
    		
    		switch ( i )
    		{
    			case 1:
    				str = "(1st line)\n";
    				break;
    			case 2:
    				str = "(2nd line)\n";
    				break;
    			case 3:
    				str = "(3rd line)\n";
    				break;
    			case 4:
    				str = "(4th line)\n";
    				break;
    			case 5:
    				str = "(5th line)\n";
    				break;
    		}		
    
    		fwrite("i = ",1,4,fp);
    		fwrite(itoa(i, buffer, 10), 1, 1, fp);
    		fwrite("   ", 1, 3, fp);
    		fwrite(str, 1, strlen(str), fp);
    
        }
       
        MessageBox(NULL , TEXT("File Written") , lpCmdLine , MB_OK);
        
        return 0;
    }
    Contents of sample.txt:
    Code:
    output:
    i = 1   (1st line)
    i = 2   (2nd line)
    i = 3   (3rd line)
    i = 4   (4th line)
    i = 5   (5th line)
    Is it possible to replace the four fwrites with one, ang get the same output?

    Something like?

    fwrite("i = " & i & " " & str, 1, 18, fp);

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Printing to Default Printer

    Code:
    const char *sArray[] = { 
        "(1st line)\n",
        "(2nd line)\n",
        "(3rd line)\n",
        "(4th line)\n",
        "(5th line)\n"
    };
    //..
    for (int i = 0; i < sizeof(sArray) / sizeof(sArray[0]); ++i )		
    {
       // no case statement needed now
       str = sArray[i];
       //... 
    }
    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    Thanks Paul for the reply.

    I was not trying to get rid of the switch block, but rather have one fwrite statement to accomodate different data types; strings, integers, etc.

    I found that the fprintf statement would give me what I was after.
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance ,
    			       PSTR lpCmdLine , int nCmdShow )
    {
        
        FILE *fp;
        char *str;
        byte b;
    
        b=255;
        fp = fopen("sample.txt", "w");
    
        str = "output:\n";
        fwrite(str, 1, strlen(str), fp);
    
        for (int i=1; i < 6; i++)
        {
    		
    		switch ( i )
    		{
    			case 1:
    				str = "(1st line)";
    				b=34;
    				break;
    			case 2:
    				str = "(2nd line)";
    				b=127;
    				break;
    			case 3:
    				str = "(3rd line)";
    				b=122;
    				break;
    			case 4:
    				str = "(4th line)";
    				b=13;
    				break;
    			case 5:
    				str = "(5th line)";
    				b=94;
    				break;
    		}		
    
    		fprintf(fp,"i = %d %s %x\n", i, str, b);
    
        }
       
        MessageBox(NULL , TEXT("File Written") , lpCmdLine , MB_OK);
        
        return 0;
    }
    sample.txt file
    Code:
    output:
    i = 1 (1st line) 22
    i = 2 (2nd line) 7f
    i = 3 (3rd line) 7a
    i = 4 (4th line) d
    i = 5 (5th line) 5e
    Seems OK.

    Now to try to put this in my real program, and see if it gives me the output I am after.

  11. #11
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    I think I have the "printing" working correctly,
    but I find some strange results when looking over the printed output.

    Is it a problem with my fprintf usage?

    In the output below generated by the following code,
    why the 8 characters «««««««« following untitled?

    Code:
    Code:
    //-------------------------------------------------------
    LPVOID Alloc(DWORD dwSize)
    {
        return HeapAlloc(g_hheap, HEAP_ZERO_MEMORY, dwSize);
    }
    //-------------------------------------------------------
    
    
    //-------------------------------------------------------
    struct EVENT {
        int     nData;
        LPBYTE  lpData;
    };
    
    typedef struct EVENT EVENT;
    typedef struct EVENT *LPEVENT;
    
    LPEVENT lpEvent;
    DWORD dw;
    
    dw = 8;
    lpEvent->nData  = dw;
    lpEvent->lpData = (LPBYTE)Alloc(lpEvent->nData);
    mmioRead(hmmio, (HPSTR)lpEvent->lpData, lpEvent->nData);
    
    fprintf(fp,"lpEvent->nData   = %d\n", lpEvent->nData);
    fprintf(fp,"lpEvent->lpData  = <%s>\n", lpEvent->lpData);
    //-------------------------------------------------------
    Output:
    Code:
    lpEvent->nData   = 8
    lpEvent->lpData  = <untitled««««««««>

  12. #12
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Printing to Default Printer

    Because lpEvent->lpData is not NULL terminated.
    If you want to read 8 characters from a file into a buffer and later print it, it's best to allocate 9 bytes, read 8 bytes from file and set the 9th byte to \0. That way, fprintf will stop correctly at the \0 and will not print garbage.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  13. #13
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    I think I understand what you are saying, and I will try as you suggest.

    What I do not understand is the length of my returned string; 8 characters in untitled and 8 «

    Is lpEvent->lpData a pointer to a buffer?

    If yes, does this buffer have a fixed or limiting size?

  14. #14
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Printing to Default Printer

    Yes, lpEvent->lpData is some kind of buffer.
    When you pass it to fprintf, fprintf will just output characters until it finds a \0 character.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  15. #15
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    With regards to the code/output in Post #11, I would like output as so:
    Code:
    lpEvent->nData   = 8
    lpEvent->lpData  = <untitled««««««««>
    char 1 = u = hex = 75
    char 2 = n = hex = 6E
    char 3 = t = hex = 74
    char 4 = i = hex = 69
    char 5 = t = hex = 74
    char 6 = l = hex = 6C
    char 7 = e = hex = 65
    char 8 = d = hex = 64
    This now is not a question of how to use the fprintf function, but rather
    how to read each character in turn from the lpEvent->lpData string and convert to hex.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured