Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Embedded DLL Delphi
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:
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 27-Aug-13 19:24pm
Edited 27-Aug-13 22:31pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.150301.1 | Last Updated 28 Aug 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100