我試圖從打印機讀取狀態和一些其他信息,但是我沒有收到打印機返回的任何數據。我無法弄清楚我失蹤或做錯了什麼?C++:從打印機讀取數據
我使用,我已經包括了WINSPOOL庫
作爲一個方面說明Qt Creator的:這是我第一次嘗試從硬件上讀取數據。
MSDN的頁面打印機的功能有: SetPrinterDataGetPrinterDataOpenPrinter
代碼
BOOL status = false;
HANDLE hPrinter = NULL;
PRINTER_DEFAULTS defaults;
defaults.pDatatype = (LPTSTR)__TEXT("RAW");
defaults.pDevMode = 0;
defaults.DesiredAccess = PRINTER_READ;
status = OpenPrinter((LPTSTR)__TEXT("CN551A"),&hPrinter,&defaults);
if(status) {
qDebug() << "Status: OPEN OK";
}
// pValueName found in registry
LPTSTR pValueName = (LPTSTR)"PrinterData";
DWORD type = REG_BINARY;
BYTE pData[2];
pData[0] = 0;
pData[1] = 0;
status = SetPrinterData(&hPrinter,pValueName,type,pData,sizeof(pData));
if(status) {
qDebug() << "Status: SET OK";
}
BYTE buffer[263];
LPDWORD pcbNeeded = 0;
LPDWORD pType = (LPDWORD)REG_BINARY;
status = GetPrinterData(&hPrinter, pValueName,pType,buffer,sizeof(buffer),
pcbNeeded);
if(status) {
qDebug() << "Status: GET OK";
qDebug() << "pType: " << pType;
qDebug() << "pcbNeeded " << pcbNeeded;
qDebug() << "buffer " << *buffer;
/* Prints a bunch of numbers
for(int i =0; i < sizeof(buffer); i++) {
qDebug() << buffer[i];
}
*/
}
ClosePrinter(&hPrinter);
輸出
Status: OPEN OK
Status: SET OK
Status: GET OK
pType: 0x3
pcbNeeded 0x0
buffer 1
我使用Qt的創造者,其中I H AVE包括WINSPOOL庫
作爲一個方面說明:這是我第一次嘗試達致這。
編輯
I found a C# program它使用Windows API調用,就像我想在PrintLabel.cs
讀取狀態和信息在ReadBytesFromPrinter
功能,但我還是無法弄清楚我的錯誤/錯誤。以爲我會鏈接它,如果有人能發現我的代碼與他們的代碼相比有什麼問題。