WinAPI function LoadLibrary() causing function fai

2019-03-06 07:11发布

问题:

The following code loads a dll in runtime using LoadLibrary().

boolean Load_Internals_dll()
{
    boolean result = true;

    //LOG_INIT;
    HINSTANCE hApexRLIO = LoadLibrary(L"APEX_RLIO_Internals.dll");

    printf("Checking getlasterror after loadlibrary:\n",GetLastError());
    if(NULL == hApexRLIO)
    {
        result = false;
        printf("[ERR]:  Unable to load dll\n");
        LOG_PRINT(" [ERR]:  Unable to load dll.\n" );

    }
    result = InitRSIDComm_Lib_Func();
    //LOG_EXIT;
    return result;
}

After loading the dll, the handle hApexRLIO is found to be not null, but the function GetLastError() returns system error code 1627 (Function failed during execution).

What could be the possible causes for this error?

There is not much documentation for this error code in the web, VC++ ten gallon heads please help me solving this.

回答1:

The documentation says:

If the function succeeds, the return value is a handle to the module.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

When the function succeeds, the return value is not NULL. This is your scenario. The documentation makes no promise as to what GetLastError will return in case the function succeeds. Some Win32 API functions will call SetLastError(ERROR_SUCCESS) before returning, but not all. This is one that does not.

In other words, there is no error. The function return value indicates success, and you should not call GetLastError because the value it returns is meaningless.