CreateProcess function issue - c++

I'm trying to start an process from my code with createprocess function. The command line is ok, and the other exe, i can start it without problems from visual studio.
When I am trying to start it from the other process with createprocess it gives me - Runtime error this app has requested the runtime to terminate it in an unusual way.
What could be the problem ? How could I remove this problem ?
STARTUPINFO si;
PROCESS_INFORMATION pi;
bool bResult;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
//Cast System::String* __gc to char*
char* chAppName = (char*)(void*)Marshal::StringToHGlobalAnsi(AppName);
char* chCmdLine = (char*)(void*)Marshal::StringToHGlobalAnsi(CmdLine);
//Start the child process.
bResult = CreateProcess( chAppName, // No module name (use command line)
chCmdLine, // 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

Try eliminating the StringToHGlobalAnsi calls and hard coding them instead first. If that stops the problems, we can then sort out the StringToHGlobalAnsi calls.

Related

How can I use CreateProcess method to refer to other 'ini' file to start 'exe' program?

I'm currently trying to execute 'ClientProc.exe'. This program uses 'SA_Client.ini' file from a same directory as a reference.
I'm using CreateProcess method to execute the .exe program as belows.
STARTUPINFO si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
LPCSTR lpApplicationName = "C:\\projects\\Client\\ClientProc.exe";
LPSTR cmd = (char*)"./";
if (!CreateProcess(lpApplicationName, // the path
cmd, // 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 (removed extra parentheses)
))
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
The problem is that I don't know how to make CreateProcess to refer to the 'ini' file even though the file exists in the same directory.
I've checked Microsoft document or other StackOverflow comments but couldn't find anything useful.
Does anyone know how to do it?
Thank you in advance!
--Edit--
I've also tried setting the lpCurrentDirectory parameter where ini file exists.
CreateProcess(lpApplicationName, // the path
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
"C:\\projects\\Client", // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
)
Then, I've got an error 267 which stands for 'ERROR_DIRECTORY'. I'm sure that the file exists in the directory. What am I doing wrong?

Process of c++ executable doesn't close correctly

in my program I start other c++ application in the same window closing the previous. Sometimes the process that I see in the Task Manager doesn't close. So I'll have many process whit the same name. How can I avoid this?
startup("../folder/c++_executable.exe");
exit(0);
void startup(LPCTSTR lpApplicationName)
{
// additional information
STARTUPINFO si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// start the program up
CreateProcess( lpApplicationName, // the path
"", // 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
;
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
I think you will need to check why it's not closing - get VisualStudio/WinDBG and attach to process that is not closing and check where it hang.

Create Process default browser

I'm currently using ShellExecute "open" to open a URL in the user's browser, but running into a bit of trouble in Win7 and Vista because the program runs elevated as a service.
I want to get thread id so ShellExecute is not possible to get thread id so i started using "CreateProcess" But i don't see any help on opening default browser whichever is it from CreateProcess.
You can use other features of the Shell API to figure out which browser is the default.
For instance, you can ask the shell for the executable name associated with .html files using AssocQueryString, and launch that via CreateProcess.
Here you can open the URL with your default browser.
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CStringA command_line;
command_line.Format("cmd.exe /c start \"link\" \"%s\"", "http://www.google.com");
if (!CreateProcessA(NULL, // No module name (use command line)
command_line.GetBuffer(),
NULL, // Process handle not inheritable
NULL, // Thread handle not inhberitable
FALSE, // Set handle inheritance to FALSE
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, // 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
)
{
TRACE("CreateProcess failed (%d).\n", GetLastError());
return;
}
Here is another answer as you wanted. As far as you want it open as a new window. You can expand it for IE, opera, and so on...
DWORD size = MAX_PATH;
char buff[MAX_PATH];
int err = AssocQueryStringA(ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_EXECUTABLE, ".html", NULL, buff, &size);
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CStringA command_line;
CStringA target_url( "http://google.com/" );
if( strcmp( "chrome.exe", PathFindFileNameA(buff)) == 0 )
command_line.Format("%s --new-window %s", buff, target_url);
else if( strcmp( "firefox.exe", PathFindFileNameA(buff)) == 0 )
command_line.Format("%s -new-instance %s", buff, target_url);
else
command_line.Format("%s %s", buff, target_url);
if (!CreateProcessA(NULL, // No module name (use command line)
command_line.GetBuffer(),
NULL, // Process handle not inheritable
NULL, // Thread handle not inhberitable
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
)
{
//... error handling
return;
}

CreateProcessAsUser() not working

I am trying to start GUI application using my service. I developed the service on VS2012 and running on windows 7. But CreateProcessAsUser function doesn't start application even though it returns successfully. following is my code:
PHANDLE hToken = NULL;
WTSQueryUserToken (WTSGetActiveConsoleSessionId (), hToken) ;
if( !CreateProcessAsUser( hToken,
NULL, // No module name (use command line)
pPath, // 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
)
what could be the issue?
Thanks,
KM.
After retrieving the user token from WTSQueryUserToken(), call DuplicateTokenEx() to convert it into a primary token, and pass that token to CreateProcessAsUser(). You also need to specify the "winsta0\default" (use: "winsta0\\default") desktop via the STARTUPINFO structure. You should also call CreateEnvironmentBlock() using the same token, and pass that environment to CreateProcessAsUser() as well.
There is not enough information to be sure of my answer but that kind of error often happen when the structures are not correctly initialized.
PHANDLE hToken = NULL;
WTSQueryUserToken (WTSGetActiveConsoleSessionId (), hToken) ;
//be sure that the handle is correct ! (can be the issue)
if (!hToken) printf("Token error.\n");
//init here !
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if( !CreateProcessAsUser( hToken,
NULL, // No module name (use command line)
pPath, // 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
)
I faced a similar issue while using WTSQueryUserToken in windows 7, but the same function worked in windows 10.
So I took the explorer.exe token, called the DuplicateTokenEx function.
Set the corresponding values for startupinfo structure
si.lpDesktop = "winsta0\\default";
si.wShowWindow = SW_SHOWNORMAL;
si.dwFlags = STARTF_USESHOWWINDOW;
and called createprocessasuser

using CreateProcess with a relative path

Is it possible to pass a relative path to create my child process?
This code will compile, but it gives an error because I'm using a relative path.
void Cminivideo3App::creerChildProcess(void)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
int retvalue = CreateProcess( TEXT("\..\Debug\traitement.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
);
int lastError = GetLastError();
}
Couple things:
As #Oswald says, \ is the root folder of the current drive, not a relative path.
You forgot to escape your backslashes. You really want TEXT("..\\Debug\\traitement.exe").
It does not look like a relative path to me. \ is the root folder of the current drive.