Click to See Complete Forum and Search --> : CreateProcess HELP


gravity-1
April 17th, 2005, 10:45 AM
Hi all,

I have a simple little problem with CreateProcess(). I've been searcing msdn and forums and haven't found the solution yet but I'm sure it's a simple one.

I want to execute this console command:

tethereal -a duration:300 -f "ip proto 1" -n >"c:\\program files\\blah\\blah.txt"

I'm using CreateProcess and I have this call:


CreateProcess("c:\\program files\\ethereal\\tethereal.exe",
" -a duration:300 -f \"ip proto 1\" -n >\"c:\\program files\\blah\\blah.txt\"",
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi))


I figured this would work but it just opens the program and closes it immediately. The '-a duration:300' is supposed to make it run for exactly 300 seconds. I know the lpApplicationName parameter is working because if I set lpCommandLine to NULL it runs until I close it. So the problem is with the lpCommandLine parameter.

I don't know if some of the special characters in that string are causing the problem or what.

If anyone knows what the problem is please enlighten me.

Thanks a lot,

grav

gravity-1
April 17th, 2005, 07:57 PM
Hey all,

I've continued working on this and I've discovered that the problem lies in me redirecting the output to a text file. If I run this code:


char params[100] = " -a duration:10 -f \"ip proto 1\"";

if (!CreateProcess("c:\\program files\\ethereal\\tethereal.exe",
params,
//NULL,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
"c:\\program files\\ethereal\\",
&si,
&pi))

the program runs fine, the command line arguments "-a and -f" are adhered to.

But when I try to write to a file instead of the standard output it does not execute properly. Does anyone know if you can use >"c:\\sometextfile.txt" from C++?

This works fine from the command prompt but C++ doesn't like this. Is there any other way to redirect output from the command line?

Any feedback is greatly appreciated.

Thanks a lot,

grav

AdaraCD
April 18th, 2005, 12:54 AM
here is a sample:

int _tmain(int argc, TCHAR *argv[])
{
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
STARTUPINFO si;
PROCESS_INFORMATION pi;
HANDLE hFile = NULL;
BOOL rc;
DWORD dwError = ERROR_SUCCESS;

if(argc < 3) {
_ftprintf(stderr,_T("Usage: %s <program> <file>\n"),argv[0]);
return EXIT_FAILURE;
}

InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd,TRUE,NULL,FALSE);
SetSecurityDescriptorGroup(&sd,NULL,FALSE);
SetSecurityDescriptorSacl(&sd, FALSE,NULL,FALSE);

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = &sd;
sa.bInheritHandle = TRUE;

hFile = CreateFile(argv[2], GENERIC_WRITE,FILE_SHARE_WRITE, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE) {
dwError = GetLastError();
goto __exit__;
}

memset(&si,0,sizeof(si));
memset(&pi,0,sizeof(pi));

si.lpReserved = NULL;
si.lpDesktop = NULL;
si.lpTitle = NULL;
si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.cbReserved2 = 0;
si.lpReserved2 = NULL;
si.hStdOutput = hFile;
si.cb = sizeof(si);

rc = CreateProcess(NULL,argv[1] , NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
if(!rc) {
dwError = GetLastError();
goto __exit__;
}

WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

__exit__:
if(hFile) CloseHandle(hFile);
DisplayError(dwError);
return EXIT_SUCCESS;
}

NoHero
April 18th, 2005, 05:03 AM
But when I try to write to a file instead of the standard output it does not execute properly. Does anyone know if you can use >"c:\\sometextfile.txt" from C++?

Read this:

http://www.codeproject.com/threads/redir.asp
http://dslweb.nwnexus.com/~ast/dload/guicon.htm

http://www.codeguru.com/Cpp/misc/misc/article.php/c277