5种运行程序的方法具体应用实例


本文整理自网络,侵删。

 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.

相关阅读 >>

Delphi webbrowser1 保存文档为 .mht

Delphi 泛型,存放n张图片

Delphi xe 遍历指定数据库,清空各表记录

Delphi 弹窗显示sql字符串

Delphi百度地图经纬度与地址互转

Delphi integer.tryparse

Delphi实现阿里云印刷文字识别_身份证识别

Delphi ttabcontrol在tabitem添加关闭按钮

Delphi 在内存中直接运行exe类型的资源文件

Delphi 62进制转10进制

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...