Delphi与Qt在Windows下使用共享内存进程间通信

Delphi部分


type
  TGuardInfo=record
    Lock: Integer;
  end;
  PGuardInfo = ^TGuardInfo;

TGuardShareMem=class
  private
    FHandle: THandle;
    FGuardInfo: PGuardInfo;
  public
    constructor Create;
    destructor Destroy; override;
    function GetGuardInfo: PGuardInfo;
  end;

{ TGuardShareMem }

constructor TGuardShareMem.Create;
begin
  FHandle := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0,
    SizeOf(TGuardInfo), PChar('guardinfo_2013'));
  FGuardInfo := MapViewOfFile(FHandle, FILE_MAP_WRITE or FILE_MAP_READ, 0, 0,
    sizeof(TGuardInfo));
  FGuardInfo.Lock := 0;
end;

destructor TGuardShareMem.Destroy;
begin
  UnmapViewOfFile(FGuardInfo);
  CloseHandle(FHandle);
  inherited;
end;

Qt部分


struct GuardInfo
{
    qint32 lock;
};

bool lock()
{
    QSharedMemory sharedMemory;
    sharedMemory.setNativeKey("guardinfo_2013");
    if (!sharedMemory.attach())
    {
        qDebug()<<"atttach fail";
        return false;
    }
    GuardInfo *p= (GuardInfo *)sharedMemory.constData();
    qDebug()<<p->lock;
    p->lock = 212;
    return true;
}

上一篇:Python学习笔记四


下一篇:Makefile中的特殊宏定义以及实用选项