我正在尝试使用 Fiddle 将一个非常简单的 C Win32 API 调用转换为 Ruby,但并没有取得太大的成功。
前两个方法调用完美无缺,但是当我进行填充结构的调用时,我遇到了一些困难,因为看起来结构正在填充垃圾数据:
这是相关的C代码:
DWORD dwHandle, dwLen;
LPVOID lpData;
UINT BufLen;
VS_FIXEDFILEINFO *pFileInfo;
LPTSTR LibName = L"C:\\windows\\notepad.exe";
dwLen = GetFileVersionInfoSize(LibName, &dwHandle);
if(dwLen != 0)
{
lpData = malloc(dwLen);
if (GetFileVersionInfo(LibName, NULL, &dwLen, lpData))
{
if (VerQueryValue(lpData, L"\\", (LPVOID)&pFileInfo, (PUINT)&BufLen))
{
printf("ProductVersion: %d.%d.%d.%d\n", HIWORD(pFileInfo->dwProductVersionMS), LOWORD(pFileInfo->dwProductVersionLS), HIWORD(pFileInfo->dwFileVersionLS), LOWORD(pFileInfo->dwFileVersionLS));
printf("FileVersion: %d.%d.%d.%d\n", HIWORD(pFileInfo->dwFileVersionMS), LOWORD(pFileInfo->dwFileVersionMS), HIWORD(pFileInfo->dwFileVersionLS), LOWORD(pFileInfo->dwFileVersionLS));
}
}
}
这是我将 C 转换为 Ruby 的尝试:
require 'fiddle/import'
module Version
extend Fiddle::Importer
dlload 'version'
typealias 'HANDLE', 'void*'
typealias 'LPVOID', 'void*'
typealias 'LPCVOID', 'const void*'
typealias 'LPDWORD', 'int*'
typealias 'HWND', 'HANDLE'
typealias 'LPCSTR', 'const char*'
typealias 'LPCWSTR', 'const wchar_t*'
typealias 'UINT', 'unsigned int'
typealias 'PUINT', 'unsigned int*'
typealias 'DWORD', 'unsigned long'
typealias 'BOOL', 'int'
extern 'DWORD GetFileVersionInfoSize(LPCSTR, LPDWORD)'
extern 'BOOL GetFileVersionInfo(LPCSTR, DWORD, DWORD, LPVOID)'
extern 'BOOL VerQueryValue(LPCVOID, LPCSTR, LPVOID, PUINT)'
VS_FIXEDFILEINFO = struct [
'DWORD dwSignature',
'DWORD dwStrucVersion',
'DWORD dwFileVersionMS',
'DWORD dwFileVersionLS',
'DWORD dwProductVersionMS',
'DWORD dwProductVersionLS',
'DWORD dwFileFlagsMask',
'DWORD dwFileFlags',
'DWORD dwFileOS',
'DWORD dwFileType',
'DWORD dwFileSubtype',
'DWORD dwFileDateMS',
'DWORD dwFileDateLS'
]
end
dwHandle = Fiddle::NULL
dwLen = Version::GetFileVersionInfoSize "C:\\windows\\system32\\notepad.exe", dwHandle
lpData = Fiddle::Pointer.malloc(dwLen)
Version::GetFileVersionInfo "C:\\windows\\notepad.exe", 0, dwLen, lpData
BufLen = Fiddle::NULL
pFileInfo = Version::VS_FIXEDFILEINFO.malloc()
success = Version::VerQueryValue lpData, "\\", pFileInfo, BufLen.ref
puts "pFileInfo: #{pFileInfo.dwSignature}"
不幸的是,pFileInfo.dwSignature
似乎每次都在变化,表明它被垃圾数据填充(奇怪的是它在VerQueryValue
调用之前为零)。该pFileInfo.dwSignature
值应始终为0xFEEF04BD
或 十进制4277077181
。
对这里发生的事情有任何想法吗?
谢谢!