DrJoness

im using a dummy.exe to create a txt file so i know if it works right away...
but i think theres a problem convertin from std string to the LPWSTR in vs 2005
if there is alternate method i would appreciate any help

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


Re: Visual C++ Language CreateProcess Issues plz help

einaros

Use std::wstring, not std::string, or don't compile your application for unicode.




Re: Visual C++ Language CreateProcess Issues plz help

DrJoness

This is an old dos app i made a year ago... im adding many things to it.. so std::string is what im going to use.... i know there is a way to convert to LPWSTR from std string.. i think there is a setting up there in VS 2k5 somewhere to allow those type castings...




Re: Visual C++ Language CreateProcess Issues plz help

TilakGopi

Hi,

U r right. LPWSTR szCmdline=(LPWSTR)cl.c_str(); - it is erroneous

The write convesion is using 'MultiByteToWideChar' as follows

--------------------------------------------------------

int nLen = cl.length();

LPWSTR szCmdLine = new WCHAR[nLen+1];

MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cl.c_str(),nLen,szCmdLine,nLen);

szCmdLine[nLen] = '\0';

----------------------------------------------------------------------------------------

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C++ Language CreateProcess Issues plz help

einaros

DrJoness wrote:
This is an old dos app i made a year ago... im adding many things to it.. so std::string is what im going to use.... i know there is a way to convert to LPWSTR from std string.. i think there is a setting up there in VS 2k5 somewhere to allow those type castings...

You are conscious about the choice to build your application for unicode If you switch to multibyte, no conversion would be necessary.






Re: Visual C++ Language CreateProcess Issues plz help

DrJoness

Ok look it seems to go through fine.. but does not loanch the program...
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...




Re: Visual C++ Language CreateProcess Issues plz help

TilakGopi

Hi,

U need to change CreateProcess like-

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

and i don't know about 5th paramter , u have given it as TRUE- indicating process handle is inheritable.That's it.

And this my launch exe function - just check it u have any obscures

--------------------------------------

BOOL LaunchExe(LPCTSTR szExe)

{

// CreateProcesss.cpp : Defines the entry point for the application.

STARTUPINFO si;

PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );

si.cb = sizeof(si);

ZeroMemory( &pi, sizeof(pi) );

// Start the child process.

if( !CreateProcess(szExe,// "C:\\WINDOWS\\NOTEPAD.EXE", // No module name (use command line).

NULL, // Command line.

NULL, // Process handle not inheritable.

NULL, // Thread handle not inheritable.

FALSE, // Set handle inheritance to FALSE.

0, // No creation flags.

NULL, // Use parent's environment block.

NULL, // Use parent's starting directory.

&si, // Pointer to STARTUPINFO structure.

&pi ) // Pointer to PROCESS_INFORMATION structure.

)

{

MessageBox(NULL,_T( "CreateProcess failed." ),_T("Result"),MB_OK);

return FALSE;

}

// Wait until child process exits.

WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.

CloseHandle( pi.hProcess );

CloseHandle( pi.hThread );

return TRUE;

}

--------------------------------------------------------------------

Hope this'll help u.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C++ Language CreateProcess Issues plz help

einaros

DrJoness wrote:
Ok look it seems to go through fine.. but does not loanch the program...
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...

Save yourself the trouble of converting: hit ALT+F7, navigate to the general properties and switch the character set from "unicode" to "multibyte", then replace all your "LPWSTR" with "LPSTR".

Also see http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=849851&SiteID=1.






Re: Visual C++ Language CreateProcess Issues plz help

DrJoness

Hmm.. still not working i tried a couple diff things...
string cl;
cl="TEST";
int nLen = cl.length();
cout << cl;
LPWSTR szCmdLine = new WCHAR[nLen+1];

MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cl.c_str(),nLen,szCmdLine,nLen);

szCmdLine[nLen] = '\0';

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(szCmdLine,NULL, 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); }
CloseHandle( hPipeRead );




Re: Visual C++ Language CreateProcess Issues plz help

DrJoness

I have to use Unicode earlier in the program.. i have an ifdef UNICODE setting there...
but can i do the same thing for this process or what but i have to leave the setting at unicode..





Re: Visual C++ Language CreateProcess Issues plz help

einaros

DrJoness wrote:
I have to use Unicode earlier in the program.. i have an ifdef UNICODE setting there...
but can i do the same thing for this process or what but i have to leave the setting at unicode..

There's nothing preventing you from using unicode structures in your application even though you use the multibyte character set in the properties. The difference is merely that the API and SDK functions accept plain character strings by default. If you compile for multibyte, and happen to want to write some unicode data to a text file, you can use LPWSTR as before, OpenFileW and WriteFileW. The difference, as mentioned, will be that calls to the API and SDK functions will default to multibyte (plain c strings).






Re: Visual C++ Language CreateProcess Issues plz help

TilakGopi

Hi,

Here is my console sample working file-

--------------------------------------------------------

#include "stdafx.h"

#include <iostream>

using namespace std;

#include <windows.h>

BOOL LaunchExe(LPWSTR szExe)

{

// CreateProcesss.cpp : Defines the entry point for the application.

STARTUPINFO si;

PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );

si.cb = sizeof(si);

ZeroMemory( &pi, sizeof(pi) );

// Start the child process.

if( !CreateProcess(szExe,// "C:\\WINDOWS\\NOTEPAD.EXE", // No module name (use command line).

NULL, // Command line.

NULL, // Process handle not inheritable.

NULL, // Thread handle not inheritable.

FALSE, // Set handle inheritance to FALSE.

0, // No creation flags.

NULL, // Use parent's environment block.

NULL, // Use parent's starting directory.

&si, // Pointer to STARTUPINFO structure.

&pi ) // Pointer to PROCESS_INFORMATION structure.

)

{

MessageBox(NULL,_T( "CreateProcess failed." ),_T("Result"),MB_OK);

return FALSE;

}

// Wait until child process exits.

WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.

CloseHandle( pi.hProcess );

CloseHandle( pi.hThread );

return TRUE;

}

int _tmain(int argc, _TCHAR* argv[])

{

string cl= "C:\\WINDOWS\\NOTEPAD.EXE";

int nLen = cl.length();

LPWSTR szCmdLine = new WCHAR[nLen+1];

MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cl.c_str(),nLen,szCmdLine,nLen);

szCmdLine[nLen] = '\0';

LaunchExe(szCmdLine);

}

----------------------------------------------------

Let's go further..

Regards,

Ch.T.Gopi Kumar






Re: Visual C++ Language CreateProcess Issues plz help

DrJoness

Problem with that code is...
1>.\.cpp(124) : error C3861: '_T': identifier not found
1>.\.cpp(124) : error C3861: '_T': identifier not found
1>.\.cpp(423) : warning C4018: '<' : signed/unsigned mismatch




Re: Visual C++ Language CreateProcess Issues plz help

einaros

DrJoness wrote:
Problem with that code is...
1>.\.cpp(124) : error C3861: '_T': identifier not found
1>.\.cpp(124) : error C3861: '_T': identifier not found
1>.\.cpp(423) : warning C4018: '<' : signed/unsigned mismatch

#include <tchar.h>






Re: Visual C++ Language CreateProcess Issues plz help

DrJoness

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);