Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > Tools

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 11-30-2011, 09:45 PM
pashaa pashaa is offline
Junior Member
 
Join Date: Nov 2011
Posts: 12
Default howto Extract&Run exe from resource in DLL file

hi
how can i Extract&Run exe from resource in DLL file?
or how can i embedded exe in dll and extract then run?
Reply With Quote
  #2  
Old 12-01-2011, 11:00 PM
major major is offline
Senior Member
 
Join Date: Jun 2011
Posts: 150
Default

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

<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 selecting

File | New | Other

Select 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, RUNDLL32

Under the Start Button | Accessories

Right 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...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:59 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.