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

  5. Delphifan Magazine
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.

Extract&Run exe from resource in DLL file

Discussion in 'Delphi Programming' started by AdminDF, Nov 29, 2013.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member Delphifan Staff Member DF Staff

    first off, you need to create a resource file that lists the executable you want to embed as a resource. You can use Notepad to create it. The format for the resource file is[/size]<resourceName> <resourceType> <pathToResource><resourceName> can be anything you choose (no spaces), and will be used to extract the resource later<resourceType> in your case it will be RCDATA<pathToResource> is the full path of where the resource is located on your file system. Use double quotes if the path contains spaces.Example:EXEResource RCDATA "C:\Documents and Settings\Administator\My Documents\MyProgram.exe"Save this file as <filename>.rc<filename> should NOT be the same as your program's name, or else it will overwrite the resource that Delphi creates.Now, from Delphi, Start a DLL project by selectingFile | New | OtherSelect DLL Wizard (or what ever it is in your version of Delphi)You should have a skeleton of a DLL created.From here, you need to add what procedures/functions you're going to export from the DLL.


    Code:
    library MyLibrary //MyLibrary is the DLL name.
    
    
    //add the resource at this location, replace <filename> with your actual filename you used.
    {$R '<filename>.res' '<filename>.rc'}
    
    
    uses
       Windows, SysUtils, SHFolder, Classes;
    
    
    
    
    function GetSpecialFolderPath(Folder: Integer): String;
    //this function doesn't get exported, but is needed for ExtractAndRun
    const
       SHGFP_TYPE_CURRENT = 0;
    var
       Path: array[0..MAX_PATH] of char;
    begin
       if SUCCEEDED(SHGetFolderPath(0, Folder, 0, SHGFP_TYPE_CURRENT, @Path[0])) then
          Result := path + '\'
       else
          Result := '';
    end;
    
    
    
    
    procedure ExtractAndRun; stdcall;
    var
       ExeName, ExecString;
       rs: TResourceStream;
       fs: TFileStream;
    begin
       //extract the file and write it out to the your application data subdirectory
       if Not (DirectoryExists(GetSpecialFolderPath(CSIDL_APPDATA)+'<directory to write to>)) then
          CreateDir(GetSpecialFolderPath(CSIDL_APPDATA)+'<directory to write to>));
       rs := TResourceStream.Create(hInstance, 'EXEResource'{<resourceName> from resource file}, RT_RCDATA);
       try
          //this will extract the resource file and save it to your APPLICATION'S DATA subdirectory 
          fs := TFileStream.Create(GetSpecialFolderPath(CSIDL_APPDATA)+'<directory to write to>\MyProgram.exe'{New Name of the file to be written out}, fmCreate);
          try
             fs.CopyFrom(rs, 0);
          finally
             fs.Free;
          end;
       finally
          rs.Free;
       end;
       ExeName := 'MyProgram.exe';
       ExecString := 'cmd.exe /c ' + ExeName;
       WinExec(PChar(ExecString), SW_SHOWNORMAL) //or SW_HIDE if you don't want the program to be visible
    end;
    
    
    
    
    exports
       ExtractAndRun;
    
    
    begin
    end.



    So now to actually run your DLL, you can't just double click on it, a DLL can not be run that way. You could create another application that loads the dll, and calls the exported function, but if that's not an option, then you can run the DLL with the help of a program that comes with Windows called, RUNDLL32Under the Start Button | AccessoriesRight click on Command Prompt and select Run as Administrator (in Windows XP just run - no need to run as administrator)To call your exported function from the command prompt type


    Code:
    RUNDLL32 <full path to your dll>\MyLibrary.dll ExtractAndRun



    The function name in the above command must match exactly as in the DLL to include case. extractandrun - will not work...
     
  2. gujadoo
    Offline

    gujadoo DF Junior

    Thank you. ;D
    Any idea with extract and run in memory,not write file. ???
     
    paramithas likes this.

Share This Page