Hello everyone:
I want to use CreateProcess() call another application, and save the
result in a text file.I can surely exeute the application,but there is
nothing data in the text file.why??
Could you please give me the code example?
Thank you!
//window message process function
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static STARTUPINFO si;
static PROCESS_INFORMATION pi;
static HANDLE hFile;
static SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = TRUE;
hFile = CreateFile("C:\\Tracert.txt",GENERIC_WRITE,FILE_SHARE_WRITE,
&sa,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hFile;
switch(msg)
{
case WM_CREATE:
CreateProcess("C:\\WINNT\\System32\\tracert.exe"," www.163.com",
NULL, NULL, FALSE,0, NULL, NULL, &si, &pi);
return 0;
case WM_PAINT:
return 0;
case WM_DESTROY:
CloseHandle(hFile);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}

Re: How can I use CreateProcess() to get another application`s result? by Norman

Norman
Sun Aug 21 09:41:13 CDT 2005

collay wrote:
> Hello everyone:
> I want to use CreateProcess() call another application, and save the
> result in a text file.I can surely exeute the application,but there is
> nothing data in the text file.why??
> Could you please give me the code example?
> Thank you!
> //window message process function
> LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
> {
> static STARTUPINFO si;
> static PROCESS_INFORMATION pi;
> static HANDLE hFile;
> static SECURITY_ATTRIBUTES sa;
> sa.bInheritHandle = TRUE;
> hFile = CreateFile("C:\\Tracert.txt",GENERIC_WRITE,FILE_SHARE_WRITE,
> &sa,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
> si.dwFlags = STARTF_USESTDHANDLES;
> si.hStdOutput = hFile;
> switch(msg)
> {
> case WM_CREATE:
> CreateProcess("C:\\WINNT\\System32\\tracert.exe"," www.163.com",
> NULL, NULL, FALSE,0, NULL, NULL, &si, &pi);
> return 0;
> case WM_PAINT:
> return 0;
> case WM_DESTROY:
> CloseHandle(hFile);
> PostQuitMessage(0);
> return 0;
> }
> return DefWindowProc(hwnd,msg,wParam,lParam);
> }
>
>
>
>
Nothing is written to the text file because you are passing FALSE as the
value of bInheritHandles in the call to CreateProcess(). This means
that, even though you pass the handle to the file in si, the created
process has no access to it.

When you fix that problem you'll find another: the first token of the
string passed via lpCommandLine will be parsed as argv[0] in the process
being created and the remaining tokens, if any, will be the arguments to
that program. You should change the command line string to
"tracert www.163.com"
(It's possible, I suppose, that the leading space in the string you're
using is sufficient to delimit an empty first token; I've never tried
it. The documentation does not guarantee that.)

Norm

--
--
To reply, change domain to an adult feline.