Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

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.

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 Administrator 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. ???
     

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