Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi 7, Add a dll to application directory on dropping a component

i am developing a a component in delphi 7 and delphi 2006,component uses a .pas (Not mine )file which requires a DLL file to be present in the application directory.

It is possible to embed the DLL file into the component, so that when the user drops it on the form or create it at run time the DLL will be placed in the application directory?

currently

1) i am telling the user to place DLL file in the application directory.

2) Add the DLL file in the Resources , so that on create, i can drop the DLL into the application directory? from delphidabbler_embed_resource. This i have done using

   {Drop the Resource..!!!}

     procedure DropDllToApplicationDirectOry(applicationPath : string);
     var
           RS: TResourceStream;
     begin
           // Create resource stream
          RS := TResourceStream.CreateFromID(HInstance, 100, RT_RCDATA);
     try
         //  applicationPath : example  c:\MyTestProject Lee\
        if DirectoryExists(applicationPath) then         RS.SaveToFile(applicationPath+'myDllFileWhichIsNeeded.dll')
     finally
     // Free the stream
     RS.Free;
    end;
  end;

this DropDllToApplicationDirectOry take the resource from the {$RmyDllFileWhichIsNeeded.dll.RES} and drope to the location but

how do i call DropDllToApplicationDirectOry this when i drop the component on the Form?

i tried initialization of the component but DLL is not copied so i get the error enter image description here

EDIT For RXControls's TRxClock when we drop the clock runs on this form, the clock begins to run(show the curent time)... so i tried this

 constructor Tmycomponeny.Create(AOwner: TComponent);
   begin
      inherited Create(AOwner);
      {add dll}
      DropDllToApplicationDirectOry(ExtractFilePath(Application.ExeName));
  end;

But this doesnt work..

The code OF RXControls

  constructor TRxClock.Create(AOwner: TComponent);
  begin
    inherited Create(AOwner);
    if not Registered then begin
     ClockInit;
    Registered := True;
  end;
   Caption := TimeToStr(Time);
   ControlStyle := ControlStyle - [csSetCaption] 
   {$IFDEF WIN32} - [csReplicatable] {$ENDIF};
   BevelInner := bvLowered;
   BevelOuter := bvRaised;
   FTimer := TRxTimer.Create(Self);
   FTimer.Interval := 450; { every second }
   FTimer.OnTimer := TimerExpired;
   FDotsColor := clTeal;
   FShowSeconds := True;
   FLeadingZero := True;
   GetTime(FDisplayTime);
   if FDisplayTime.Hour >= 12 then Dec(FDisplayTime.Hour, 12);
   FAlarmWait := True;
  FAlarm := EncodeTime(0, 0, 0, 0);
 end;

enter image description here

like image 230
PresleyDias Avatar asked Dec 13 '22 02:12

PresleyDias


2 Answers

This idea is not going to work, in general. You are assuming that the developer will always drop your component onto a form. But they could just as well check an existing project out from revision control and then the DLL would not be created.

In my opinion you should simply document the dependency and let the developer ensure that the dependency is met. Suggest that they add the DLL to their revision control system so that it is checked out into the application directory.

The developer is going to need to be aware of this dependency when it comes to deployment. That has to be the developer's responsibility. So it is just cleaner and easier to let the developer manage this full stop.

like image 167
David Heffernan Avatar answered Dec 24 '22 02:12

David Heffernan


Although David is correct that documenting is probably the only reliable approach, your question is an interesting one.

You seem to have written the code to write the dll to a file so the problem is responding to an event that will be fired when the component is created?

If I was to do this I think I would check on component creation that the dll exists and if not create it at this stage if not. Is it possible for you to load the dll dynamically? If you can only use the dll linked statically, then David's way is really the only reliable way.

like image 36
Toby Allen Avatar answered Dec 24 '22 02:12

Toby Allen