unit dll_dyn_intf;

interface

Type
 T_GetGUID = Function (Filename: PChar; OUT GUID: PChar): HResult; stdcall;
 T_RegisterPlugin = Function (Filename: PChar): HResult; stdcall;
 T_UnRegisterPlugIn = Function (Filename: PChar): HResult; stdcall;

Function dotNET_GetGUID(PathToDLL: String; Filename: PChar; OUT GUID: PChar): HResult;
Function dotNET_RegisterPlugin(PathToDLL: String; Filename: PChar): HResult;
Function dotNET_UnregisterPlugin(PathToDLL: String; Filename: PChar): HResult;

Const
 DLLName = 'DotNetPluginManager.dll';

implementation

Uses windows, lpl_sysutils, sysutils;

Function CheckForDLL(PathToDLL: String): Boolean;
Var
 S: String;
begin
 S := PathToOk(pathToDLL) + DLLName;
 Result := Fileexists(S);
end;

Function dotNET_GetGUID(PathToDLL: String; Filename: PChar; OUT GUID: PChar): HResult;
Var
 TheFunc: T_GetGUID;
 Lib: THandle;
Begin
 Result := E_FAIL;
 If Not CheckForDLL(PathToDLL) then begin
  exit;
 end;
 Lib := LoadLibrary(Pchar(PathToOk(pathToDLL) + DLLName));
 If Lib = 0 then begin
  exit;
 end;
 @TheFunc := GetProcAddress(Lib, 'GetGUID');
 If @TheFunc = nil then begin
  FreeLibrary(Lib);
  exit;
 end;
 Try
  Result := TheFunc(Filename, GUID);
 Finally
  FreeLibrary(Lib);
 End;
End;

Function dotNET_RegisterPlugin(PathToDLL: String; Filename: PChar): HResult;
Var
 TheFunc: T_RegisterPlugin;
 Lib: THandle;
Begin
 Result := E_FAIL;
 If Not CheckForDLL(PathToDLL) then begin
  exit;
 end;
 Lib := LoadLibrary(Pchar(PathToOk(pathToDLL) + DLLName));
 If Lib = 0 then begin
  exit;
 end;
 @TheFunc := GetProcAddress(Lib, 'GetGUID');
 If @TheFunc = nil then begin
  FreeLibrary(Lib);
  exit;
 end;
 Try
  Result := TheFunc(Filename);
 Finally
  FreeLibrary(Lib);
 End;
End;

Function dotNET_UnregisterPlugin(PathToDLL: String; Filename: PChar): HResult;
Var
 TheFunc: T_UnRegisterPlugIn;
 Lib: THandle;
Begin
 Result := E_FAIL;
 If Not CheckForDLL(PathToDLL) then begin
  exit;
 end;
 Lib := LoadLibrary(Pchar(PathToOk(pathToDLL) + DLLName));
 If Lib = 0 then begin
  exit;
 end;
 @TheFunc := GetProcAddress(Lib, 'GetGUID');
 If @TheFunc = nil then begin
  FreeLibrary(Lib);
  exit;
 end;
 Try
  Result := TheFunc(Filename);
 Finally
  FreeLibrary(Lib);
 End;
end;

end.
