5种运行程序的方法具体应用实例(带参数)

{  
    代码名   : 5种运行程序的方法具体应用实例(带参数)
    说明     : 无
    作者     : JJony
    QQ       : 254706028
    博客     : http://blog.csdn.net/jzj_jony
    空间     : http://jonychen,ys168.com
    测试环境 : WinXPSP2
    声明     : 您可以任意转载,但请注明文章作者和出处
}

5种运行程序的方法具体应用实例(带参数)

在各论坛经常看到有人问怎么在程序中执行另一个程序,下面我就介绍5种比较常用的
运行程序的方法,包括WinExec、ShellExecute、CreateProcess、CreateProcessAsUser、
CreateProcessWithLogonW,这5种方法前3种用的比较多,后2种较少,正好趁此机会
介绍给大家,多一条路就多一个机会吗:)

//以下为完整源代码
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,shellapi;
const
  cmd='C:/WINDOWS/system32/cmd.exe';//要运行的程序
  para='/?';                        //参数
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//CreateProcess运行程序
procedure TForm1.Button3Click(Sender: TObject);
var
  StartUpInfo: TStartUpInfo;
  ProcessInfo: TProcessInformation;
begin

  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  with StartupInfo do
  begin
    cb := SizeOf(TStartupInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := SW_SHOWNORMAL;
  end;
  CreateProcess(PChar(cmd),          //程序名
                pchar(para),         //参数
                nil,
                nil,
                False,
                NORMAL_PRIORITY_CLASS,
                nil,
                nil,
                StartupInfo,
                ProcessInfo);


end;
//WinExec运行程序
procedure TForm1.Button1Click(Sender: TObject);
begin
  winexec(pchar(cmd+' '+para),SW_SHOWNORMAL);  //命令行+参数
end;
//shellexecute运行程序,需要添加shellapi单元
procedure TForm1.Button2Click(Sender: TObject);
begin
  shellexecute(handle,
               'open',
               pchar(cmd),    //程序名
               pchar(para),   //参数
               nil,           //默认文件夹
               SW_SHOWNORMAL);//正常显示
end;

//CreateProcessAsUser运行程序
procedure TForm1.Button4Click(Sender: TObject);
var
 hToken:thandle;
 ph:thandle;
 si:STARTUPINFO;
 pi:PROCESS_INFORMATION;
begin
 ph:=openprocess(PROCESS_ALL_ACCESS ,
                 false,
                 GetCurrentProcessID());
 if ph<=0 then exit;
 openprocesstoken( ph,TOKEN_ALL_ACCESS,hToken); //去当前进程Token等同于取当前帐户Token
 try
  ZeroMemory( @si, sizeof( STARTUPINFO ) );
  si.cb := sizeof( STARTUPINFO );
  Si.lpDesktop := PChar('Winsta0/Default');
  si.wShowWindow:=SW_SHOWNORMAL;
  CreateProcessAsUser(hToken,
                      pchar(cmd) ,       //程序名
                      pchar(para),       //参数
                      nil,
                      nil,
                      FALSE,
                      CREATE_DEFAULT_ERROR_MODE, //NORMAL_PRIORITY_CLASS or

CREATE_NEW_CONSOLE,
                      nil,
                      nil,
                      si,
                      pi );
 finally
  closehandle(ph);
 end;
end;

//以下为CreateProcessWithLogonW常量、结构、函数定义
const
  LOGON_WITH_PROFILE         = $00000001;
  LOGON_NETCREDENTIALS_ONLY  = $00000002;
  LOGON_ZERO_PASSWORD_BUFFER = DWORD($80000000);
type
LPSTARTUPINFOW = ^STARTUPINFOW;
  _STARTUPINFOW = record
    cb: DWORD;
    lpReserved: LPWSTR;
    lpDesktop: LPWSTR;
    lpTitle: LPWSTR;
    dwX: DWORD;
    dwY: DWORD;
    dwXSize: DWORD;
    dwYSize: DWORD;
    dwXCountChars: DWORD;
    dwYCountChars: DWORD;
    dwFillAttribute: DWORD;
    dwFlags: DWORD;
    wShowWindow: WORD;
    cbReserved2: WORD;
    lpReserved2: ^Byte;
    hStdInput: THANDLE;
    hStdOutput: THANDLE;
    hStdError: THANDLE;
  end;
  STARTUPINFOW = _STARTUPINFOW;
  TStartupInfoW = STARTUPINFOW;
  PStartupInfoW = LPSTARTUPINFOW;
 
function CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword: LPCWSTR;
                                 dwLogonFlags: DWORD;
                                 lpApplicationName: LPCWSTR;
                                 lpCommandLine: LPWSTR;
                                 dwCreationFlags: DWORD;
                                 lpEnvironment: pointer;
                                 lpCurrentDirectory: LPCWSTR;
                                 const lpStartupInfo: STARTUPINFOW;
                                 var lpProcessInformation: PROCESS_INFORMATION): BOOL;

stdcall;external 'ADVAPI32.dll';

//结束定义
//CreateProcessWithLogonW运行程序
procedure TForm1.Button5Click(Sender: TObject);
var
  si: StartupInfoW;
  pif: PROCESS_INFORMATION;
  spath:array[0..MAX_PATH] of widechar;
begin
  if not fileexists(cmd) then exit;
  stringtowidechar(cmd+' '+para,@spath,sizeof(spath));
  si.cb := SizeOf(startupinfoW);
  si.dwFlags  := STARTF_USESHOWWINDOW;
  si.wShowWindow := SW_SHOWDEFAULT;
  si.lpReserved := nil;
  si.lpDesktop := nil;
  si.lpTitle := 'JJony';
  CreateProcessWithLogonW('test',       //用户名,必须是已存在的帐户
                           nil,        //域名,不在域中为空
                           '123456', //密码,一定要和指定帐户对应哦
                           LOGON_WITH_PROFILE,
                           nil,        //程序名
                           @spath,     //命令行+' '+参数
                           CREATE_DEFAULT_ERROR_MODE,
                           nil,
                           nil,
                           si,
                           pif);
end;
end. 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值