Skip to content

Instantly share code, notes, and snippets.

@peters
Created August 12, 2014 08:26
Show Gist options
  • Save peters/0d5d79452aace2a7b33c to your computer and use it in GitHub Desktop.
Save peters/0d5d79452aace2a7b33c to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include "windows.h"
#include <thread>
#include <iostream>
#pragma comment(lib, "Winspool.lib")
typedef struct _CStatusThreadInfo
{
DWORD dSleepTime;
HANDLE hPrinter;
HANDLE hError;
HANDLE hStatus;
} CStatusThreadInfo;
void monitorZebraPrinter(CStatusThreadInfo *pInfo) {
HANDLE handles[2] = {
pInfo->hError,
pInfo->hStatus
};
DWORD dType = 0;
DWORD dRet = 0;
DWORD dNeeded = 0;
DWORD dwResult = 0;
printf("entering listening thread...\n");
while (true) {
printf("waiting for one or multiple events to be signaled by LM\n");
printf("--------------------------------------------------------");
dRet = WaitForMultipleObjects(2, handles, FALSE, pInfo->dSleepTime);
if (dRet != WAIT_FAILED) {
if (dRet == WAIT_OBJECT_0 || dRet == WAIT_OBJECT_0 + 1) {
GetPrinterData(pInfo->hPrinter, L"Error", &dType, (LPBYTE) &dwResult, sizeof(dwResult), &dNeeded);
printf("Error (WINDOWS): 0x%08X\n", dwResult);
GetPrinterData(pInfo->hPrinter, L"ExternalError", &dType, (LPBYTE) &dwResult, sizeof(dwResult), &dNeeded);
printf("ExternalError (Zebra specific): 0x%08X\n", dwResult);
}
}
}
};
static std::wstring GetEventName(HANDLE hPrinter, LPWSTR eventName) {
BYTE tmp[100];
DWORD dType = 0;
DWORD dRet = 0;
DWORD dNeeded = 0;
if (dRet = GetPrinterData(hPrinter, eventName, &dType, (LPBYTE) tmp, 100, &dNeeded) != ERROR_SUCCESS){
return NULL;
}
return std::wstring((wchar_t*) tmp);
};
int _tmain(int argc, _TCHAR* argv []){
HANDLE hPrinter = NULL;
LPWSTR printerName = _T("Zebra TTP 2030 (Copy 1)");
BOOL bStatus = OpenPrinter(printerName, &hPrinter, NULL);
if (!bStatus) {
printf("failed to open printer");
return -1;
}
std::wstring statusEvent = GetEventName(hPrinter, L"StatusEvent");
if (statusEvent.empty()) {
printf("failed to get status event name");
return -1;
}
std::wstring errorEvent = GetEventName(hPrinter, L"ErrorEvent");
if (errorEvent.empty()) {
printf("failed to get error event name");
return -1;
}
wprintf(L"statusEvent: %s\n", statusEvent.c_str());
wprintf(L"errorEvent: %s\n", errorEvent.c_str());
CStatusThreadInfo cti;
cti.hPrinter = hPrinter;
cti.dSleepTime = INFINITE;
cti.hStatus = OpenEvent(SYNCHRONIZE, TRUE, statusEvent.c_str()); // USB002StatusEvent
cti.hError = OpenEvent(SYNCHRONIZE, TRUE, errorEvent.c_str()); // USB002ErrorEvent
if (cti.hStatus == NULL) {
wprintf(L"failed to open event %s. reason: %d", statusEvent.c_str(), GetLastError());
return -1;
}
if (cti.hError == NULL) {
wprintf(L"failed to open event %s. reason: %d", errorEvent.c_str(), GetLastError());
return -1;
}
printf("ready...\n");
std::thread poll(monitorZebraPrinter, &cti);
poll.join();
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment