1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Donation with Paypal!!!

    Go to your paypal account and send directly donation to [email protected]

    1 month - 10 $ - Standart VIP

    6 months - 20 $- Standart VIP

    1 year - 30 $- Standart VIP

    2 years - 50 $- Standart VIP

    Gold member for life - 150 $- Standart VIP

    High Vip (Standart VIP include) group please send PM or email to [email protected] for info

    After Donation please send email to [email protected]

  3. Donation Ways 2020


    Paysend
  4. Telegram
Dismiss Notice

Donation with Paypal!!!

Go to your paypal account and send directly donation to [email protected]

1 month - 10 $ - Standart VIP

6 months - 20 $- Standart VIP

1 year - 30 $- Standart VIP

2 years - 50 $- Standart VIP

Gold member for life - 150 $- Standart VIP

High Vip (Standart VIP include) group please send PM or email to [email protected] for info

After Donation please send email to [email protected]

Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

DLL usage in code dinamically, without any decleration.

Discussion in 'Delphi Programming' started by mrmarman, Dec 15, 2013.

  1. mrmarman
    Offline

    mrmarman DF Member

    Hi DelphiFan(s) :)

    - @R3n4m3 described; how to extract any stored DLL from an EXE's resource, before threads... This is a good solution for not to separate any project with belonging DLL. Keep together on a file.

    - I want to explain that; (@R3n4m3 used filestream) a little different method to extract and how to use this DLL dynamically, after extraction.

    Example : We have a DLL and it exports a function like below :

    Project and DLL Source with Executables link...

    [​IMG]

    Sample DLL Project Source (MyDLL_SumAndMultiply.dll)
    Code:
    library MyDLL_SumAndMultiply;
    
    uses
      SysUtils,
      Classes;
    
    {$R *.res}
    
    function MySUMFunction( iParam01, iParam02 : Integer ):Integer; stdcall;
    begin
      Result := iParam01 + iParam02;
    end;
    
    function MyMULTFunction( iParam01, iParam02 : Integer ):Integer; stdcall;
    begin
      Result := iParam01 * iParam02;
    end;
    
    Exports
      MySUMFunction,
      MyMULTFunction;
    
    begin
    end.
    Resource File (RES.RES)

    Code:
    MyDLLRes RCDATA "..\DLLProject\MyDLL_SumAndMultiply.dll"
    Sample Project Source

    Code:
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    // We embedded the DLL by Resource below...
    {$R RES.RES}
    
    type // describe DLL function(s)/procedure(s) under type
      TMySUMFunction  = function ( iParam01, iParam02 : Integer ):Integer; stdcall; //MySUMFunction
      TMyMULTFunction = function ( iParam01, iParam02 : Integer ):Integer; stdcall; //MyMULTFunction
    
    
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
      SumFunc : TMySUMFunction;
      Res     : TResourceStream;
      dllHand : THandle;
      strDLL  : String;
    begin
    // --- Sum Button --- //
      strDLL := ExtractFilePath(Application.Exename) + 'NewNameDLL.DLL';
      Res := TResourceStream.Create( hInstance, 'MyDLLRes', RT_RCDATA);
        Res.SaveToFile( strDLL );
      Res.Free;
    
      dllHand := LoadLibrary( PChar( strDLL ) ) ;
      if dllHand <> 0 then
      begin
        @SumFunc := GetProcAddress(dllHand, 'MySUMFunction') ;
        if Assigned (SumFunc) then
        begin
          Spinedit3.Value := SumFunc( SpinEdit1.Value, SpinEdit2.Value );
        end
        else MessageDlg( 'The function/procedure does not exists !', mtError, [mbOk], 0);
        FreeLibrary(dllHand) ;
      end
      else MessageDlg( 'DLL does not exists or load error. !', mtError, [mbOk], 0);
      if FileExists( strDLL ) then DeleteFile( strDLL );
    end;
    
    procedure TForm1.BitBtn2Click(Sender: TObject);
    var
      MultFunc: TMyMULTFunction;
      Res     : TResourceStream;
      dllHand : THandle;
      strDLL  : String;
    begin
    // --- Multiply Button --- //
      strDLL := ExtractFilePath(Application.Exename) + 'NewNameDLL.DLL';
      Res := TResourceStream.Create( hInstance, 'MyDLLRes', RT_RCDATA);
        Res.SaveToFile( strDLL );
      Res.Free;
    
      dllHand := LoadLibrary( PChar( strDLL ) ) ;
      if dllHand <> 0 then
      begin
        @MultFunc := GetProcAddress(dllHand, 'MyMULTFunction') ;
        if Assigned (MultFunc) then
        begin
          Spinedit4.Value := MultFunc( SpinEdit1.Value, SpinEdit2.Value );
        end
        else MessageDlg( 'The function/procedure does not exists !', mtError, [mbOk], 0);
        FreeLibrary(dllHand) ;
      end
      else MessageDlg( 'DLL does not exists or load error. !', mtError, [mbOk], 0);
      if FileExists( strDLL ) then DeleteFile( strDLL );
    end;
    
    end.
     
  2. AdminDF
    Online

    AdminDFAdminDF is a Verified Member Delphifan Staff Member DF Staff

    Thank you very good tutor
     
  3. Proof
    Offline

    Proof Full Stack web developer engineer

    Thank you, very nice exemple  ;)
     
  4. Axel2014
    Offline

    Axel2014 DF Junior

    RE:

    Good tutor. Thanks
     
  5. scater
    Offline

    scater DF Junior

    thank you
     
  6. oscarchavez
    Offline

    oscarchavez DF Junior

    I can't download the file
     
  7. RcXXcR
    Online

    RcXXcR Guest

    RE:

    thanks alot :D
     
  8. peyman1
    Online

    peyman1 Guest

    RE:

    thanks
     
  9. Prof_Ho2
    Offline

    Prof_Ho2 DF Member

    Thank you very helpful info
     

Share This Page