如何在Delphi中创建一个执行外部exe程序的DLL? [英] How to create a dll in Delphi thats executes an external exe program?

查看:85
本文介绍了如何在Delphi中创建一个执行外部exe程序的DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Delphi的新手,我只是想创建一个执行外部程序的dll。



起初我曾试图将exe程序嵌入这个DLL并没有用。以下是代码及其错误:



res和rc文件的内容 - EXEResource RCDATAC:\Documents and Settings \ Allll Users \ Application Data \ extension.exe



代码开头如下:

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'





如果有人能告诉我如何纠正上述内容或只是让我创建,我将不胜感激一个执行外部程序的DLL ...请在代码中插入完整的文件路径,以便我可以确切地看到它是如何完成的。请您提前感谢。



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.

推荐答案

R'ress.res''ress.rc'}

使用
Windows,SysUtils,SHFolder,Classes;


function GetSpecialFolderPath(Folder:Integer): String ;

const
SHGFP_TYPE_CURRENT = 0 ;
var
路径: array [ char ;> 0 .. MAX_PATH];
begin
if SUCCEEDED(SHGetFolderPath(0,Folder, 0 ,SHGFP_TYPE_CURRENT,@ Path [ 0 ]))然后
结果:=路径+ ' \ '
else
结果:='
' ;
结束;


程序ExtractAndRun; STDCALL;
var
ExeName,ExecString;
rs:TResourceStream;
fs:TFileStream;
begin
//解压缩文件并将其写入应用程序数据子目录
if Not(DirectoryExists(GetSpecialFolderPath(CSIDL_APPDATA)+'
C:\Documents 设置\所有用户\Application数据))然后
CreateDir(GetSpecialFolderPath(CSIDL_APPDATA)+ ' C:\Documents and Settings \All Users \ Application Data));
rs:= TResourceStream.Create(hInstance,'
EXEResource ' {< resourceName> from资源文件},RT_RCDATA);
try
//这将提取资源文件并将其保存到您的APPLICATION'
S DATA子目录
fs:= TFileStream.Create(GetSpecialFolderPath(CSIDL_APPDATA)+ ' C:\Documents and Settings \ All Users \ Application Data \ _extension.exe',fmCreate) ;
尝试
fs.CopyFrom(rs,0);
最后
fs.Free;
end ;
最后
rs.Free;
end ;
ExeName:= ' extension.exe';
ExecString:= ' cmd.exe / c' + ExeName;
WinExec(PChar(ExecString),SW_HIDE)
end ;


出口
ExtractAndRun;

开始
结束
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'





如果有人能告诉我如何纠正上述内容或只是让我创建,我将不胜感激一个执行外部程序的DLL ...请在代码中插入完整的文件路径,以便我可以确切地看到它是如何完成的。请您提前感谢。



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.


您应该学会阅读错误消息并更正他们为您识别的问题。

例如:



  • 第18行你有类型错误 - 你定义了结果
  • 在第25行,你在错误的地方有一个分号。
  • 在第30行,你有一个没有终止引号字符的字符串。
  • 等。

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.


这篇关于如何在Delphi中创建一个执行外部exe程序的DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆