If the function fails I am trying to log an error message that will clarify the reason for the failure. In order to do that I'm using GetLastErrorMsg immediately after the failed function.
However, no matter what, the GetLastErrorMsg function returns the message "The operation completed succesfully".
On the other hand, if I replace the call to GetLastErrorMsg with the Windows API call like: FormatMessage(..,.., GetLastError(),...) I retrieve the correct error.
In the following code I'm calling PrintPDFDocument with non existing file name, yet Amyuni dll function GetLastErrorMsg returns with "The operation completed succesfully":
Code: Select all
if (TRUE != PrintPDFDocument("non_existing_file_path, "printer_name", 1, 10000, 1))
{
GetLastErrorMsg((char*)lpMsgBuf,sizeof(buf));
cout << "Printing failed, Amyuni error is: " << (char*)lpMsgBuf << endl;
}
while the following code provides the correct error message:
Code: Select all
if (TRUE != PrintPDFDocument("non_existing_file_path, "printer_name", 1, 10000, 1))
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, NULL, NULL);
cout << "Printing failed, Microsoft error is: " << (char*)lpMsgBuf << endl;
LocalFree(lpMsgBuf);
}
To summarize: I am preferring the usage of the Amyuni GetLastErrorMsg thinking that this function encapsulates both Windows API error messages and Amyuni specific errors. From the above bad experience however, I wonder:
1) Why the GetLastErrorMsg retruned with incorrect message?
2) What are the real rules of calling GetLastErrorMsg after library function failure?
3) If for several Library functions a failure occur and the GetLastErrorMsg is a no no. Is it safe to assume that using the Windows GetLastError along with FormatMessage will always get me the correct reason text for the failure.