Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2005 Mar : CreateProcessWithLogon
| Subject: | CreateProcessWithLogon |
| Posted by: | "Jim Muir" (jpmuirnospamplease@adelphia.net) |
| Date: | Sat, 19 Mar 2005 07:18:51 |
I picked up this fine codesnippit from Peter Below from a previous
posting. It works wonderfully. But I tried to modify the function so
that I could pass some parameters. Unfortunately when I run it with the
additional parameter CommandLine (string) it still works but the
executable that is run doesn't find the parameter I send in CommandLine.
Here is the code I use to execute the external program:
procedure TWPMergeForm.RzBitBtn2Click(Sender: TObject);
var S, Name, Ext: string;
begin
GetNameAndExt(ExtractFileName(ParamStr(0)),name,ext);
s:= '\Interval-prime\C-Drive Interval-Prime\IBHiS\IBH Common Exchange
(I-Drive)\IBH-iS\Bin\AddWPTemplate.exe ';
//MessageDlg(s+' '+name, mtWarning, [mbOK], 0);
If RunAsUser(s, 'MyDomain', 'Me', 'MyComplexPassword', name )then
MessageDlg('Success', mtInformation, [mbOK], 0);
end;
function RunAsUser(const Filename, Domain, Username, Password,
CommandLine: string):
Boolean;
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
wFilename, wDomain, wUsername, wPassword, wCommandline: PWideChar;
begin
FillChar (StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_SHOWNORMAL;
GetMem(wFilename, Length(Filename) * SizeOf(WideChar) +
SizeOf(WideChar));
GetMem(wDomain, Length(Domain) * SizeOf(WideChar) +
SizeOf(WideChar));
GetMem(wUsername, length(Username) * SizeOf(WideChar) +
SizeOf(WideChar));
GetMem(wPassword, length(Password) * SizeOf(WideChar) +
SizeOf(WideChar));
GetMem(wCommandline, length(CommandLine) * SizeOf(WideChar) +
SizeOf(WideChar));
StringToWideChar(Filename, wFilename, Length(Filename) *
SizeOf(WideChar) + SizeOf(WideChar));
StringToWideChar(Domain, wDomain, Length(Domain) *
SizeOf(WideChar) + SizeOf(WideChar));
StringToWideChar(Username, wUsername, Length(Username) *
SizeOf(WideChar) + SizeOf(WideChar));
StringToWideChar(Password, wPassword, Length(Password) *
SizeOf(WideChar) + SizeOf(WideChar));
StringToWideChar(CommandLine, wCommandline, Length(Commandline) *
SizeOf(WideChar) + SizeOf(WideChar));
//function defined elsewhere
{ function CreateProcessWithLogonW(
lpUsername,
lpDomain,
lpPassword:PWideChar;
dwLogonFlags:dword;
lpApplicationName: PWideChar;
lpCommandLine: PWideChar;
dwCreationFlags: DWORD;
lpEnvironment: Pointer;
lpCurrentDirectory: PWideChar;
const lpStartupInfo: tSTARTUPINFO;
var lpProcessInformation: TProcessInformation
): BOOL; stdcall; external 'advapi32.dll';
}
Result := CreateProcessWithLogonW(
wUsername,
wDomain,
wPassword,
0,
wFilename,
wCommandline,
0,
nil,
nil,
StartupInfo,
ProcessInfo);
if Result then begin
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end else
RaiseLastOSError;
FreeMem(wFilename);
FreeMem(wDomain);
FreeMem(wUsername);
FreeMem(wPassword);
FreeMem(wCommandline);
end;
In the external program should not the ParamStr(1) contain the value of
the variable "name" that I sent to the runas function ?
I looked at the Windows SDK help and it appears that if the
lpAppicationName and lpCommandline are both used, that this should work.
What am I Missing?
Jim Muir