system("app.exe") also loads cmd.exe

Nov 27, 2011 at 6:32pm
Like the TT says, I make a system call to run another program, but it also loads cmd.exe for some reason, is there anyway to make it not do that? it's really annoying.

the program I'm calling is made using the windows api as a WS_POPUP style if that matters.
Nov 27, 2011 at 6:52pm
That's how system works. It's what it does. It opens a shell (i.e. cmd.exe)

If you want to start some other process, use the OS API. For example, under windows, CreateProcess
Nov 27, 2011 at 7:56pm
Ah ok, that makes sense I guess.

I'm looking at CreateProcess atm, but it won't accept a string as the second argument and it wants a wchar* instead, how do I turn simple text into that data type?
Nov 27, 2011 at 7:59pm
It should work fine with a string if you use the c_str() member function from the string class: http://www.cplusplus.com/reference/string/string/c_str/
Nov 27, 2011 at 8:27pm
that didn't work (unless I messed up somehow but i don't think so)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        STARTUPINFO          si = { sizeof(si) };
        PROCESS_INFORMATION  pi;
        char * cstr, *p;

          string str ("monocle.exe");

          cstr = new char [str.size()+1];
          strcpy (cstr, str.c_str());

        if(CreateProcess(0, cstr, 0, 0, FALSE, 0, 0, 0, &si, &pi))
        {
        // optionally wait for process to finish
        WaitForSingleObject(pi.hProcess, INFINITE);


        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);


that's my code and I get the following error:
error: cannot convert 'char*' to 'WCHAR*' for argument '2' to 'BOOL CreateProcessW(const WCHAR*, WCHAR*, _SECURITY_ATTRIBUTES*, _SECURITY_ATTRIBUTES*, BOOL, DWORD, void*, const WCHAR*, _STARTUPINFOW*, _PROCESS_INFORMATION*)'

I'm coding and compiling in QT Creator if that makes a difference
Nov 27, 2011 at 8:38pm
I had it wrong. I normally just dereference the first character in the string, in your case it would look like this CreateProcess(0, &str[0], 0, 0, FALSE, 0, 0, 0, &si, &pi)
Last edited on Nov 27, 2011 at 8:40pm
Nov 27, 2011 at 8:43pm
same error, thanks for playing though. same result with &cstr[0]
Nov 27, 2011 at 9:02pm
Well then you're doing something wrong. I just compiled it and it works.
Nov 27, 2011 at 9:21pm
That's really strange, I changed it to the following as per your suggestion and I still get that error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        STARTUPINFO          si = { sizeof(si) };
        PROCESS_INFORMATION  pi;
        char * cstr, *p;

          string str ("monocle.exe");

          cstr = new char [str.size()+1];
          strcpy (cstr, str.c_str());

          if(CreateProcess(0, &str[0], 0, 0, FALSE, 0, 0, 0, &si, &pi))
        {
        // optionally wait for process to finish
        WaitForSingleObject(pi.hProcess, INFINITE);


        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
Nov 27, 2011 at 9:55pm
I fixed it. for future reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        STARTUPINFO          si = { sizeof(si) };
        PROCESS_INFORMATION  pi;
        char * cstr, *p;

        wstring str = (L"monocle.exe");//wstring type and the L to convert the string to wstring.

        if(CreateProcess(0, &str[0], 0, 0, FALSE, 0, 0, 0, &si, &pi))
        {
        // optionally wait for process to finish
        WaitForSingleObject(pi.hProcess, INFINITE);


        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
Nov 28, 2011 at 3:32am
Yes, this is the good old "I mix WinAPI names with string data types without caring about their actual signatures".

1. If you use wide strings, you MUST call the WinAPI functions using their -W names. In your case, CreateProcessW().
2. If you use narrow (normal) strings, you MUST call the WinAPI functions using there -A names. In your case, CreateProcessA().
3. If you use the WinAPI names without any suffix (CreateProcess() in your case), you MUST use a data type that changes depending on the UNICODE #define:

1
2
3
4
typedef std::basic_string<TCHAR> tstring;

tstring str = TEXT("monocle.exe");
if (CreateProcess(0, &str[0], .....)
Topic archived. No new replies allowed.