CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Get Dll Version

  1. #1
    Join Date
    Feb 2000
    Location
    Quebec
    Posts
    17

    Get Dll Version

    I create a Win32 Dll and i put an Version ressource
    how i can get these version number in an other program?
    i dont want pass by an function.. i think we have an other way
    to get a dll version.


  2. #2
    Join Date
    Feb 2000
    Location
    WA, USA
    Posts
    17

    Re: Get Dll Version

    You may use GetFileVersionInfo() API to read version info.


  3. #3
    Join Date
    Feb 2000
    Location
    Quebec
    Posts
    17

    Re: Get Dll Version

    thanx for your answer but do you have a little example on how to use the GetFileVersionInfo() ?



  4. #4
    Join Date
    Feb 2000
    Location
    WA, USA
    Posts
    17

    Re: Get Dll Version


    The following is an untested example.

    #include <windows.h>
    #include <stdio.h>

    void ShowVersionInfo( PSTR pszFileName )

    {

    DWORD cbVerInfo, dummy;
    // How big is the version info?
    cbVerInfo = GetFileVersionInfoSize( pszFileName, &dummy );
    if ( !cbVerInfo )
    return;

    // Allocate space to hold the info
    PBYTE pVerInfo = new BYTE[cbVerInfo];
    if ( !pVerInfo )
    return;
    _try
    {
    if ( !GetFileVersionInfo(pszFileName, 0, cbVerInfo, pVerInfo) )
    _leave;
    char * predefResStrings[] =
    {
    "Comments",
    "CompanyName",
    "FileDescription",
    "FileVersion",
    "InternalName",
    "LegalCopyright",
    "LegalTrademarks",
    "OriginalFilename",
    "ProductName",
    "ProductVersion",
    "PrivateBuild",
    "SpecialBuild",
    0
    };

    for ( unsigned i=0; predefResStrings[i]; i++ )
    {

    char szQueryStr[ 0x100 ];
    char szQueryStr2[0x100 ];

    // Format the string with the 1200 codepage (Unicode)
    wsprintf( szQueryStr, "\\StringFileInfo\\%04X%04X\\%s", GetUserDefaultLangID(), 1200, predefResStrings[i] );

    PSTR pszVerRetVal;
    UINT cbReturn;
    BOOL fFound;
    fFound = VerQueryValue( pVerInfo, szQueryStr, (LPVOID *)&pszVerRetVal, &cbReturn );
    if ( fFound )
    printf( " %s %s\n", predefResStrings[i], pszVerRetVal );
    }
    }
    _finally
    {
    delete []pVerInfo;
    }

    }


    void main(int argc, char* argv[])
    {

    if (argc <= 1)
    printf(" Usage: command dllname\n");

    ShowVersionInfo(argv[1]);

    }






  5. #5
    Join Date
    Nov 1999
    Location
    NY
    Posts
    906

    Re: Get Dll Version

    If you implement the GetDllVersion() function API in your DLL and export the API (Search MSDN for GetDllVersion), you can do it in a standard way. Here's a sample console application that can get any DLL's version resource information (if it exports the GetDllVersion function). Try it on shell32.dll or comctl32.dll.


    #include <windows.h>
    #include <iostream.h>

    #include <shlwapi.h>

    void main(int argc, char ** argv)
    {
    if (argc < 2)
    {
    cout << "Program to find verions of DLLs" << endl;
    cout << "Send in the dll name(s) as arguments" << endl;
    return;
    }

    int i = 1;
    while (i < argc)
    {
    LPCTSTR lpszDllName = argv[ i ];

    HINSTANCE hinstDll = LoadLibrary(lpszDllName);
    int rc = GetLastError();

    TCHAR szText[MAX_PATH] = TEXT("Could not load DLL");


    if(hinstDll)
    {
    DLLGETVERSIONPROC pDllGetVersion;

    /*
    You must get this function explicitly because the DLL might not implement
    the function. Depending upon the DLL, the lack of implementation of the
    function may be a version marker in itself.
    */
    pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hinstDll,
    TEXT("DllGetVersion"));
    if(pDllGetVersion)
    {
    DLLVERSIONINFO dvi;
    HRESULT hr;

    ZeroMemory(&dvi, sizeof(dvi));
    dvi.cbSize = sizeof(dvi);

    hr = (*pDllGetVersion)(&dvi);

    if(SUCCEEDED(hr))
    {
    wsprintf( szText, TEXT("DLL Version %d.%02d Build#%d Platform : "),
    dvi.dwMajorVersion,
    dvi.dwMinorVersion,
    dvi.dwBuildNumber);

    switch(dvi.dwPlatformID)
    {
    case DLLVER_PLATFORM_WINDOWS:
    lstrcat(szText, TEXT("Windows"));
    break;

    case DLLVER_PLATFORM_NT:
    lstrcat(szText, TEXT("Windows NT"));
    break;

    default:
    lstrcat(szText, TEXT("Undefined"));
    break;
    }
    }
    else
    {
    lstrcpy( szText,
    TEXT("Cannot determine DLL version."));
    }
    }
    else
    {
    //If GetProcAddress failed, the DLL does not implement DllGetVersion.
    lstrcpy( szText, TEXT("does not implement DllGetVersion."));
    }

    FreeLibrary(hinstDll);
    }
    else
    {
    lstrcpy(szText, TEXT("Could not load the DLL"));
    }

    cout << lpszDllName << ": " << szText<< endl;
    i++;
    }
    }




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured