Einzelnen Beitrag anzeigen

TheSledgeHammer

Registriert seit: 22. Mai 2019
Ort: Mulfingen
43 Beiträge
 
Delphi 10.3 Rio
 
#5

AW: Mutex-Handling in DLL für zwei Programme

  Alt 22. Mai 2019, 14:19
Minimales Beispielprojekt zum Nachvollziehen?
Ich werde mal die Methoden hier rein schreiben, die notwendig wären, das wird so 1:1 nicht laufen.

Delphi-Quellcode:

TMyDllProc = procedure() of object; stdcall;

TExampleClass = class(TThread)
  private
    FHandleDLL: THandle;
    // methods from DLL
    FRegister: TMyDllProc;
    FUnRegister: TMyDllProc;
    FAcquire: TMyDllProc;
    FRelease: TMyDllProc;

    function InitMutexDLL(): boolean;
    procedure DeInitMutexDLL();
    procedure ExecuteMutex();

    procedure Execute(); override;
end;

// ...

function TExampleClass.InitMutexDLL(): boolean;
begin
 
  Result := false;
  FHandleDLL := LoadLibrary(PChar('MutexDLL.dll'));

  if FHandleDLL <> INVALID_HANDLE_VALUE then
  begin

    @FRegister := GetProcAddress(FHandleDLL, 'RegisterMutex');
    @FAcquire := GetProcAddress(FHandleDLL, 'EnterMutex');
    @FRelease := GetProcAddress(FHandleDLL, 'ReleaseMutex');
    @FUnregister := GetProcAddress(FHandleDLL, 'UnRegisterMutex');

    Result := Assigned(FRegister) and Assigned(FAcquire) and Assigned(FRelease) and Assigned(FUnregister);

    if Result then
      FRegister();

  end;

end;

// ...

procedure TExampleClass.DeInitMutexDLL();
begin

  if FHandleDLL <> INVALID_HANDLE_VALUE then
  begin
    FUnRegister();
    FreeLibrary(FHandleDLL);
  end;

end;

// ...

procedure TExampleClass.ExecuteMutex();
begin

  try
    FAcquire();
    // access INI-File
  finally
    FRelease();
  end;

end;

// ...

procedure TExampleClass.Execute();
begin
  InitMutexDLL();
  while not Terminated do
  begin
    ExecuteMutex();
    Sleep(100)
  end;
  DeInitMutexDLL();
end;
Tobias

Geändert von TheSledgeHammer (22. Mai 2019 um 14:24 Uhr)
  Mit Zitat antworten Zitat