To umieść w pliku *.dpr
var
han, FileMapping: THandle;
t: String;
begin
han:= OpenMutex(MUTEX_ALL_ACCESS, true, 'Mutex jakis sobie tam');
if han = 0 then begin
han:= CreateMutex(nil, true, 'Mutex jakis sobie tam');
if han = ERROR_ALREADY_EXISTS then begin
MessageBox(0, '', '', 0);
CloseHandle(han);
Exit;
end;
FileMapping:= CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, MemoryFileSize, MemoryFileName);
if FileMapping <> 0 then begin
ViewOfFile:= MapViewOfFile(FileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
ReleaseMutex(han);
CloseHandle(han);
UnmapViewOfFile(ViewOfFile);
CloseHandle(FileMapping);
end else begin
//MessageBox(0, 'Inna aplikacja urzywa tego samego mutexa', '', 0);
FileMapping:= OpenFileMapping(FILE_MAP_WRITE, True, MemoryFileName);
if FileMapping = 0 then
Exit;
ViewOfFile:= MapViewOfFile(FileMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
t:= 'Te dane zostaną przekazane do aplikacji która jest już uruchomiona';
Move(PChar(t)^, ViewOfFile^, Length(t));
UnmapViewOfFile(ViewOfFile);
CloseHandle(FileMapping);
PostMessage(HWND_BROADCAST, WM_MESSAGE, 0, 0);
CloseHandle(han);
end;
a to przed implementation w module z definicją głównego okna aplikacji
const
MemoryFileSize = 4 * 1024;
MemoryFileName = 'D9287E19-6F9E-45fa-897C-D392F73A0F2F';
var
WM_MESSAGE: Integer = WM_USER + 19;
var
ViewOfFile: Pointer;
w tym samum module dopisz
Application.OnMessage:= FF;
.
.
.
procedure TForm1.FF(var Msg: TMsg; var Handled: Boolean);
var
i: String;
begin
if MSg.message = WM_MESSAGE then begin
i:= StrPas(PChar(ViewOfFile));
Memo1.Lines.Add(i);
end;
end;
oraz
initialization
WM_MESSAGE:= RegisterWindowMessage('{70BCC26C-CBF2-4364-B988-90F46C3D269C}');