Problem trying to delete a c# dll with Delphi 5 using COM

Asked

Viewed 132 times

4

I have a DLL that I use to perform digital signature of PDF’s created in c# (Assinaturadigitalpdf.dll), it in turn uses a dll that handles PDF files (itextsharp.dll), I load it into Delphi and run signatures smoothly.

I built those DLL’s into mine. EXE (used .res file for both), and I aim to extract them in Runtime when necessary, call the routines and then delete them. I managed to do this without problems, however at the time of deletion, I delete first the "itextsharp.dll" that goes smoothly and then try to delete the "Assinadigitalpdf.dll" which returns me error 5: Access Denied.

If I just load the dll and don’t call the "Assinarpdf" routine I can delete the file without problems.

I simplified the code to the maximum, leaving only a "Return = true" in the DLL and still not being able to delete, follow the simplified codes and with problem:

DLL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace AssinarPdfCom
{
    [ComVisible(true)]
    [Guid("CE805AD4-511E-4E63-A37F-9FF6C97D664B")]
    public class AssinaPdf 
    {
        [DllExport]
        public static bool AssinarPdf(string CaminhoPdfEntrada, string CaminhoPdfSaida)
        {
            return true;
        }
    }
}

Class responsible for calling DLL Delphi:

unit uAssinaPdf;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  DB, StdCtrls, SDEngine, FileCtrl, Dialogs;

type
  TAssinarPdfFunc = function (pdfPathIn, pdfPathOut: String): Boolean; stdcall;

type

  TAssinaPdf = class(TObject)
  private
    dllHandle: HWND;
  public
    function AssinarPdf(pdfPathIn, pdfPathOut: String): Boolean;
    constructor Create; overload;
    destructor Destroy; override;
  end;

implementation

function TAssinaPdf.AssinarPdf(pdfPathIn, pdfPathOut: String): Boolean;
var
  funcAssinaPdf: TAssinarPdfFunc;
begin
  @funcAssinaPdf := GetProcAddress(dllHandle, 'AssinarPdf');
  if @funcAssinaPdf = nil then
  begin
    result := False;
    exit;
  end;

  result := funcAssinaPdf(pdfPathIn, pdfPathOut);
  funcAssinaPdf := nil;
end;

constructor TAssinaPdf.Create;
begin
  inherited Create;

  dllHandle :=  LoadLibrary('c:\temp\AssinaturaDigitalPdf.dll');
end;

destructor TAssinaPdf.Destroy;
var
  hMod: HMODULE;
begin
  inherited Destroy;
  FreeLibrary(dllHandle);
  dllHandle := 0;
  hMod :=  GetModuleHandle('AssinaturaDigitalPdf.dll');
  FreeLibrary(hMod);

  if FileExists('c:\temp\itextsharp.dll') then
    if not DeleteFile('c:\temp\itextsharp.dll') then
      RaiseLastWin32Error; //aqui deleta normalmente
  if FileExists('c:\temp\AssinaturaDigitalPdf.dll') then
    if not DeleteFile('c:\temp\AssinaturaDigitalPdf.dll') then
      RaiseLastWin32Error; //aqui gera erro.

end;


end.

Event of the button that calls the routines:

procedure TfrmTeste.actAssinarDocumentoAnexadoExecute(Sender: TObject);
var
  pathIn, pathOut: String;
  AssinaPdf: TAssinaPdf;
begin
  inherited;

  pathIn := 'c:\in\teste.pdf';
  pathOut := 'c:\out\teste.pdf';

  AssinaPdf := TAssinaPdf.Create;
  try

    if not AssinaPdf.AssinarPdf(pathIn, pathOut) then
      ShowMessage('falha');

  finally
    FreeAndNil(AssinaPdf);
  end;

end;

The problem is not folder permission as the first DLL is deleted smoothly, and I can’t find any reference that might be crashing the deletion.

Note: I can’t delete the DLL or manually when the program is open.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.