如此共享内存怎么不行呢???(20分)

  • 主题发起人 主题发起人 wp231957
  • 开始时间 开始时间
W

wp231957

Unregistered / Unconfirmed
GUEST, unregistred user!
loadlibrary('hookapi.dll');<br> &nbsp;hMapObj := OpenFileMapping(FILE_MAP_WRITE,{获取完全访问映射文件}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; False,{不可继承的}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LPCTSTR(MappingFileName));{映射文件名字}<br> &nbsp;if hMapObj = 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;ShowMessage('不能定位内存映射文件块!');<br> &nbsp; &nbsp;Halt;<br> &nbsp;end;<br><br> &nbsp;pShMem := MapViewOfFile(hMapObj,FILE_MAP_WRITE,0,0,0);<br> &nbsp;if pShMem = nil then<br> &nbsp;begin<br> &nbsp; &nbsp;ShowMessage('映射文件错误'+ IntToStr(GetLastError));<br> &nbsp; &nbsp;CloseHandle(hMapObj);<br> &nbsp; &nbsp;Halt;<br> &nbsp;end;<br><br> &nbsp;FillChar(pShMem^, SizeOf(TShareMem), 0);<br> &nbsp;pShMem^.hProcWnd := Self.Handle;<br> &nbsp;fWndClosed:=false;<br>procedure TForm1.Button6Click(Sender: TObject);<br>begin<br> &nbsp;showmessage(inttostr(pshmem^.ftimerid )); &nbsp;//这里总是0 &nbsp;我已经在DLL中为其赋值为1<br><br>end;<br><br>//以下为DLL代码:<br>initialization<br> &nbsp; &nbsp;hMappingFile := OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);<br> &nbsp; &nbsp;if hMappingFile=0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;hMappingFile := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareMem),MappingFileName);<br> &nbsp; &nbsp; &nbsp;FirstProcess:=true; {这是第一个进程,即主程序}<br> &nbsp; &nbsp;end &nbsp;else FirstProcess:=false;<br> &nbsp; &nbsp;if hMappingFile=0 then Exception.Create('不能建立共享内存!');<br> &nbsp; &nbsp;pShMem := &nbsp;MapViewOfFile(hMappingFile,FILE_MAP_WRITE or FILE_MAP_READ,0,0,0);<br> &nbsp; &nbsp;if pShMem = nil then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;CloseHandle(hMappingFile);<br> &nbsp; &nbsp; &nbsp;Exception.Create('不能映射共享内存!');<br> &nbsp; end;<br> &nbsp; if FirstProcess then<br> &nbsp; begin<br> &nbsp; &nbsp; pShMem^.bCanSpyNow:=false;<br><br> &nbsp; end;<br> &nbsp; pshmem^.ftimerid:=1; &nbsp; //这里已经赋值了,为什么在主程序中始终为0呢<br>finalization<br> &nbsp;UnMapViewOfFile(pShMem); {取消映射视图}<br> &nbsp;CloseHandle(hMappingFile); {关闭映射文件句柄}
 
//以下为DLL代码:<br>initialization<br> &nbsp;ShowMessage('Initialize ok');//加上这一行<br> &nbsp; &nbsp;hMappingFile := OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);<br><br>这样如果是正确初始化了,会有显示窗口的,我怀疑是没有执行初始化部分
 
后退
顶部