简体   繁体   中英

Understanding VerQueryValue

On MSDN I noticed the following for the VerQueryValue function:

lplpBuffer [out]
LPVOID
When this method returns, contains the address of a pointer to the requested version information in the buffer pointed to by pBlock. The memory pointed to by lplpBuffer is freed when the associated pBlock memory is freed._

How does the system know when pBlock is freed since pBlock is allocated by the caller?

I'm using the following code:

UINT reqSize = ::GetSystemDirectoryW(nullptr, 1);

std::vector<wchar_t> winDirectory (reqSize, 0);

UINT retVal = ::GetSystemDirectoryW(&winDirectory[0], reqSize);

std::wstring filePath(winDirectory.begin(), winDirectory.end()-1);

filePath.append(L"\\kernel32.dll");

DWORD bufSize = ::GetFileVersionInfoSizeW(
    filePath.c_str(),
    nullptr);

std::vector<BYTE> fileInfo (bufSize, 0);

::GetFileVersionInfoW(
    filePath.c_str(),
    0,
    bufSize,
    &fileInfo[0]);

UINT size = 0;

VS_FIXEDFILEINFO * ptr = nullptr;

BOOL error = ::VerQueryValueW(
    &fileInfo[0],
    L"\\",
    reinterpret_cast<LPVOID*>(&ptr),
    &size);

VerQueryValue返回指向您分配的初始内存块内某处的指针(GetFileVersionInfoSize返回一个足够大的块的大小,以包含整个版本资源+ ansi到unicode转换所需的任何空间等)

At least in some occasions, VerQueryValue performs conversion of the version data (eg Unicode to ASCII conversion when Unicode version of GetFileVersionInfo , but ASCII version of VerQueryValue are used). GetFileVersionInfoSize obviously computes the buffer size large enough to hold converted data.

GetFileVersionInfo copies data into the supplied buffer. As the format of this data isn't readily available/documented, you need to use the helper function VerQueryValue to retrieve pointers to specific entries within the buffer GetFileVersionInfo filled in.

The way MS documented that "The pointer returned by VerQueryValue isn't allocated from anywhere - it's just pointing to somewhere within another buffer" is somewhat confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM