BOOLEAN MapSharedMemory(PPKT_BUFFER PktBuffer)
{
if(!PktBuffer->BufferMdl)
return FALSE;
//
// The preferred V5 way to map the buffer into user space
//
PktBuffer->UserBaseAddress = MmMapLockedPagesSpecifyCache(PktBuffer->BufferMdl, // MDL
UserMode, // Mode
MmCached, // Caching
NULL, // Address
FALSE, // Bugcheck?
NormalPagePriority); // Priority
if(!PktBuffer->UserBaseAddress)
return FALSE;
return TRUE;
}
相关结构:
typedef struct _PKT_BUFFER
{
PMDL BufferMdl;
PVOID UserBaseAddress;
PVOID KernelBaseAddress;
}PKT_BUFFER, *PPKT_BUFFER;
相关说明:
驱动运行在IRQL=2级,驱动获得事件对像后像不停的给应用程序信号,不停地速度很快地向应用程序发送。
应用程序:
TThreadPara=packed record
ctrlcode:integer;
ListItem:TListItem;
end;
procedure TFireWallFrm.BtnMonitorAllStartClick(Sender: TObject);
var
para:TThreadPara;
id:dword;
begin
para.ctrlcode:=MONITOR_INFO;
para.ListItem:=self.LVMonitorAll.Items.Add;
threadhandle:=CreateThread(nil,0,@GetMonitorInfo,@para,0,id);
end;
function GetMonitorInfo(var para:TThreadPara):dword;stdcall;
var
pinfo:PMonitorMessage;
pSend:PULONG;
begin
if MainFrm.bFWStart then
begin
if eventhandle=0 then
begin
eventhandle:=CreateEvent(nil,false,false,nil);
GetMem(pSend,sizeof(Thandle));
Copymemory(pSend,@eventhandle,sizeof(eventhandle));
MainFrm.IpWall.WriteIo(GET_EVENT,pSend,sizeof(eventhandle));
freemem(pSend);
end;
Getmem(pinfo,sizeof(TMonitorMessage));
while(true) do
begin
WaitForSingleObject(eventhandle, INFINITE);
MainFrm.IpWall.ReadIo(para.ctrlcode,@pinfo,sizeof(pointer));
showmessage(inttostr(ULONG (pinfo)));//这里显示的地址是UserBaseAddress一样的
para.ListItem.SubItems.Add(iptostring(pinfo^.ip));//程序运行到这出现访问内存错误,我想应该是共享内存不对,可是又想不出问题,因为DBG显示的UserBaseAddress和我用showmessage显示的一样
para.ListItem.SubItems.Add(inttostr(htons(pinfo^.port)));
case pinfo.protocol of
6:para.ListItem.SubItems.Add('TCP');
17:para.ListItem.SubItems.Add('UDP');
1:para.ListItem.SubItems.Add('ICMP');
end;
if pinfo^.send then
para.ListItem.SubItems.Add('发送')
else
para.ListItem.SubItems.Add('接收');
para.ListItem.SubItems.Add(inttostr(pinfo^.size));
end;
end;
end;