Home » Developing U++ » U++ Developers corner » Print raw data (direct print)
Print raw data (direct print) [message #35876] |
Fri, 30 March 2012 10:36 |
|
Hello,
I propose to add method to communicate direct with a printer.
For this I want to share with windows code, for linux I developed this by using lpr -oraw in nodeJS JavaScript code.
OK. The windows U++ version is:
#include <Core/Core.h>
#include <windows.h>
#include <Winspool.h>
#pragma comment(lib, "Winspool.lib")
using namespace Upp;
bool RawDataToPrinter(const String &printername, const String &data, const String &docname, const String &type);
bool RawDataToPrinter(const String &printername, const String &data, const String &docname){
return RawDataToPrinter(printername, data, docname, "RAW");
}
bool RawDataToPrinter(const String &printername, const String &data){
return RawDataToPrinter(printername, data, "My Document", "RAW");
}
#if COMPILER_MSC
//
// RawDataToPrinter - sends binary data directly to a printer
//
// printername: String specifying printer name
// data: String raw data bytes
// docname: String specifying document name
// type String specifying data type. E.G.: RAW, TEXT, ...
//
// Returns: true for success, false for failure.
//
bool RawDataToPrinter(const String &printername, const String &data, const String &docname, const String &type){
bool bStatus = false;
HANDLE hPrinter = NULL;
DOC_INFO_1 DocInfo;
DWORD dwJob = 0L;
DWORD dwBytesWritten = 0L;
// Open a handle to the printer.
bStatus = OpenPrinter( (LPTSTR)(~printername), &hPrinter, NULL );
if (bStatus) {
// Fill in the structure with info about this "document."
DocInfo.pDocName = (LPTSTR)(~docname);
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = (LPTSTR)(~type);
// Inform the spooler the document is beginning.
dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
if (dwJob > 0) {
// Start a page.
bStatus = StartPagePrinter( hPrinter );
if (bStatus) {
// Send the data to the printer.
bStatus = WritePrinter( hPrinter, (LPTSTR)(~data), data.GetLength(), &dwBytesWritten);
EndPagePrinter (hPrinter);
}else{
Cout()<<"StartPagePrinter error\n";
}
// Inform the spooler that the document is ending.
EndDocPrinter( hPrinter );
}else{
Cout()<<"StartDocPrinter error\n";
}
// Close the printer handle.
ClosePrinter( hPrinter );
}else{
Cout()<<"OpenPrinter error "<<printername<<"\n";
}
// Check to see if correct number of bytes were written.
if (!bStatus || (dwBytesWritten != data.GetLength())) {
bStatus = false;
Cout()<<"not sent all bytes\n";
} else {
bStatus = true;
}
return bStatus;
}
#else
#error Wrong OS for compile raw printer
#endif
and a little test to communicate with ZEBRA label printer:
CONSOLE_APP_MAIN
{
String data;
data<<"N"
<<"\nS4"
<<"\nD15"
<<"\nq400"
<<"\nR"
<<"\nB20,10,0,1,2,30,173,B,\"123456\""
<<"\nP0"
<<"\n";
ToAscii(data);
if(RawDataToPrinter("\\\\ilupascu.main.orange.md\\ZEBRA", data)){// or local printer just the name of the printer
Cout()<<"OK\n";
}else{
Cout()<<"FAILED\n";
}
}
|
|
|
Goto Forum:
Current Time: Thu May 02 23:23:56 CEST 2024
Total time taken to generate the page: 0.04716 seconds
|