Click to See Complete Forum and Search --> : CreateProcess() help


Uziel2101
March 27th, 2008, 02:08 PM
Hey all,
I am writing an MFC app that Sync files for building purposes. There is an option to show a preview of what file(s) would be sycned over to your new directory. I wanted to use CreateProcess to catch that information so that I could display it. However, I haven't been able to actually get CreateProcess() to work for "cmd.exe", I have been able to get it to work for batch file ( thats another story ). here is a snippet of the CreateProcess


STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES saAttr;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(si);

saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;


CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0);
SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0);

si.hStdError = hChildStdoutWr;
si.hStdOutput = hChildStdoutWr;
si.dwFlags |= STARTF_USESTDHANDLES;

CreateProcess(NULL,(LPWSTR)spawnstr, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);


I've tried all kinds of things:
ex:
CreateProcess( (LPWSTR)"cmd.exe", (LPWSTR)spawnstr, ...


spawnstr has something temporary ( I just want to get copying done first )
"xcopy C:\test.txt C:\testingfolder\test.txt"

When debugging my strings look correct, I'm not sure exactly where to go from here...It's most likely something stupid that people will point and laugh at...

Help please? Many thanks in advance.

Lindley
March 27th, 2008, 02:27 PM
I recall there being some special case for cmd.exe. Perhaps it uses that if the first parameter is NULL?

a71104
March 29th, 2008, 01:14 PM
you just can't convert from ANSI to Unicode like that! :)
"cmd.exe" is an ANSI string, LPWSTR represents a pointer to a Unicode one. use the L prefix:

CreateProcess(L"cmd.exe", L"xcopy . . .

or, for character set portability, the _T macro:

CreateProcess(_T("cmd.exe"), _T("xcopy . . .

the _T macro automatically prepends L when the UNICODE macro is defined in your program (usually defined by compiler command line, or before #including windows.h or tchar.h), while it does absolutely nothing when it is not.

hope this helps.

Arjay
March 30th, 2008, 08:17 PM
For the most part you can ignore the first parameter of CreateProcess and put all the command line stuff into the second parameter.

As others have mentioned, use L"" or the _T("") macros. You also need to escape the backslash characters.

CreateProcess( NULL, _T("xcopy C:\\test.txt C:\\testingfolder\\test.txt"), ... );

Finally you need to double quote any paths that contain strings;

CreateProcess( NULL, _T("xcopy \"C:\\some folder\\test.txt\" \"C:\\testing folder\\test.txt\""), ... );

Warning: Check the return value of CreateProcess. If it's zero, use GetLastError() to determine what the error is. Also, look into using GetExitCodeProcess to retrieve the error code of the launched process.

Lastly, consider using an api such as SHFileOperation (http://msdn2.microsoft.com/en-us/library/bb762164.aspx) to do the file copying for you. Some guys don't agree, but I find it more difficult to get meaningful error information when using the cmd shell programs (like xcopy). If you use an api, you can get a direct error code.