Click to See Complete Forum and Search --> : Get Dll Version


DarK|cE
February 23rd, 2000, 09:29 AM
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.

Sesha
February 23rd, 2000, 12:35 PM
You may use GetFileVersionInfo() API to read version info.

DarK|cE
February 23rd, 2000, 01:27 PM
thanx for your answer but do you have a little example on how to use the GetFileVersionInfo() ?

Sesha
February 23rd, 2000, 02:07 PM
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]);

}

Kailash Marthi
February 23rd, 2000, 02:15 PM
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++;
}
}