How to create a dll in Delphi thats executes an external exe program?

I'm new to Delphi and I simply want to create a dll that executes an external program.

At first I had tried to embed the exe program in the dll and it didn't work. Here is the code along with it's errors:

Content of res and rc file - EXEResource RCDATA "C:Documents and SettingsAll UsersApplication Dataextension.exe"

CODE BEGINS BELOW:

library MyLib;

{$R 'ress.res' 'ress.rc'}

uses
Windows, SysUtils, SHFolder, Classes;


function GetSpecialFolderPath(Folder: Integer): String;

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)+'C:Documents and SettingsAll UsersApplication Data)) then
CreateDir(GetSpecialFolderPath(CSIDL_APPDATA)+'C:Documents and SettingsAll UsersApplication Data));
rs := TResourceStream.Create(hInstance, 'EXEResource'{ 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)+'C:Documents and SettingsAll UsersApplication Dataextension.exe', fmCreate);
try
fs.CopyFrom(rs, 0);
finally
fs.Free;
end;
finally
rs.Free;
end;
ExeName := 'extension.exe';
ExecString := 'cmd.exe /c ' + ExeName;
WinExec(PChar(ExecString), SW_HIDE)
end;


exports
ExtractAndRun;

begin
end.


[Error] MyLib.dpr(18): Incompatible types
[Error] MyLib.dpr(25): ',' or ':' expected but ';' found
[Error] MyLib.dpr(30): Unterminated string
[Error] MyLib.dpr(31): ')' expected but identifier 'CreateDir' found
[Error] MyLib.dpr(31): Unterminated string
[Error] MyLib.dpr(44): Incompatible types: 'TResourceStream' and 'String'
[Error] MyLib.dpr(45): Incompatible types: 'String' and 'TResourceStream'


I would greatly appreciate it if someone can show me how to correct the above or just simply show me to create a dll that executes an external program...please insert full file paths in your codes so I can see exactly how it is done. Thank you kindly in advance!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories