Click to See Complete Forum and Search --> : sending text to the printer


Dark_Phoenix
March 23rd, 2006, 10:17 AM
Hi guys!

This is my first time posting here, and I am a newbie with API functions
so please be kind!

I am learning how to use the Printer API to send simple text to my printer, here is the code I have so far.

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
char test[] = "This is a test line to be printed.";

LPHANDLE hPrinter;
LPBYTE DocInfo;
LPVOID Buff = test;
DWORD read;
LPDWORD written;

OpenPrinter("HP DeskJet 712C", hPrinter, NULL);
StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);
StartPagePrinter(hPrinter);

WritePrinter(hPrinter, Buff, read, written);

EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);

cout<<"Test line sent to printer"<<endl
<<"Hit <enter> to end";
cin.get();
return 0;
}

I am getting a linker error - undefined reference to all of these function calls

OpenPrinter
StartDocPrinter
StartPagePrinter
WritePrinter
EndPagePrinter
EndDocPrinter
ClosePrinter

What am I doing wrong here? Am I not using the right #include directive?
Or is it something else?

Or am I just totally wrong?

Thanks in advance for any help!

philkr
March 23rd, 2006, 10:39 AM
You have to link with winspool.lib. Change the project settings or try this:
#pragma comment(lib, "Winspool.lib")

Dark_Phoenix
March 24th, 2006, 07:28 AM
#pragma comment(lib, "Winspool.lib")
OK, I did that and I am still getting the same linker errors. I did a search and could not find a Winspool.lib file in my compiled directory but there was a libwinspool file so I tried that one but still did not work.

I am at a loss, any other ideas?

BTW, I am using DEV-C++ and OS is Windows XP

philkr
March 24th, 2006, 07:33 AM
I did a search and could not find a Winspool.lib file in my compiled
You should have it. If you don't have get it from somewhere.

Dark_Phoenix
March 24th, 2006, 08:32 AM
Any Idea where I can download a copy of it from?

Guidosoft
March 24th, 2006, 11:16 AM
There's actually an error in your code.

Right after LPDWORD written, you should have

written = new DWORD;


And at the end you should have:

delete written;


Because you can't just use pointers to nothing. It has to be referneced to an allocated area in memory.

You should have just done DWORD written, and then used &written.

Dark_Phoenix
March 25th, 2006, 11:10 AM
OK, I have winspool linked and compiled the program, but it crashes as soon as it is executed. A window comes up with 'WinPrintProject has encountered an error and needs to close' The only thing that I can think of is that there is something wrong with how I am using the pointers for the WritePrinter call, but I am not really sure.
Can anyone point me in the right direction?

Dark_Phoenix
March 27th, 2006, 01:24 PM
Well, I got the program to compile with no errors, But there is no output from the printer. I though that I would try the FlushPrinter function to empty the buffer but I got a 'FlushPrinter Undeclaired' error, So I guess my winspool library does not contain that function? Is there something with the code I am not seeing or is it something with my OS (windows XP) that I need to look at?

#include <iostream>
#include <windows.h>
#include <winspool.h>

using namespace std;

int main()
{
char test[] = "This is a test line to be printed.";
char printername[] = "HP DeskJet 712C";
char docname[] = "Print Test";

LPTSTR pPrinterName = printername;
LPVOID pBuf = test;
DWORD cbBuf = sizeof(test);
DWORD Level = 1;
DWORD Written;
HANDLE hPrinter;
DOC_INFO_1 pDocInfo;
pDocInfo.pDocName = docname;
pDocInfo.pOutputFile = NULL;
pDocInfo.pDatatype = NULL;

OpenPrinter(pPrinterName, &hPrinter, NULL);
StartDocPrinter(hPrinter, Level, (BYTE*)&pDocInfo);
StartPagePrinter(hPrinter);

WritePrinter(hPrinter, pBuf, cbBuf, &Written);
FlushPrinter(hPrinter, pBuf, cbBuf, &Written, cSleep);

EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);

cout<<endl
<<"Test line sent to printer"<<endl
<<"Hit <enter> to end";


cin.get();
return 0;
}

I Did a check on all of the function calls and they all indicate success, and GetLastError returned no errors for any of the function calls.
Anyone have any ideas why there is no printer output?