Hallo,
also folgendes Problem:
Ich habe mehrere Programme, die die gleiche
DLL-Funktion nutzen. Diese Funktion soll aber nur einmal laufen.
Da dachte ich, ich kann in der
DLL-Funktion mit CreateSemaphore ein Semaphore "registrieren", dass von der anderen Anwendungen mit "OpenSemaphore" erkannt wird und sobald die
Dll-Funktion beendet ist, soll das Semaphore wieder entfernt werden, dass scheint aber nicht zu funktionieren.
Woran liegt das?
Delphi-Quellcode:
const
SEMAPHORE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED
or SYNCHRONIZE
or 3;
var
Form1: TForm1;
Semaphore: THandle;
procedure TForm1.Button1Click(Sender: TObject);
begin
Semaphore:= CreateSemaphore(
nil, 0,1,'
TestName');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
{ When calling OpenSemaphore, you use the first parameter to identify the
type of access that you want for the semaphore. Most developers pass the
value SEMAPHORE_ALL_ACCESS.
Use the 2nd parameter to identify whether or not a process created by this
process can inherit the handle to the mutex. If you pass True, a process
created through a call to CreateProcess can inherit the mutex handle,
otherwise it cannot.
The 3rd parameter is the name of the semaphore. As is the case with mutexes,
this name must be unique.
If OpenSemaphore is successful, it returns the handle of the semaphore.
If OpenSemaphore fails, it returns 0.
If OpenSemaphore returns 0, use GetLastError to determine the cause of the failure. }
if OpenSemaphore(SEMAPHORE_ALL_ACCESS, true,'
TestName')<>0
then
ShowMessage ('
Schon offen');
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if Semaphore<>0
then begin
ShowMessage ('
Rel.');
{ The 1st argument is the handle to the semaphore, and
the 2nd is the number of counts to add to the semaphore. A thread should
only increment the semaphore's count equal to the number of counts held
by that thread. Normally, this will be equal to 1.
The 3rd parameter is a pointer to 32-bit variable that can be assigned
the count of the semaphore prior to the release. If you do not need to
know the semaphore's previous count, pass nil in this third parameter.
All of the semaphore related functions and constants discussed in this
section are declared in the Windows unit. Consequently, this unit must
appear in the uses clause of any code that needs to use semaphores. }
ReleaseSemaphore(Semaphore, 1, @Semaphore);
end;
end;
Danke Tim