主页 > tokenpocket钱包 > [bitpie安卓版]怎么使用c调用windows api进行打印

[bitpie安卓版]怎么使用c调用windows api进行打印

admin tokenpocket钱包 2022年07月27日
摘要:如何使用c调用windows api进行打印:本文主要介绍怎么使用c调用windows api进行打印,希望能够解决您遇到有关问题,下


前言
#include <iostream> #include <windows.h> #include "node.h" #include "base64.h" using namespace std; using v8::FunctionCallbackInfo; using v8::Isolate; using v8::Local; using v8::NewStringType; using v8::Object; using v8::String; using v8::Value; using v8::Integer; using v8::Int8Array; BOOL RawDataToPrinter(LPSTR szPrinterName, LPBYTE lpData, DWORD dwCount); string getDefaultPrinterName(); void localPrintRawData(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); Local<v8::Context> context = isolate->GetCurrentContext(); v8::String::Utf8Value portString(isolate, args[0]); std::string base64Str(*portString); vector<BYTE> bytes = base64_decode(base64Str); char* buffer = new char[bytes.size()]; copy(bytes.begin(), bytes.end(), buffer); string printerName = getDefaultPrinterName(); if (printerName.size() > 0) { printerName += "\0"; wstring ws(printerName.begin(), printerName.end()); RawDataToPrinter(const_cast<char*>(printerName.c_str()), &bytes[0], bytes.size()); } else { cout << "no printer" << endl; } } BOOL RawDataToPrinter(LPSTR szPrinterName, LPBYTE lpData, DWORD dwCount) { HANDLE hPrinter; DOC_INFO_1 DocInfo; DWORD dwJob; DWORD dwBytesWritten; // Need a handle to the printer. if (!OpenPrinter(szPrinterName, &hPrinter, NULL)) { int y = GetLastError(); cout << "openFial" << y << endl; return FALSE; } // Fill in the structure with info about this "document." DocInfo.pDocName = LPSTR("My Document\0"); DocInfo.pOutputFile = NULL; DocInfo.pDatatype = NULL; // LPWSTR("RAW\0"); // Inform the spooler the document is beginning. if ((dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo)) == 0) { int x = GetLastError(); cout << "StartDocPrinter Fial" << x << endl; ClosePrinter(hPrinter); return FALSE; } // Start a page. if (!StartPagePrinter(hPrinter)) { EndDocPrinter(hPrinter); ClosePrinter(hPrinter); return FALSE; } // Send the data to the printer. if (!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten)) { EndPagePrinter(hPrinter); EndDocPrinter(hPrinter); ClosePrinter(hPrinter); return FALSE; } // End the page. if (!EndPagePrinter(hPrinter)) { EndDocPrinter(hPrinter); ClosePrinter(hPrinter); return FALSE; } // Inform the spooler that the document is ending. if (!EndDocPrinter(hPrinter)) { ClosePrinter(hPrinter); return FALSE; } // Tidy up the printer handle. ClosePrinter(hPrinter); // Check to see if correct number of bytes were written. if (dwBytesWritten != dwCount) return FALSE; return TRUE; } void getPrinterList() { PRINTER_INFO_2* printerList; unsigned char size; unsigned long pcbNeeded; unsigned long pcReturned; EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &pcbNeeded, &pcReturned); if ((printerList = (PRINTER_INFO_2*)malloc(pcbNeeded)) == 0) { return; } if (!EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)printerList, pcbNeeded, &pcbNeeded, &pcReturned)) { free(printerList); return; } for (int i = 0; i < (int)pcReturned; i++) { string printName(printerList[i].pPrinterName); if (printerList[i].Attributes & PRINTER_ATTRIBUTE_NETWORK) { cout << "网络打印机" << printName << endl; } else { cout << "本地打印机" << printName << endl; } } cout << "number " << pcReturned << endl; } string getDefaultPrinterName() { DWORD size = 0; GetDefaultPrinter(NULL, &size); if (size) { TCHAR* buffer = new TCHAR[size]; GetDefaultPrinter(buffer, &size); string printerName(buffer); return printerName; } else { return ""; } } void Initialize(Local<Object> exports) { NODE_SET_METHOD(exports, "localPrintRawData", localPrintRawData); } NODE_MODULE(zq_device, Initialize)

关于 "怎么使用c++调用windows api进行打印" 就介绍到这。希望大家多多支持编程宝库。

在客户的使用过程中,遇到一个问题,如果机器安装了打印机驱动,那么调用厂商提供的 sdk 进行打印的话,会导致出现小票只打印一半的情况,对此,需要绕过厂商 sdk 使用系统的打印才能够解决这一问题。
首先看看如何进行打印:

广告位
标签: 编程宝库   编程   程序设计   c