Print Spooler API

I can't figure out how to get my printer to print. I can do it the old DOS way, but I want to do it a more modern way that works better for Windows. I found this:

http://msdn.microsoft.com/en-us/library/dd162861(v=VS.85).aspx

Perfect. I wrote an test application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <windows.h>
#include <wincon.h>
#include <winspool.h>

#include <stdio.h>
#include <stdlib.h>

DOC_INFO_1 docinfo1;
char docname[] = "Crap face";
char outputfile[] = "testingout";
char datatype[] = "RAW";
int print_handle = 0;
int bytes_written;
char string[] = "This is a test...\n1... 2... 3...\n\f";

int temp;
char portname[] = "HP Deskjet 3900 Series";
HANDLE conhandle;
COORD consize = {80, 50};
SMALL_RECT conwinsize = {0, 0, 80-1, 50-1};

int main(){
	conhandle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTitle("Printer test!");
	SetConsoleScreenBufferSize(conhandle, consize);
	SetConsoleWindowInfo(conhandle, TRUE, &conwinsize);
	WriteConsole(conhandle, "Test!\n", 12, &temp, NULL);
	printf("1...\n");
	printf("2...\n");
	printf("3...\n");
	printf("4...\n");
	system("PAUSE");
	
	OpenPrinter(portname, &print_handle, NULL);
	printf("Handle: 0x%08X\n", print_handle);
	if(print_handle == 0){
		printf("Crap!\n");
		system("PAUSE");
		return 0;
	}
	
	docinfo1.pDocName = docname;
	docinfo1.pOutputFile = outputfile;
	docinfo1.pDatatype = datatype;
	temp = StartDocPrinter(print_handle, 1, &docinfo1);
	printf("%i ");
	
	temp = StartPagePrinter(print_handle);
	printf("%i");
	
	temp = WritePrinter(print_handle, string, strlen(string), &bytes_written);
	printf("%i\n");
	printf("Bytes written: %i\n", bytes_written);
	
	system("PAUSE");
	
	printf("EndPagePrinter()\n");
	EndPagePrinter(print_handle);
	
	system("PAUSE");
	
	printf("EndDocPrinter()\n");
	EndDocPrinter(print_handle);
	
	system("PAUSE");
	
	printf("ClosePrinter()\n");
	ClosePrinter(print_handle);
	
	system("PAUSE");
	
	return 0;
}


There's just one problem... it doesn't print! I look at the que, and the file is clearly there, and bytes have been written to the spooler, yet the document does not print. Is there anyone familliar with this API or thinks they can figure it out that can help me in understanding why my program doesn't work the way it should?
Last edited on
It works for me if i change the following two lines:
1
2
3
4
5
6
7
DOC_INFO_1 docinfo1;
char docname[] = "Crap face";
char outputfile[] = "";  //**********changed to empty string
char datatype[] = "text"; //*********changed from RAW to text
int print_handle = 0;
int bytes_written;
char string[] = "This is a test...\n1... 2... 3...\n\f";



Did your code compile without any warnings - I ask because some of your types seem mismatched.
For example you have :
int print_handle = 0;
I would have expected:
HANDLE print_handle = INVALID_HANDLE_VALUE
Last edited on
It never gave me any warnings, though for compatability with other compilers and compiler settings, it would be a good idea to take your suggestion and change the types. I just used basic integer types because I knew for the way my system is set up it would work (since it is a test program).

The changes you made worked. It successfully prints out a document. Here's a question though -- how do I use print commands (like 0x07 for instance which, on my receipt printer, will open the cash drawer)? I've searched all over the internet, and I find plenty of documentation on these commands, but no documentation on how to use them using this API.
You should be able to send control characters:
char string[] = "This is a test \x07";

( 0x07 is the alert/bell char which has a predefined escape sequence which is \a )

See if that works.

Actually I have several POS receipt printers which have cash drawer connections, I'll try one later this week/next week.
Topic archived. No new replies allowed.