|
-
July 12th, 2010, 07:49 AM
#1
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.
-
July 12th, 2010, 09:20 AM
#2
Re: Printing to Default Printer
Maybe the following articles can get you started: http://www.codeguru.com/cpp/w-p/printing/
-
July 14th, 2010, 12:23 PM
#3
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);
-
July 14th, 2010, 02:15 PM
#4
Re: Printing to Default Printer
Define "won't compile". What are the errors?
Viggy
-
July 18th, 2010, 10:55 AM
#5
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.
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?
-
July 18th, 2010, 11:39 AM
#6
Re: Printing to Default Printer
 Originally Posted by mmscg
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.
-
July 18th, 2010, 11:49 AM
#7
Re: Printing to Default Printer
OK, Thanks!
I can continue on this path then.
-
July 18th, 2010, 06:23 PM
#8
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);
-
July 18th, 2010, 06:39 PM
#9
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
-
July 19th, 2010, 04:56 PM
#10
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.
-
July 21st, 2010, 07:39 AM
#11
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««««««««>
-
July 21st, 2010, 09:06 AM
#12
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.
-
July 21st, 2010, 12:38 PM
#13
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?
-
July 21st, 2010, 01:45 PM
#14
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.
-
July 24th, 2010, 10:47 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|