CreateProcessWithLogon函数调用另一个程序 但另一个程序带参数 ( 积分: 50 )

  • 主题发起人 主题发起人 tsedlinux
  • 开始时间 开始时间
T

tsedlinux

Unregistered / Unconfirmed
GUEST, unregistred user!
wUsername := WideString(EDIT1.Text) ;
wDomain := '';
wPassword := WideString(EDIT2.Text) ;
wApplicationName:= 'c:/kvolself/BTOW'; 。。。。。。。。此处
pwUsername := Addr(wUsername[1]);
pwDomain := Addr(wDomain[1]);
pwPassword := Addr(wPassword[1]);
pwApplicationName := Addr(wApplicationName[1]);

FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
StartupInfo.cb := SizeOf(TStartupInfo);
if not CreateProcessWithLogon(pwUsername,pwDomain,pwPassword,LOGON_WITH_PROFILE,
pwApplicationName,nil,CREATE_DEFAULT_ERROR_MODE,
nil,nil,StartupInfo,ProcessInfo) then
RaiseLastOSError;

待执行程序有一个参数 如'c:/kvolself/BTOW' 这样执行时程序函数报找不到指定的路径 ,如果把它的参数取消 如c:/kvolself 就可以执行
它的参数在这个函数中应该怎么调用呢?
 
lpApplicationName是可执行文件名字.lpCommandLine是命令行参数.
如果lpApplicationName是空的话,那么将会把第一个参数作为可执行文件执行.所有带参数执行的话有两种调用方式.
我给你两种调用方式的的例子,都是调用记事本打开C:/boot.ini文件的

const
LOGON_WITH_PROFILE = $00000001;
LOGON_NETCREDENTIALS_ONLY = $00000002;

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';

procedure TForm1.Button1Click(Sender: TObject);
var
wUsername, wDomain, wPassword, wApplicationName, wCommandLine : WideString;
pwUsername, pwDomain, pwPassword, pwApplicationName, pwCommandLine : PWCHAR;
StartupInfo : STARTUPINFOW;
ProcessInfo : PROCESS_INFORMATION;
begin
wUsername := 'WANGRUI';
wDomain := '';
wPassword := 'wr';
wApplicationName := 'c:/windows/notepad.exe';
wCommandLine := ' c:/boot.ini';
pwUsername := PWCHAR(wUsername);
pwDomain := PWCHAR(wDomain);
pwPassword := PWCHAR(wPassword);
pwApplicationName := PWCHAR(wApplicationName);
pwCommandLine := PWCHAR(wCommandLine);

FillChar(StartupInfo, SizeOf(STARTUPINFOW), 0);
StartupInfo.cb := SizeOf(STARTUPINFOW);
if not CreateProcessWithLogonW(pwUsername,
pwDomain,
pwPassword,
LOGON_WITH_PROFILE,
pwApplicationName,
pwCommandLine,
CREATE_DEFAULT_ERROR_MODE,
nil,
nil,
StartupInfo,
ProcessInfo) then
RaiseLastOSError;

end;

procedure TForm1.Button2Click(Sender: TObject);
var
wUsername, wDomain, wPassword, wCommandLine : WideString;
pwUsername, pwDomain, pwPassword, pwCommandLine : PWCHAR;
StartupInfo : STARTUPINFOW;
ProcessInfo : PROCESS_INFORMATION;
begin
wUsername := 'WANGRUI';
wDomain := '';
wPassword := 'wr';
wCommandLine := 'c:/windows/notepad.exe c:/boot.ini';
pwUsername := PWCHAR(wUsername);
pwDomain := PWCHAR(wDomain);
pwPassword := PWCHAR(wPassword);
pwCommandLine := PWCHAR(wCommandLine);

FillChar(StartupInfo, SizeOf(STARTUPINFOW), 0);
StartupInfo.cb := SizeOf(STARTUPINFOW);
if not CreateProcessWithLogonW(pwUsername,
pwDomain,
pwPassword,
LOGON_WITH_PROFILE,
nil,
pwCommandLine,
CREATE_DEFAULT_ERROR_MODE,
nil,
nil,
StartupInfo,
ProcessInfo) then
RaiseLastOSError;

end;
 
老大的 STARTUPINFOW是什么东西啊?
我用的D6
 
是宽字符版本的StartInfo啊.Windows单元声明的.和你用Delphi的版本没关系.
CreateProcessWithLogon的真正导出函数名是CreateProcessWithLogonW,
是宽字符函数.所以要用STARTUPINFOW而不是STARTUPINFOA,或STARTUPINFO.
那些是ANSI函数的.
能用了就给分吧
 
我正在试呢 一定会给分的
为什么它报undeclared identifier:"startupinfow"
未定义? 怎么定义它?
 
在Windows单元里面有.
type
PStartupInfoA = ^TStartupInfoA;
PStartupInfoW = ^TStartupInfoW;
PStartupInfo = PStartupInfoA;
_STARTUPINFOA = record
cb: DWORD;
lpReserved: PAnsiChar;
lpDesktop: PAnsiChar;
lpTitle: PAnsiChar;
dwX: DWORD;
dwY: DWORD;
dwXSize: DWORD;
dwYSize: DWORD;
dwXCountChars: DWORD;
dwYCountChars: DWORD;
dwFillAttribute: DWORD;
dwFlags: DWORD;
wShowWindow: Word;
cbReserved2: Word;
lpReserved2: PByte;
hStdInput: THandle;
hStdOutput: THandle;
hStdError: THandle;
end;
_STARTUPINFOW = record
cb: DWORD;
lpReserved: PWideChar;
lpDesktop: PWideChar;
lpTitle: PWideChar;
dwX: DWORD;
dwY: DWORD;
dwXSize: DWORD;
dwYSize: DWORD;
dwXCountChars: DWORD;
dwYCountChars: DWORD;
dwFillAttribute: DWORD;
dwFlags: DWORD;
wShowWindow: Word;
cbReserved2: Word;
lpReserved2: PByte;
hStdInput: THandle;
hStdOutput: THandle;
hStdError: THandle;
end;
_STARTUPINFO = _STARTUPINFOA;
TStartupInfoA = _STARTUPINFOA;
TStartupInfoW = _STARTUPINFOW;
TStartupInfo = TStartupInfoA;
{$EXTERNALSYM STARTUPINFOA}
STARTUPINFOA = _STARTUPINFOA;
{$EXTERNALSYM STARTUPINFOW}
STARTUPINFOW = _STARTUPINFOW;
{$EXTERNALSYM STARTUPINFO}
STARTUPINFO = STARTUPINFOA;
 
很奇怪 我这就是编不过去
不过说过了就要算数的 先给分
如果兄弟有时间就把你的这个项目发个MAIL给我吧 我再试试
threadobject@163.com
 
接受答案了.
 
发给你了
 
我怎么还没收到邮件呢?
再试试这个吧 tsedlinux@sina.com
 
后退
顶部