I want to be able to create a process using CreateProcess()
and at a certain point, I'll suspend it and set a new command line and then
resume it....Is that possible?
-elias
http://eassoft.cjb.net
I don't know, but I don't understand why you'd want to do that. Since
command lines don't generally change, why would a program ever process its
command line more than once? If your ultimate goal is just to change the
behavior of the program, there are several other well-established methods of
inter-process communication available. Describe what it is you're really
trying to accomplish by changing the command line.
--Rob
-elias
"Rob Kennedy" <rike...@students.wisc.edu> wrote in message
news:3aefbf46$2_1@dnews...
> True, the application only processes it's command line only once,
> but i want to createprocess in suspended mode w/o passing command line
> params first and then SetCommandLine.
> and then resume it.
If you create a process in suspend mode, then a process is not initialize.
Use a pipe for standart input instead.
--
WBR, LVT.
"Leonid Troyanovsky" <lv...@eco-pro.ru> wrote in message
news:3AEFCDD7...@eco-pro.ru...
> Never used pipes....
> Will they allow me to SetCommandLine() when the process is executed?
No, they only allow you to pass parameters into a child process.
--
WBR, LVT.
> Never used pipes....
This exapmle of redirection standart _output_ of child process to pipe:
program childRD;
uses
Windows,
SysUtils,
Dialogs,
uchildRD in 'uchildRD.pas';
var
s : String;
begin
s := InputBox('cmd','command','cmd /c echo Hi');
ShowMessage(ExecRD(s,2000));
end.
--
WBR, LVT.
-------------------------------uchildRD.pas-------------------------------
unit uchildRD;
interface
uses
Windows,
SysUtils;
function ExecRD (const cmdline : String; Timeout :Dword):String;
implementation
type
ThreadParams = record
hReadPipe : THandle;
s : String;
end;
PThreadParams = ^ThreadParams;
function ThreadRead(Params : Pointer):Dword; stdcall;
var
Info : PThreadParams;
Buffer : array [0..4095] of Char;
nb: DWord;
i: Longint;
begin
Result := 0;
Info := PThreadParams(Params);
while ReadFile( Info^.hReadPipe,
buffer,
SizeOf(buffer),
nb,
nil) do
begin
if nb = 0 then
Break;
for i:=0 to nb-1 do
if buffer[i] = #10 then
Info^.s := Info^.s + #13#10
else
if buffer[i] <> #13 then
Info^.s := Info^.s + buffer[i];
end;
end;
function OEMToStr(const OEM : String):String;
begin
SetLength(Result, Length(OEM));
OEMToChar(@OEM[1], @Result[1]);
end;
function ExecRD;
var
hReadPipe,
hWritePipe: THandle;
saPipe: TSecurityAttributes;
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
Params : PThreadParams;
ReaderID : Dword;
ReaderHandle : THandle;
begin
Result := '';
Params := nil;
ReaderHandle := 0;
saPipe.bInheritHandle := true;
saPipe.lpSecurityDescriptor := nil;
saPipe.nLength := SizeOf(TSecurityAttributes);
if not CreatePipe(hReadPipe,hWritePipe,@saPipe,0) then
RaiseLastWin32Error;
FillChar(StartInfo,SizeOf(StartInfo),0);
StartInfo.dwFlags:= STARTF_USESTDHANDLES or
STARTF_USESHOWWINDOW;
StartInfo.wShowWindow:=SW_HIDE;
StartInfo.hStdOutput:= hWritePipe;
StartInfo.hStdError:= hWritePipe;
StartInfo.cb:=SizeOf(StartInfo);
try
if CreateProcess(nil,
PChar(cmdline),
nil,
nil,
True,
0,
nil,
nil,
StartInfo,
ProcInfo)
then
begin
CloseHandle(ProcInfo.hThread);
CloseHandle(hWritePipe);
end
else
RaiseLastWin32Error;
New(Params);
Params^.hReadPipe := hReadPipe;
ReaderHandle := CreateThread( nil,
0,
@ThreadRead,
Params,
0,
ReaderId);
if ReaderHandle = 0 then
RaiseLastWin32Error;
if WaitForSingleObject(ReaderHandle, Timeout) = WAIT_TIMEOUT then
begin
TerminateThread(ReaderHandle,0);
TerminateProcess(ProcInfo.hProcess, 1);
Result := 'Process terminated.'#13#10;
end;
Result := OEMToStr(Result+Params^.s);
finally
if ReaderHandle > 0 then
CloseHandle(ReaderHandle);
if ProcInfo.hProcess > 0 then
CloseHandle(ProcInfo.hProcess);
if hReadPipe > 0 then
CloseHandle(hReadPipe);
if Assigned(Params) then
Dispose(Params);
end;
end;
end.
I still don't understand. Instead of creating the process and then setting
its command line (which I don't think you can do), why not figure out the
command line first and _then_ create the process?
Since you're creating the process suspended already, it will not have done
anything before you try setting its command line, so I don't see how that
would be any different from starting the process with the right command line
at the start.
--Rob