I'm creating a Qt WebEngine application for Windows that display a webpage and uses printer to print out whatever is displayed. I'm trying to get an error state out of my Custom vkp80III ticket printer using windows' winspool library and this example https://support.microsoft.com/en-us/kb/160129
App has this print call. (printerState is useless, it seems to be always idle but anyways):
void printReceipt() {
if (mDefaultPrinter->printerState() == QPrinter::Error) {
this->printCallback(false);
}
else {
this->print(mDefaultPrinter, [=](bool success) { this->printCallback(success); });
}
}
And then comes the problem, callback after print succesfully started:
void printCallback(bool success){
DWORD size;
bool result = GetDefaultPrinter (NULL, &size);
qDebug() << "LEN: " << size;
LPWSTR pname = (LPWSTR)malloc(size+1);
result = GetDefaultPrinter(pname, &size);
if(!result){
qDebug() << "GetDefaultPrinter err: " << GetLastError();
}
else {
pname[size] = 0;
qDebug() << "PRINTER NAME: " << &pname << ", LEN: " << size;
}
QString name = mDefaultPrinter->printerName();
//These give more or less odd results
//wchar_t* w_str = const_cast<wchar_t*>(name.toStdWString().c_str());
//LPWSTR w_str = (LPWSTR)name.utf16();
/*wchar_t* w_str = (wchar_t*) malloc (sizeof(wchar_t)*name.length()+1);
int len = name.toWCharArray(w_str);
w_str[len]=0;*/
//OpenPrinter example uses LPHANDLE but that didn't work
HANDLE hPrinter = NULL;
PRINTER_DEFAULTS pd;
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
qDebug() << "TRYING GET PRINTER: " << name;
if(OpenPrinter(pname, &hPrinter, &pd)) {
qDebug() << "GOT PRINTER ERR STATE: " << IsPrinterError(&hPrinter);
}
free(pname);
emit printReceiptComplete(success);
}
I put couple of debug logs inside MSDN example and it shows that when code tries to GetPrinter(hPrinter) it returns INVALID_HANDLE_ERROR. So I never get to point of getting the printer's errors. First I thought it was the wrong printer name, that's why there are different lines of code getting it / converting it to LPWSTR.
So what works: -I get default printer's name "CUSTOM VKP80III" so it IS there -OpenPrinter(&handle) returns true and I get the handle
Problem -GetPrinter(handle) returns false and GetLastError() is invalid handle, why?