winapi – call to LoadLibrary from ATL COM plug-in – Education Career Blog

Hi
I have an ATL based COM plugin and there is another 3rd party library that I want to use. It’s not COM dll and implemented in C++.

details on 3rd party dll as follows:

Configuration type = Dynamic Library(.dll)

Use of MFC = Use MFC in a static library

Use of ATL = not using ATL

Character set = use multi-btye char set

CRL support=no clr support

details on COM dll project as follows:

Configuration type = Dynamic Library(.dll)

Use of MFC = Use MFC in a shared library

Use of ATL = dynamic link to ATL

Character set = use unicode char set

CRL support=no clr support

This is my code

HRESULT FinalConstruct()
{
    LPCWSTR libPath = _T("LicEnf.dll") ;
    const char * clibPath = "LicEnf.dll" ;

    SetLastError(0);
    HMODULE hMod = LoadLibraryA(clibPath);
    if(hMod==NULL)
    {

        LOG4CXX_TRACE(CALMLogger::GetModuleLogger(ModuleName),
            _T("Could not load library:GetLastError() returned ") << GetLastError());

    }

    return S_OK;
}

FinalConstruct is called 3times. hMod always 0 and on the log file i have

Could not load library:GetLastError() returned 183

Could not load library:GetLastError() returned 126

Could not load library:GetLastError() returned 126

where

-183 (0xB7) =ERROR_ALREADY_EXISTS

-126 (0x7E) =ERROR_MOD_NOT_FOUND

On the other hand if I call this code from another test application which is an ordinary exe file , loadLibrary works fine.
Is there any limitation of loading a native win32 dll from COM-dll and what’s my mistake here ?

Thanks

,

Did you try to specify full path to the library?

,

LoadLibrary required full path name. here is the working code

HRESULT FinalConstruct()
{
    LPCWSTR libPath = _T("d:\\projects\\LMS\\src\\LMS_LIB\\LicEnf\\Debug\\LicEnf.dll" ) ;
    DWORD  retval=0;
    BOOL   success; 
    TCHAR  bufferBUFSIZE=TEXT(""); 
    TCHAR  bufBUFSIZE=TEXT(""); 
    TCHAR** lppPart={NULL};

    SetLastError(0);
    SetErrorMode(0);
    retval = GetFullPathName(libPath,
         BUFSIZE,
         buffer,
         lppPart);

    HMODULE hMod = LoadLibrary(buffer);
    if(hMod==NULL)
    {
        int lastError = GetLastError();
        CString message;
        message.Format( _T("Failed in finding LicEnf.dll (%d)"), lastError );
        LOG4CXX_TRACE(CALMLogger::GetModuleLogger(ModuleName),message );

    }

    return S_OK;
}

Leave a Comment