我正在尝试使用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
。
对这里发生了什么有任何想法?
谢谢!