Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

CreateProcess() problem plz help

the code im about to put down.. seems to run through without errors... however. when i cout out the final conversion i get wierd numbers...
but basically what i have here is create process.. im using a dummy.exe to create a txt file so i know if it works right away...

if(first4=="EXEC"){
LPWSTR szCmdline=(LPWSTR)cl.c_str();

STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInformation;
HANDLE hPipeRead, hpRead;
HANDLE hPipeWrite;
SECURITY_ATTRIBUTES sa;

memset(&ProcessInformation,0,sizeof(ProcessInformation));
memset(&sa,0,sizeof(sa));
sa.nLength=sizeof(sa);
sa.bInheritHandle = TRUE;
CreatePipe(&hPipeRead,&hPipeWrite,&sa,0);

memset(&StartupInfo,0,sizeof(StartupInfo));
cout << "pipes and memset";
GetStartupInfo(&StartupInfo);
StartupInfo.hStdOutput = hPipeWrite;
StartupInfo.hStdInput = hPipeRead;
StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
cout << "StartupInfo Read\n";
CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInformation);
cout << szCmdline;
CloseHandle( hPipeWrite );
Sleep(100); DWORD stuff; char buff[1000]; bool firstsend; int offset = 0, bRecv;
PeekNamedPipe(hPipeRead, NULL, 0, NULL, &stuff, NULL);
if(stuff != 0) {
ZeroMemory(buff, sizeof(buff));
firstsend = true;
do {
ReadFile(hPipeRead, buff, 1000, &stuff, NULL);
if(firstsend) { send(usersock, buff + offset, strlen(buff) - offset, 0); firstsend = false; }
else send(usersock, buff, strlen(buff), 0);
} while(stuff == 1000);
} else { char ccc[128]="NoReply program run"; send(usersock, ccc, strlen(ccc), 0); }
}

//
// This is for an ftp server and webserver i have to get this create process functioning and outputing
// the dos screen info through a socket which i already have setup here and should work... but errors
// out when if statement is called... ive been playing wiht it for about 2hr... plz help
// im using Visual Studio 2005 C++ and im pretty sure this is CLR
[2160 byte] By [JD007] at [2007-11-11 10:31:26]
# 1 Re: CreateProcess() problem plz help
Maybe you should try this:
LPSTR szCmdline = cl.c_str();
. . .
CreateProcessA(NULL, szCmdline, . . .);
Also check the return value of CreateProcessA function and obtain more error details with GetLastError function.
I hope this helps. Or give us more and clearer details about the problem.
Viorel at 2007-11-11 20:58:37 >
# 2 Re: CreateProcess() problem plz help
cpp(753) : error C2440: 'initializing' : cannot convert from 'const char *' to 'LPSTR'

this is visual studio 2005 man they dont accept some of those things...
JD007 at 2007-11-11 20:59:38 >
# 3 Re: CreateProcess() problem plz help
If cl is of type std::string, then try this:
LPSTR szCmdline=(LPSTR)cl.c_str();
Viorel at 2007-11-11 21:00:43 >
# 4 Re: CreateProcess() problem plz help
has to be LPWSTR for the CreateProcess function sorry...
JD007 at 2007-11-11 21:01:39 >
# 5 Re: CreateProcess() problem plz help
I think there might be a setting in there somewhere to allow typecast if you know.. i think it changed from previous versions of vs...
JD007 at 2007-11-11 21:02:43 >
# 6 Re: CreateProcess() problem plz help
has to be LPWSTR for the CreateProcess function sorry...
For proposed above CreateProcessA, it can be LPSTR.
Viorel at 2007-11-11 21:03:42 >
# 7 Re: CreateProcess() problem plz help
ok...

what string am i supposed to send to that second option in CreateProcess....
Ive seen a couple different ways of doing it but i cant get it to work... when i cout out the LPWSTR i get some wierd numbers i dont know if its even compatable with cout.. but anyways... assuming thats ok..

CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInformation);

what should i put in szCmdLine and isn't there a second 3rd place to specify directory and filename.. the file im using is
c:/dummy.exe
thats all im sending to the szCmdLine... it should create a txt file.. i double click on the app txt file appears.. but doesn't work in create process...
JD007 at 2007-11-11 21:04:46 >
# 8 Re: CreateProcess() problem plz help
here is the error im getting...

Here is the error im getting...

Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.
at CreateProcessW(Char* , Char* , _SECURITY_ATTRIBUTES* , _SECURITY_ATTRIBUTE
S* , Int32 , UInt32 , Void* , Char* , _STARTUPINFOW* , _PROCESS_INFORMATION* )
at execute(Void* params)

for this one...
CreateProcess(NULL, L"c:\\test.exe", NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInformation);
JD007 at 2007-11-11 21:05:44 >
# 9 Re: CreateProcess() problem plz help
The second parameter (lpCommandLine) to CreateProcessW is not a const wchar_t*, it's a wchar_t*. You need to pass in a non-const param.

Try the following code:

STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

TCHAR szCommandLine[] = _T("\"C:\\Windows\\system32\\calc.exe\"");
if( !CreateProcess( NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) )
{
_tprintf( _T("CreateProcess failed - %d\n"), GetLastError() );
return;
}

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

This should work.
And then change the line with CreateProcess to:

if( !CreateProcess( NULL, L"C:\\Windows\\system32\\calc.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) )
This should produce an access violation.

Jeffrey Richter discusses this in Programming Applications for Microsoft Windows.
When you do L"c:\\test.exe", Visual C++ places the string in a read-only portion of the file image. But internally, CreateProcessW intends to modify this string (it restores it prior to returning). In your case, when it tries to modify the string that is read-only, you get the access violation.
/\/\|\/ at 2007-11-11 21:06:48 >