I have a DLL that I use to make PDF's digital signature created in C # (Digital SignaturePdf.dll), it in turn uses a dll that handles PDF files (itextsharp.dll), I load it into Delphi and execute signatures without problems .
I've embedded these dlls in my .EXE (I used .res file for both), and I aim to extract them in runtime when necessary, call the routines and then delete them. I was able to do this without problems, but at the time of deletion, I first delete the "itextsharp.dll" which goes without problems and in the sequence I try to delete the "DigitalDataPdf.dll" which returns me error 5: Access denied.
If I just load the dll and do not call the routine "SignPdf" I can delete the file without any problems.
I simplified the code to the maximum, leaving only a "return = true" in the DLL and I still can not delete it, it follows the simplified and problem codes:
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 the DLL call 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.
Button event calling 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 without any problems, and I can not find any references that might be blocking the deletion.
Note: I can not delete the DLL or manually when the program is open.