Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
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
    Online

    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. N0body
    Online

    N0bodyN0body is a Verified Member DelphiFan Administrator Staff Member DF Staff

    Thank you very good tutor
     
  3. Proof
    Online

    Proof DF Junior

    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
    Offline

    RcXXcR Guest

    RE:

    thanks alot :D
     
  8. peyman1
    Offline

    peyman1 Guest

    RE:

    thanks
     
  9. Prof_Ho2
    Offline

    Prof_Ho2 DF Member

    Thank you very helpful info
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block