【发布时间】:2016-07-10 00:16:07
【问题描述】:
在将旧的 32 位代码移植到 64 位时,我得到了
'Hook DLL', '无法映射文件'
我做错了什么?
var
hObjHandle: THandle; //Variable for the file mapping object
lpHookRec: PHookRec; //Pointer to our hook record
procedure MapFileMemory(dwAllocSize: DWord);
begin //MapFileMemory
//Create a process wide memory mapped variable
hObjHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, dwAllocSize, 'HookRecMemBlock');
if (hObjHandle = 0) then
begin
MessageBox(0, 'Hook DLL', 'Could not create file map object', mb_Ok);
exit
end;// (hObjHandle = 0)
//Get a pointer to our process wide memory mapped variable
lpHookRec := MapViewOfFile(hObjHandle, FILE_MAP_WRITE, 0, 0, dwAllocSize);
if (lpHookRec = nil) then
begin
CloseHandle(hObjHandle);
MessageBox(0, 'Hook DLL', 'Could not map file', mb_Ok);
exit
end //lpHookRec = Nil)
end; //MapFileMemory
procedure UnMapFileMemory;
begin //UnMapFileMemory
//Delete our process wide memory mapped variable
if (lpHookRec <> nil) then
begin
UnMapViewOfFile(lpHookRec);
lpHookRec := nil
end; // (lpHookRec <> Nil)
if (hObjHandle > 0) then
begin
CloseHandle(hObjHandle);
hObjHandle := 0
end //(hObjHandle > 0)
end; // UnMapFileMemory
procedure DllEntryPoint(dwReason: DWord);
begin { DllEntryPoint }
case dwReason of
Dll_Process_Attach:
begin
{if we are getting mapped into a process, then get}
{a pointer to our process wide memory mapped variable}
hObjHandle := 0;
lpHookRec := nil;
MapFileMemory(sizeof(lpHookRec^))
end;
Dll_Process_Detach:
begin
{if we are getting unmapped from a process then, remove}
{the pointer to our process wide memory mapped variable}
UnMapFileMemory
end;
end { case dwReason }
end; { DllEntryPoint }
【问题讨论】:
-
这是来自互联网的代码。
-
阅读文档。我敢打赌,在 64 位上 CreateFileMapping 的第一个参数不是 $ffffffff。通过 INVALID_HANDLE_VALUE。试着理解你的代码,而不是盲目地复制它
-
为什么要将项目更改为 64 位? x64 处理器也支持 32 位程序。 :-)
-
@Franciscocamilo,因为他想映射文件> 2GB。
-
@Franciscocamilo 实际上并非所有 64 位系统都可以运行 32 位程序。 Wow64 模拟器是运行 32 位应用程序所必需的,在某些 64 位版本的 Windows 上,该组件是可选并且可能未安装。