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;
}