Try it (in .lpr) :
Some misbehavior with multiform apps
{ you can add units after this }, Unit1, Unit2,
windows;
const
MemFileSize = 1024;
MemFileName = 'one_inst_TST';
var
MemHnd, MainFormHnd : HWND; // DWord;
FFileView: Pointer;
begin
MemHnd := CreateFileMapping(HWND($FFFFFFFF),nil, PAGE_READWRITE, 0, MemFileSize, MemFileName);
if GetLastError<>ERROR_ALREADY_EXISTS then begin
FFileView:=nil;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
MainFormHnd:= Form1.Handle;
// Write Handle of MainForm to the file
FFileView := MapViewOfFile(MemHnd,FILE_MAP_ALL_ACCESS,0,0,0);
if FFileView <> nil then begin
CopyMemory( FFileView, @MainFormHnd, 4);
UnmapViewOfFile(FFileView);
end;
Application.CreateForm(TForm2, Form2);
Application.Run;
end
else begin
// Read Handle of MainForm from the file created by previous instance
FFileView := MapViewOfFile(MemHnd,FILE_MAP_READ,0,0,0);
CopyMemory( @MainFormHnd, FFileView, 4);
SetForegroundWindow(MainFormHnd); // activate previous
end;
CloseHandle(MemHnd);
end.