zoukankan      html  css  js  c++  java
  • 内存映射

    内存映射

    代码
    {*******************************************************}
    { }
    { Module Name: unitMemoryMap.pas }
    { Creation Date 2010-03-10 }
    { Copyright (C) 2010 Ming.z }
    { }
    {*******************************************************}
    unit unitMemoryMap;

    interface

    uses
    Windows, Messages, SysUtils, Classes;

    type
    PShareMem
    = ^TShareMem;
    TShareMem
    = packed record
    FHandle: HWND;
    FMsgBuf:
    string[50];
    FAction: Byte;
    end;

    procedure CreateMemoryMap;
    procedure DestoryMemoryMap;
    function OpenMap: Boolean;
    function CloseMap: Boolean;
    procedure ZeroMemoryMap;
    procedure WriteMapFile(const PMainHandle:HWND;const PMsg:String;PAction: Byte);

    var
    hMap: THandle;
    ShareRecord:TShareMem;
    PShare: PShareMem;
    const
    K_MapName
    = 'MsgReceptor';

    implementation

    procedure CreateMemoryMap;
    begin
    hMap:
    =CreateFileMapping($FFFFFFFF,
    nil,
    PAGE_READWRITE,
    0,
    SizeOf(TShareMem),
    K_MapName);

    if hMap=0 then
    raise Exception.Create('Error while create file')
    else
    PShare:
    =PShareMem(MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0,0));
    if PShare = nil then
    begin
    CloseHandle(hMap);
    end;
    end;

    procedure DestoryMemoryMap;
    begin
    if PShare <> nil then
    UnmapViewOfFile(PShare);
    if hMap<>0 then
    CloseHandle(hMap);
    end;

    function OpenMap: Boolean;
    begin
    Result :
    = False;
    hMap :
    = OpenFileMapping(FILE_MAP_WRITE,False,K_MapName);
    if hMap <> 0 then
    PShare :
    = MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0);
    if PShare <> nil then
    Result :
    = true;
    end;

    function CloseMap: Boolean;
    begin
    if PShare <> nil then UnmapViewOfFile(PShare);
    if hMap <> 0 then CloseHandle(hMap);
    Result :
    = true;
    end;

    procedure ZeroMemoryMap;
    begin
    FillMemory(PShare,SizeOf(TShareMem),
    0);
    end;

    procedure WriteMapFile(const PMainHandle:HWND;const PMsg:String;PAction: Byte);
    begin
    ShareRecord.FHandle :
    = PMainHandle;
    ShareRecord.FMsgBuf :
    = PMsg;
    ShareRecord.FAction :
    = PAction;
    CopyMemory(PShare,@ShareRecord,SizeOf(TShareMem));
    end;

    end.

  • 相关阅读:
    LVS负载均衡部署
    将源码包制作成rpm包
    root用户被提示:Operation not permitted
    varnish加速web
    优化nginx数据包头缓存
    NGINX并发量优化
    Python+API接口测试框架设计(pytest)
    python+API接口测试框架设计(unittest)
    python编程面试题
    Python + unittest知识点回顾
  • 原文地址:https://www.cnblogs.com/Jekhn/p/1917809.html
Copyright © 2011-2022 走看看