to launch an exe, I have wrote the CreateProcess of this thread How do I open an .exe from another C++ .exe?
this works but if my first program end the cmd will close and i want to keep my launched program running
my code looks like that
void Client::StartServer(){
//init process structures
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// get working directory
char tmp[512];
GetModuleFileName(NULL, tmp, 512);
stringstream sstmp;
string stmp;
sstmp << tmp;
stmp = sstmp.str();
//remove name.exe from path
while(stmp.back() != '\\'){
stmp.pop_back();
}
//launch
sstmp.str("");
sstmp << stmp << "Sim_Server.exe";
cout << "start : " << sstmp.str() << " end "<< endl;
LPCTSTR path = sstmp.str().c_str();
if( !CreateProcess(path,NULL,NULL,NULL,false,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi) ){
throw runtime_error("unable to launch server");
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
Related
In c++ I want to open my text file with notepad, BUT:
Not to stop the program or wait to close the notepad...
Not to create a console/cmd tab (I'am using Visual studio 2017, windows)
Is it possible ?
I have this :
_popen("notepad.exe C:\X\X\X.txt", "r");
but it open a cmd tab.
(Windows only solution)
By modifying the example: https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes
for C++:
#include <iostream>
#include <Windows.h>
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cout << "Usage: " << argv[0] << " [cmdline]\n";
return EXIT_FAILURE;
}
STARTUPINFOA si = {sizeof(si)};
PROCESS_INFORMATION pi = {};
// Start the child process.
if (!CreateProcessA(nullptr, // No module name (use command line)
argv[1], // Command line
nullptr, // Process handle not inheritable
nullptr, // Thread handle not inheritable
false, // Set handle inheritance to FALSE
0, // No creation flags
nullptr, // Use parent's environment block
nullptr, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi)) // Pointer to PROCESS_INFORMATION structure
{
std::cout << "CreateProcess failed (" << GetLastError() << ").\n";
return EXIT_FAILURE;
}
// Wait until child process exits.
//WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return EXIT_SUCCESS;
}
I'm trying to recast a char* to a LPWSTR to use in the CreateProcess API-function. However, I can't get it to work properly. (Compiler is not complaining in the end, however CreateProcess keeps telling me it can't find the path specified (error-code)). This makes me assume that recasting the char* into a LPWSTR fails and therefore, the path is not accessible. (I also tried it with functions such as mbstowcs, but without any luck so far.
My sample-code:
int StartCommand(char* Command) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
cout << Command << endl; //Shows the right command
wchar_t wtext[20];
mbstowcs(wtext, Command, strlen(Command) + 1);//Plus null
LPWSTR CommandLPWSTR = wtext;
if (!CreateProcess(NULL, CommandLPWSTR, NULL, NULL, FALSE, CREATE_NO_WINDOW | CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi)) {
cout << GetLastError() << endl;
cout << GetLastErrorAsString() << endl; //TELLS ME: Can't find the path specified
Message = "Couldn't start command!";
Exit(Message);
}
AssignProcessToJobObject(Handle, pi.hProcess); // Does not work if without CREATE_BREAKAWAY_FROM_JOB
ResumeThread(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return pi.dwProcessId;
}
Pid = StartCommand((char*)("C:/notepad.exe").c_str());
Can you help me? I need to run a file type of .exe. I have got two test1.txt and test2.txt files and in those files there are two PATHes to pr1.exe and pr2.exe. But in the first txt file I run pr1.exe and pr2.exe , in another one there are pr2.exe and pr1.exe .
#include <iostream>
#include <fstream>
#include <process.h>
using namespace std;
int main(int argc, char *argv[]) {
cout << "argc = " << argc << endl;
for (int i = 0; i < argc; i++) {
cout << "Argument: " << i << " = " << argv[i] << endl;
}
if (argc != 2) {
cout << "Error" << endl;
exit(-1);
}
char ch;
ifstream infile;
infile.open(argv[1]);
if (!infile) {
cout << "errrrror: cant open a file" << argv[1];
exit(-1);
}
while (infile) {
infile.get(ch);
cout << ch;
}
cout << endl;
system("pause");
return 0;
}
For example: I write path of the test1.txt and it prints two PATHes of pr1.exe and pr2.exe it's like:
"C:\Users\N\Desktop\process\The 2d file\Debug\The 2d file.exe"
"C:\Users\N\Desktop\process\the 1st file\Debug\the 1st file.exe"
Can you advice me how I should run them?
What you're looking for is the CreateProcess function, more info can be found on msdn:
// start the program up
#include "windows.h"
std::wstring("pr1.exe");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if(!CreateProcess(NULL, (LPWSTR)wstr.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
//error
}
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
Note: If you're not sending any command line paramaters with your program, you could also swap the first and second argument.
for (int i = 0; i < n; i++)
{
const char* cstr = strings[i].c_str();
swprintf_s(fullCommandLine, L"\"%s\" \"%s\" %S", pathToModule, pathToFile, cstr);
if(CreateProcess(NULL,
(LPWSTR)fullCommandLine,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
cout << "succes";
}
else cout << "fail";
}
I'm creating n procesess to find string in given file like this, and In my module(wchich looks for given string in file) I want to send messages to other n-1 processes to quit
while (file >> readout)
{
if (readout == search)
{
cout << "I found string";
SendMessage(/*what should be here*/);
}
}
From where I could get handles to those other processes?
Please see my PostThreadMessage to Console Application.
I created that because it certainly is possible to send a message to a console program, we just must make a message loop, just as it is possible to show a window from a console program.
Note that PostThreadMessage needs a thread id, not a process id. Every process also has a thread id and a process's thread id is in the PROCESS_INFORMATION from CreateProcess.
The following is a larger example but easier to use for demonstrating that PostThreadMessage works in console programs. This program will call itself (passing its thread id) if there is no argument for it then it will wait for the new process to send messages. If there is an argument then it will assume the argument is a thread id and send a message to that thread followed by a WM_QUIT.
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szCmdline[300];
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
BOOL bSuccess = FALSE;
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = NULL;
siStartInfo.hStdOutput = NULL;
siStartInfo.hStdInput = NULL;
DWORD dwThread;
MSG Msg;
TCHAR ThreadIdBuffer[40];
// if no argument then execute ourself then wait for a message from that thread
if (argc == 1) {
_itot_s(GetCurrentThreadId(), ThreadIdBuffer, 40, 10);
szCmdline[0] = '"';
szCmdline[1] = 0;
_tcscat_s(szCmdline, 300, argv[0]); // ourself
int n = _tcslen(szCmdline);
szCmdline[n++] = '"';
szCmdline[n++] = ' ';
szCmdline[n++] = 0;
_tcscat_s(szCmdline, 300, ThreadIdBuffer); // our thread id
bSuccess = CreateProcess(argv[0], // execute ourself
szCmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
if (!bSuccess) {
std::cout << "Process not started\n";
return 0;
}
std::cout << "Waiting\n";
// Now wait for the other process to send us a message
while (GetMessage(&Msg, NULL, 0, WM_USER)) {
if (Msg.message == WM_COMMAND)
std::cout << "WM_COMMAND\n";
else
std::cout << "Message: " << Msg.message << '\n';
}
std::cout << "End of message loop\n";
return 0;
}
// if there is an argument then assume it is a threadid of another one of us
std::cout << "Press Enter to send the message\n";
if (std::wcin.get() != '\n')
return 0;
dwThread = _wtoi(argv[1]);
if (!PostThreadMessage(dwThread, WM_COMMAND, (WPARAM)0, (LPARAM)0))
std::cout << GetLastError() << " PostThreadMessage error\n";
if (!PostThreadMessage(dwThread, WM_QUIT, (WPARAM)0, (LPARAM)0))
std::cout << GetLastError() << " PostThreadMessage error\n";
return 0;
}
I want to make a program that opens a windows explorer window, waits for 5 seconds, and then closes the window. I've tried the following:
#include "stdafx.h"
#include <windows.h>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
void _tmain( int argc, TCHAR *argv[] ) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 ) {
cout << "Usage: " << argv[0] << "<path>";
return;
}
// Build the command string.
wstring app = L"explorer.exe ";
wstring str_command = app + argv[1];
wchar_t* command = const_cast<wchar_t*>( str_command.c_str() );
// Open the window.
if( !CreateProcess( NULL, // No module name (use command line)
command, // 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
) {
cout << "CreateProcess failed: " << GetLastError();
return;
}
cout << "Opened window!" << endl;
// Wait for it.
Sleep(5000);
cout << "Done waiting. Closing... ";
// Close explorer.
HANDLE explorer = OpenProcess(PROCESS_TERMINATE, false, pi.dwProcessId);
if( !explorer ) {
cout << "OpenProcess failed: " << GetLastError();
return;
}
if( !TerminateProcess( explorer, 0 ) ) {
cout << "TerminateProcess failed: " << GetLastError();
return;
}
// Close process and thread handles.
CloseHandle( explorer );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
cout << "Done.";
}
I get it to open well enough, but I can't get it to close. TerminateProcess fails with error code 5. I've also tried posting a WM_CLOSE message to the window. I get a success value out of that, but the window stays open.
Please help!
I found this thread:
Close all browser windows?
It says:
Use the InternetExplorer object to open each window and invoke the Quit method when done. This has the added benefit of closing only the windows you opened (so that windows opened by the user or other applications are unaffected).
https://msdn.microsoft.com/library/aa752127.aspx
I know that this does not help much (missing snippets), but at least something.