Getting a DLL name that does not call the application name

I wrote two COM classes in C ++ contained in one MFC library. They are downloaded as plugins by a third-party application.

How can I get the file name and version number of the DLL from these classes?

+3
source share
3 answers
CString GetCallingFilename(bool includePath)
{
    CString filename;
    GetModuleFileName(AfxGetInstanceHandle(), filename.GetBuffer(MAX_PATH), MAX_PATH);

    filename.ReleaseBuffer();

    if( !includePath )
    {
        int filenameStart = filename.ReverseFind('\\') + 1;
        if( filenameStart > 0 )
        {
            filename = filename.Mid(filenameStart);
        }
    }

    return filename;
}

CString GetCallingVersionNumber(const CString& filename)
{
    DWORD fileHandle, fileVersionInfoSize;
    UINT bufferLength;
    LPTSTR lpData;
    VS_FIXEDFILEINFO *pFileInfo;

    fileVersionInfoSize = GetFileVersionInfoSize(filename, &fileHandle);
    if( !fileVersionInfoSize )
    {
        return "";
    }

    lpData = new TCHAR[fileVersionInfoSize];
    if( !lpData )
    {
        return "";
    }

    if( !GetFileVersionInfo(filename, fileHandle, fileVersionInfoSize, lpData) )
    {
        delete [] lpData;
        return "";
    }

    if( VerQueryValue(lpData, "\\", (LPVOID*)&pFileInfo, (PUINT)&bufferLength) ) 
    {
        WORD majorVersion = HIWORD(pFileInfo->dwFileVersionMS);
        WORD minorVersion = LOWORD(pFileInfo->dwFileVersionMS);
        WORD buildNumber = HIWORD(pFileInfo->dwFileVersionLS);
        WORD revisionNumber = LOWORD(pFileInfo->dwFileVersionLS);

        CString fileVersion;
        fileVersion.Format("%d.%d.%d.%d", majorVersion, minorVersion, buildNumber, revisionNumber);

        delete [] lpData;
        return fileVersion;
    }

    delete [] lpData;
    return "";
}
+1
source
TCHAR fileName[MAX_PATH + 1];
GetModuleFileName(hInstance, fileName, MAX_PATH);

Where hInstanceis the one you get in the function DllMain. Do not use GetModuleHandle(0)because it returns the hInstancehost application.

+6
source

dll DLL.

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)

GetModuleFileName(hInstance, buffer, MAX_PATH);

dll.

GetFileVersionInfoSize
GetFileVersionInfo

.

+6

Source: https://habr.com/ru/post/1711170/


All Articles