Skip to content

Commit

Permalink
Replace System with CreateProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
fxzxmic authored Jan 9, 2021
1 parent b04aef3 commit 40cb5cd
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdlib.h>
#include <string>
#include <iostream>
#include <Windows.h>

using namespace std;

Expand All @@ -9,27 +9,30 @@ int main(int argc, char* argv[]) {
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
_splitpath_s(argv[0], drive, sizeof(drive), dir, sizeof(dir), fname, sizeof(fname), ext, sizeof(ext)); //Get program path information

string ch_body = "";
ch_body = fname;

string ch = "bash.exe -c \"";
ch += fname;
int count = 1;
for (count; count < argc; ++count) {
string ch_tmp = " ";
ch_tmp += argv[count];
ch_body += ch_tmp;
ch += " ";
ch += argv[count];
}
ch += "\""; //Splice command

string ch_head = "bash.exe -c \"";
string ch_tail = "\"";
string ch = "";
ch += ch_head;
ch += ch_body;
ch += ch_tail; //Splice command

const char* ch_out;
ch_out = ch.c_str();
system(ch_out); //Forward command
wstring widstr;
widstr = wstring(ch.begin(), ch.end());
LPWSTR sConLin= (LPWSTR)widstr.c_str(); //Convert string to LPWSTR

STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));

CreateProcess(NULL, sConLin, NULL, NULL, false, 0, NULL, NULL, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); //Create and terminate child process

return 0;
}

0 comments on commit 40cb5cd

Please sign in to comment.