Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi MapFileMemory(dwAllocSize:DWord)';挂钩DLL'';无法映射文件';_Delphi_Winapi_64 Bit - Fatal编程技术网

Delphi MapFileMemory(dwAllocSize:DWord)';挂钩DLL'';无法映射文件';

Delphi MapFileMemory(dwAllocSize:DWord)';挂钩DLL'';无法映射文件';,delphi,winapi,64-bit,Delphi,Winapi,64 Bit,在将旧的32位代码移植到64位时,我得到了 “挂钩DLL”,“无法映射文件” 我做错了什么? var hObjHandle:THandle//文件映射对象的变量 lpHookRec:PHookRec//指向钩子记录的指针 过程MapFileMemory(dwAllocSize:DWord); 开始//映射文件内存 //创建进程范围内存映射变量 hObjHandle:=CreateFileMapping($ffffffffff,nil,PAGE_READWRITE,0,dwAllocSize,'H

在将旧的32位代码移植到64位时,我得到了

“挂钩DLL”,“无法映射文件”

我做错了什么?
var
hObjHandle:THandle//文件映射对象的变量
lpHookRec:PHookRec//指向钩子记录的指针
过程MapFileMemory(dwAllocSize:DWord);
开始//映射文件内存
//创建进程范围内存映射变量
hObjHandle:=CreateFileMapping($ffffffffff,nil,PAGE_READWRITE,0,dwAllocSize,'HookRecMemBlock');
如果(hObjHandle=0),则
开始
MessageBox(0,“挂钩DLL”,“无法创建文件映射对象”,mb_Ok);
出口
完(hObjHandle=0)
//获取指向进程范围内存映射变量的指针
lpHookRec:=MapViewOfFile(hObjHandle,文件映射写入,0,0,dwAllocSize);
如果(lpHookRec=nil),则
开始
闭合手柄(hObjHandle);
MessageBox(0,“挂钩DLL”,“无法映射文件”,mb_Ok);
出口
结束//lpHookRec=Nil)
结束//MapFileMemory
过程记忆;
开始//取消文件内存
//删除进程范围内存映射变量
如果(lpHookRec nil),则
开始
未经批准的文件(lpHookRec);
lpHookRec:=nil
完(lpHookRec无)
如果(hObjHandle>0),则
开始
闭合手柄(hObjHandle);
hObjHandle:=0
结束/(hObjHandle>0)
完不匹配存储器
程序DllEntryPoint(DWord原因:DWord);
开始{DllEntryPoint}
案由
Dll\进程\附加:
开始
{如果我们正在映射到一个进程,那么获取}
{指向进程范围内存映射变量的指针}
hObjHandle:=0;
lpHookRec:=零;
MapFileMemory(sizeof(lpHookRec^))
结束;
Dll\u进程\u分离:
开始
{如果我们正在从进程中取消映射,请删除}
{指向进程范围内存映射变量的指针}
不匹配存储器
结束;
结束{case dwReason}
结束;{DllEntryPoint}

您需要进行相关更改,以允许您在64位上运行

哦,读一下陈雷蒙的,还有。
您确实不应该在
DllMain
中映射内存映射文件
相反,您应该等到dll客户端开始调用dll中的任何工作例程,然后才进行映射

最重要的是,如果您正在调试,那么最好先检查一下

var
  hObjHandle: THandle; //Variable for the file mapping object
  lpHookRec: PHookRec; //Pointer to our hook record

procedure MapFileMemory(dwAllocSize: NativeUInt);
begin
  if Assigned(lpHookRec) then exit;
  hObjHandle := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, dwAllocSize, 'HookRecMemBlock');
  if (hObjHandle = 0) then begin
    MessageBox(0, 'Hook DLL', 'Could not create file map object '#10#13+
               SysErrorMessage(GetLastError), mb_Ok);
    exit;
  end;

  //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 '#10#13+
               SysErrorMessage(GetLastError), mb_Ok);
    exit;
  end; 
end;

procedure UnMapFileMemory;
begin 
  //Delete our process wide memory mapped variable
  if Assigned(lpHookRec) then begin
    UnMapViewOfFile(lpHookRec);
    lpHookRec := nil;
  end; 
  if (hObjHandle <> 0) then begin
    CloseHandle(hObjHandle);
    hObjHandle := 0;
  end; 
end;

var
  CS: TRTLCriticalSection;    

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;
      //do not call MapMemFile here, do it when your work-routine is called for the first time.
    end;
    Dll_Process_Detach: begin
      {if we are getting unmapped from a process then, remove}
      {the pointer to our process wide memory mapped variable}
      EnterCriticalSection(CS);
        if Assigned(lpHookRec) then UnMapFileMemory;
      LeaveCriticalSection(CS);
    end;
  end; { case dwReason }
end; { DllEntryPoint }
var
hObjHandle:THandle//文件映射对象的变量
lpHookRec:PHookRec//指向钩子记录的指针
过程MapFileMemory(dwAllocSize:NativeUInt);
开始
如果已分配(lpHookRec),则退出;
hObjHandle:=CreateFileMapping(无效的句柄值,nil,PAGE,READWRITE,0,dwAllocSize,'HookRecMemBlock');
如果(hObjHandle=0),则开始
MessageBox(0,'Hook DLL','无法创建文件映射对象'#10#13+
SysErrorMessage(GetLastError),mb_Ok);
出口
结束;
//获取指向进程范围内存映射变量的指针
lpHookRec:=MapViewOfFile(hObjHandle,文件映射写入,0,0,dwAllocSize);
如果(lpHookRec=nil),则开始
闭合手柄(hObjHandle);
MessageBox(0,'Hook DLL','无法映射文件'#10#13+
SysErrorMessage(GetLastError),mb_Ok);
出口
结束;
结束;
过程记忆;
开始
//删除进程范围内存映射变量
如果已分配(lpHookRec),则开始
未经批准的文件(lpHookRec);
lpHookRec:=零;
结束;
如果(hObjHandle 0),则开始
闭合手柄(hObjHandle);
hObjHandle:=0;
结束;
结束;
变量
CS:TRTLCriticalSection;
程序DllEntryPoint(DWord原因:DWord);
开始{DllEntryPoint}
案由
Dll\进程\附加:开始
{如果我们正在映射到一个进程,那么获取}
{指向进程范围内存映射变量的指针}
hObjHandle:=0;
lpHookRec:=零;
//不要在这里调用MapMemFile,在第一次调用您的工作例程时执行此操作。
结束;
Dll\u进程\u分离:开始
{如果我们正在从进程中取消映射,请删除}
{指向进程范围内存映射变量的指针}
肠危重科;
如果已分配(lpHookRec),则取消分配内存;
离境管制组(CS);
结束;
结束;{case dwReason}
结束;{DllEntryPoint}

这是来自internet的代码。请阅读文档。我打赌在64位上,CreateFileMapping的第一个参数不是$FFFFFF。传递无效的\u句柄\u值。试着理解你的代码而不是盲目地复制它为什么你想把你的项目改成64位?x64处理器也支持32位程序。:-)@Franciscocamilo,因为他想映射大于2GB的文件。@Franciscocamilo实际上并非所有64位系统都能运行32位程序。Wow64 emulator需要运行32位应用程序,在某些64位版本的Windows上,该组件是可选的,可能未安装。除了调用
MessageBox
,其他所有操作都可以从
DllMain
安全执行。这在.Wow下得到了明确的解释,但是,1)我无法添加SysErrorMessage(GetLastError)函数来了解实际错误(可能是,我需要包含一些单元,请告知)2)如果我在DllMain中没有包含MapFileMemory(sizeof(lpHookRec^),我如何确定“首次使用”并运行该函数?
var
  hObjHandle: THandle; //Variable for the file mapping object
  lpHookRec: PHookRec; //Pointer to our hook record

procedure MapFileMemory(dwAllocSize: NativeUInt);
begin
  if Assigned(lpHookRec) then exit;
  hObjHandle := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, dwAllocSize, 'HookRecMemBlock');
  if (hObjHandle = 0) then begin
    MessageBox(0, 'Hook DLL', 'Could not create file map object '#10#13+
               SysErrorMessage(GetLastError), mb_Ok);
    exit;
  end;

  //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 '#10#13+
               SysErrorMessage(GetLastError), mb_Ok);
    exit;
  end; 
end;

procedure UnMapFileMemory;
begin 
  //Delete our process wide memory mapped variable
  if Assigned(lpHookRec) then begin
    UnMapViewOfFile(lpHookRec);
    lpHookRec := nil;
  end; 
  if (hObjHandle <> 0) then begin
    CloseHandle(hObjHandle);
    hObjHandle := 0;
  end; 
end;

var
  CS: TRTLCriticalSection;    

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;
      //do not call MapMemFile here, do it when your work-routine is called for the first time.
    end;
    Dll_Process_Detach: begin
      {if we are getting unmapped from a process then, remove}
      {the pointer to our process wide memory mapped variable}
      EnterCriticalSection(CS);
        if Assigned(lpHookRec) then UnMapFileMemory;
      LeaveCriticalSection(CS);
    end;
  end; { case dwReason }
end; { DllEntryPoint }