LoadLibrary(…) failing with error code 1627:ERROR_

2019-10-09 07:10发布

This question already has an answer here:

I am trying to call the LoadLibrary for a dll which is placed in the exe path. it is giving error as ERROR_FUNCTION_FAILED. Below is the code, Please let me know the cause for 1627

    typedef int (__stdcall  *func)();
    int DynamicLoad_Lib()
    {
        func  call;
        int  iReturn;
        cout<<"\n\n DynamicLoad_Lib \n\n";
        HINSTANCE hinstLib = LoadLibrary(L"MYLib.dll");
        cout<<"\n\nGetLastError() "<<GetLastError()<<"\n";             
        if(hinstLib == NULL)
            return 0;
        call = (func_another)GetProcAddress(hinstLib, "Initialize");
        if(call == NULL)
        {
            FreeLibrary(hinstLib);
            return 0;
        }
        iReturn = (*call)();
        call = (func)GetProcAddress(hinstLib, "Terminate");
        if(call == NULL)
        {
            FreeLibrary(hinstLib);
            return 0;
        }
        FreeLibrary(hinstLib);
        return 1;
    }

int _tmain(int argc, _TCHAR* argv[])
{
    DynamicLoad_Lib();
    return 0;
}

1条回答
ら.Afraid
2楼-- · 2019-10-09 07:29

This prints the last error that happened in any Windows API function:

    cout<<"\n\nGetLastError() "<<GetLastError()<<"\n";             
    if(hinstLib == NULL)
        return 0;

It should be, print any error that happened in the LoadLibrary function:

    if(hinstLib == NULL)
    {
        cout<<"\n\nGetLastError() "<<GetLastError()<<"\n";             
        return 0;
    }

LoadLibrary RTFM:

Return value

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.

查看更多
登录 后发表回答