GetPrinter function fails to retrieves the printer information. [modified]
-
Hi Please help me to find out the problem in GetPrinter function. First I am trying to get the printer handle for the specified printer using OpenPrinter function. . HANDLE hPrinter = 0; OpenPrinter ((LPTSTR)(LPCTSTR)strPrinterAddress, &hPrinter, NULL); This function gets succeed. Using this printer handle I am trying to access the specified printer information’s using the GetPrinter function. GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); } Even using GetLastError() function I am not able retrieve error message. This function is not failing all the times. When I am trying to print out of 10 times, 6 times the printing is succeed. Only 4 times it’s getting failed. Please help me shoot out this problem. Thanks Gokul
modified on Tuesday, October 14, 2008 4:08 AM
-
Hi Please help me to find out the problem in GetPrinter function. First I am trying to get the printer handle for the specified printer using OpenPrinter function. . HANDLE hPrinter = 0; OpenPrinter ((LPTSTR)(LPCTSTR)strPrinterAddress, &hPrinter, NULL); This function gets succeed. Using this printer handle I am trying to access the specified printer information’s using the GetPrinter function. GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); } Even using GetLastError() function I am not able retrieve error message. This function is not failing all the times. When I am trying to print out of 10 times, 6 times the printing is succeed. Only 4 times it’s getting failed. Please help me shoot out this problem. Thanks Gokul
modified on Tuesday, October 14, 2008 4:08 AM
Why do you check
dwNeeded
instead ofGetPrinter
return value? What isGetLastError
return value whenGetPrinter
fails? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Why do you check
dwNeeded
instead ofGetPrinter
return value? What isGetLastError
return value whenGetPrinter
fails? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
The Get Printer function will return required buffer size in dwNeeded. I hope this should always be greater than zero. So I checked with the buffer size. When I am trying to print the last error code it is printed nothing (empty).
I would check the
GetPrinter
return value, and callGetLastError
only when it fails. Are you aware that (MSDN [^]): Remarks Security Alert The pDevMode member in the PRINTER_INFO_2, PRINTER_INFO_8, and PRINTER_INFO_9 structures can be NULL. When this happens, the printer is unusable until the driver is reinstalled successfully. ? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi Please help me to find out the problem in GetPrinter function. First I am trying to get the printer handle for the specified printer using OpenPrinter function. . HANDLE hPrinter = 0; OpenPrinter ((LPTSTR)(LPCTSTR)strPrinterAddress, &hPrinter, NULL); This function gets succeed. Using this printer handle I am trying to access the specified printer information’s using the GetPrinter function. GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); } Even using GetLastError() function I am not able retrieve error message. This function is not failing all the times. When I am trying to print out of 10 times, 6 times the printing is succeed. Only 4 times it’s getting failed. Please help me shoot out this problem. Thanks Gokul
modified on Tuesday, October 14, 2008 4:08 AM
Gokul_md wrote:
GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); }
This should be:
if (! GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded))
{
DWORD dwError = GetLastError();
FormatMessage(..., dwError, ...);
}ClosePrinter(hPrinter);
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Gokul_md wrote:
GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); }
This should be:
if (! GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded))
{
DWORD dwError = GetLastError();
FormatMessage(..., dwError, ...);
}ClosePrinter(hPrinter);
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Yes i have formated the dword error code to string in the above mentioned way only. but it return the empty string.
Gokul_md wrote:
Yes i have formated the dword error code...
What was its value?
Gokul_md wrote:
...but it return the empty string.
How are you verifying this?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Gokul_md wrote:
Yes i have formated the dword error code...
What was its value?
Gokul_md wrote:
...but it return the empty string.
How are you verifying this?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
i have formated the message using the below method. LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | RMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, MAKELANGID(LANG_NEUTRAL, LANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,NULL ); CString strMessage = (LPTSTR)lpMsgBuf; The error message is printed in the log file. FILE *fp = fopen("C:\\log.txt","a+"); fprintf(fp,"\n Error Occured - %s ", strMessage ); fclose(fp); in that log file it printer as Error Occured -
-
i have formated the message using the below method. LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | RMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, MAKELANGID(LANG_NEUTRAL, LANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,NULL ); CString strMessage = (LPTSTR)lpMsgBuf; The error message is printed in the log file. FILE *fp = fopen("C:\\log.txt","a+"); fprintf(fp,"\n Error Occured - %s ", strMessage ); fclose(fp); in that log file it printer as Error Occured -
What is the value of
dwError
?"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch