【问题标题】:how to get the Version of a DLL?如何获取 DLL 的版本?
【发布时间】:2012-11-25 15:38:02
【问题描述】:

我正在从 system32 (shell32.dll) 打开一个 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);

【问题讨论】:

  • 出于好奇,为什么需要你打开的dll的版本号?

标签: c++ winapi dll resources versioning


【解决方案1】:

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

【讨论】:

  • 谢谢!如何使用 GetFileInfoSize ?我看到参数是:In LPCTSTR lptstrFilename, Out_opt LPDWORD lpdwHandle ...意思是第一个参数是dll的路径,第二个是什么?
  • 函数运行时 LPDWORD 设置为零,它是可选的,所以我只需将 NULL 传递给它。
【解决方案2】:

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

Determining the version number of a DLL or Executable

【讨论】:

  • (耳语)有不少,看你想怎么做,你也可以通过shell com接口... LOL。
猜你喜欢
  • 2011-08-26
  • 2015-07-01
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 2010-12-17
相关资源
最近更新 更多