Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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 Settings\All Users\Application Data\extension.exe"

CODE BEGINS BELOW:
Delphi
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 Settings\All Users\Application Data)) then
      CreateDir(GetSpecialFolderPath(CSIDL_APPDATA)+'C:\Documents and Settings\All Users\Application Data));
   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)+'C:\Documents and Settings\All Users\Application Data\extension.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.
Posted
Updated 27-Aug-13 21:31pm
v2

1 solution

You should learn to read the error messages and correct the issues they are identifying for you.
For example:

  • at line 18 you have a type error - have you defined Result?
  • at line 25 you have a semi-colon in the wrong place.
  • at line 30 you have a string that does not have a terminating quote character.
  • etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900