如何获取DLL的版本?

时间:2012-11-25 15:38:02

标签: c++ winapi dll resources versioning

我正在从system32打开一个DLL(shell32.dll) 我想收到它的版本。 我该怎么做?我开始写它,但只是不知道如何继续:

我也看到有类似的函数:GetFileVersionSize,有没有办法使用它们?

这是代码,如果有人可以帮我继续并提供线索,我会真的认可它

谢谢!

#define PATH    "C:\\Users\\rachel\\Desktop\\shell32.dll"

PVERSION dll_getVersion(PCHAR pDllPath)
{
 PVERSION       version             =   NULL;
 HINSTANCE      dllLoad             =   NULL;
 HRSRC          resourceHandle      =   NULL;
 HGLOBAL            loadResourceHandle  =   NULL;
 LPVOID         lockResourceHande   =   NULL;
 DWORD          sizeOfResource      =   0;

 //LPCTSTR  lptstrFilename = NULL;
 //DWORD    dfHandle = 0;
 //DWORD dwLen = 0;
 //LPVOID   lpData = NULL;
 //BOOL test = FALSE;

 //DWORD fileVersionSize = 0;
 //unsigned long u = 0;
 //fileVersionSize = GetFileVersionInfoSize(PATH , &u);

 //test = GetFileVersionInfo(PATH, dfHandle , dwLen ,lpData);



if (NULL == pDllPath)
{
    printf("error #1 : dllPath is invalid \n");
    return version;
}

version = (PVERSION)calloc(1,sizeof(VERSION));
if (NULL == version)
{
    printf("the allocation failed \n");
    return version;
}

//opening the dll using the path */
dllLoad = LoadLibrary(pDllPath);
if (NULL == dllLoad)
{
    printf("failed to load the dll ! \n");
    printf("the last error is : %d\n" , GetLastError());
    free(version);
    version = NULL;
    return version;
}

resourceHandle          =    FindResource(dllLoad ,MAKEINTRESOURCE(16) , RT_VERSION);
if (NULL == resourceHandle)
{
    printf("problem with find resource!!!! \n");
    return NULL;
}
loadResourceHandle      =    LoadResource(dllLoad , resourceHandle);

if (NULL == loadResourceHandle)
{
    printf("problem with load resource function! \n");
    return NULL;
}

lockResourceHande = LockResource(loadResourceHandle);
if (NULL == lockResourceHande)
{
    printf("error in lock resource function \n");
    return NULL;
}

sizeOfResource = SizeofResource(dllLoad, resourceHandle);

2 个答案:

答案 0 :(得分:2)

Here,一个适合你的例子,希望这会有所帮助。调用GetFileInfoSize,分配该大小的缓冲区,然后传递分配给GetFileInfo的缓冲区,然后在VerQueryValue中使用传入GetFileInfo的初始化缓冲区。

答案 1 :(得分:1)

实际上有两种方法可以获得DLL的版本。许多系统DLL导出DllGetVersion()函数。对于没有的DLL,您必须回退到GetFileVersionInfo()及相关函数。以下文章向您展示了使用两者的示例:

Determining the version number of a DLL or Executable