Rasty Rasty - 1 year ago 213
C Question

Winapi: PrintUIEntry returns error code 2

I'm new with winapi and have some problem. Here is my code:

typedef BOOL (WINAPI *pPrintUIEntry)(HWND, HMODULE, wchar_t *, DWORD);

printuiDll = LoadLibrary("printui.dll");

if (printuiDll == NULL)
printf("Error LoadLibrary %d.\n", GetLastError());

return -1;

PrintUIEntry = (pPrintUIEntry) GetProcAddress(printuiDll, "PrintUIEntryW");

if (PrintUIEntry == NULL)
printf("Error GetProcAddress %d.\n", GetLastError());

return -1;
//cmd is /ia /m "Printer" /f "C:\Program Files x86)\\drivers\Printer\Printer.inf

if (PrintUIEntry(GetDesktopWindow(), printuiDll, cmd, SW_SHOW))
printf("Error PrintUIEntry %d.\n", GetLastError());

return -1;

Sorry I cant copy the code its my university work. The problem I have is that
returns 0 and it gives me error withe code
. I've tried to google little bit about this function but I could found much. Hope you can help. thanks.


I've checked and file exists.

Answer Source

If you read the documentation:

INFO: Windows Rundll and Rundll32 Interface

Rundll32 printui.dll,PrintUIEntry

You would see that your declaration of the PrintUIEntryW function pointer is wrong. Most importantly, functions that are designed to be called by rundll32 do not have a return value, and PrintUIEntry is not documented as using SetLastError() for error reporting.

So you cannot do any kind of error checking in this situation. You are checking for a non-existent BOOL value, which means you are checking for whatever random value happens to be stored in the EAX register when PrintUIEntryW exits, and then you are calling GetLastError() to retrieve the last error code set by SetLastError(), which may or may not be related to PrintUIEntryW at all (or may have been used internally and is not meant for caller interpretation).

In this case, this question becomes a duplicate of How to check the result of PrintUIEntry call.

Since you are trying to install a printer, consider using AddPrinter() or InstallPrinterDriverFromPackage() instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download