Für diejenigen, die keine Komponente verwenden wollen, hier, eine Konsolenanwendung für Demozwecke:
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE}
uses
Windows;
var
hFile: THandle;
Instances: Integer = 0;
pData: PInteger;
const
lpFilename: PChar = '
ProjFileSize';
begin
hFile := OpenFileMapping( FILE_MAP_READ
or FILE_MAP_WRITE, True, lpFilename );
if hFile = ERROR
then
hFile := CreateFileMapping( INVALID_HANDLE_VALUE,
NIL, PAGE_READWRITE, 0, 4, lpFilename );
if hFile <> ERROR
then
try
pData := MapViewOfFile( hFile, FILE_MAP_READ
or FILE_MAP_WRITE, 0, 0, 4 );
if Assigned( pData )
then
begin
try
Instances := pData^;
Writeln( Instances, '
instances have already been existing; increasing it...' );
inc( Instances );
pData^ := Instances;
Writeln( '
Increased and updated!' );
Write( '
Instances = ', Instances, '
(press return to enter the terminating-process)' );
Readln;
Instances := pData^;
Writeln( '
Terminating... decreasing instances' );
dec( Instances );
Writeln( '
Instances = ', Instances );
pData^ := Instances;
Writeln( '
Decreased and updated!' );
finally
UnmapViewOfFile( pData );
end;
Write( '
Press any key to exit...' );
Readln;
end;
finally
CloseHandle( hFile );
end;
end.
Führe diese Konsolenanwendung einfach mehrmals hintereinander aus, ohne die vorherigen Instanzen zu schließen, um die Instanzenzählung zu sehen!
Have fun!