opening an external program

I am tying to open a program by using the system command.
system ("C:\\Users\\someone\\Desktop\\somthing.exe");
And that works fine, but if I do something like this it will not work because of the space between Program and Files.
system ("C:\\Program Files\\something.exe");

how do I fix this?
Last edited on
You could use ShellExecute, part of the Windows' Library. I assume you are fond of the WinAPI due to you posting this in Windows Forum, but you may not know what I am talking about.
Anyway, ShellExecute can open a file. So, example:
1
2
// To open a program:
ShellExecute(NULL, "open", "C://Program Files (x86)//something.exe", NULL, NULL, SW_SHOWNORMAL);


What that will do is open the program (or according to the function, open the file). Example 2:
1
2
3
// This will open Notepad, using a char.
char notepad[] = "notepad.exe";
ShellExecute(NULL, "open", notepad, NULL, NULL, SW_SHOWNORMAL);


I assume you know to include <windows.h> at the top of your file, but just in case you don't, put this at the top:
#include <windows.h>

If you need a better reference, here is a link:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
Last edited on
I get this error when I tried it

error: cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'HINSTANCE__* ShellExecuteW(HWND, LPCWSTR, LPCWSTR, LPCWSTR, LPCWSTR, INT)'
ShellExecute(NULL, "open", "C://Program Files (x86)//something.exe", NULL, NULL, SW_SHOWNORMAL);

I am including windows.h
^
Last edited on
ShellExecuteW(HWND, LPCWSTR, LPCWSTR, LPCWSTR, LPCWSTR, INT)'


That would be because ShellExecuteW is for wide strings, aka, wchar_t, as said in your error. You can use ShellExecuteW, and to do so, you would just change the parameters to L"", instead of just regular "". So, example time:
 
ShellExecuteW(NULL, L"open", L"C://Program Files (x86)//something.exe", NULL, NULL, SW_SHOWNORMAL);


You could also use ShellExecuteA, which is just ShellExecute, except using chars. Example time 2:
 
ShellExecuteA(NULL, "open", "File Path To .exe File", NULL, NULL, SW_SHOWNORMAL);


I always use A functions in windows, due to my passionate hate for wide strings.
And really (irrelevant) I don't use the WinAPI much anymore. I use cross platform stuff. But, the choice is always yours, however.
I'd suggest "CreateProcessA()":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define WIN32_LEAN_AND_MEAN

#include <windows.h>


struct Process_Info: public PROCESS_INFORMATION
{
public:
    Process_Info() = default;

    ~Process_Info()
    {
        CloseHandle(this->hThread);
        CloseHandle(this->hProcess);
    }
};


struct Startup: public STARTUPINFO
{
public:
    Startup(): STARTUPINFO(), cb(sizeof(Startup)) {}

    DWORD cb;
};

int main(int NumOfArgs, char* Args[])
{
     Startup si;
     Process_Info pi;

     CreateProcessA("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

    return 0;
}

Just another option to consider.
Last edited on
Thank you, using this worked!
ShellExecuteA(NULL, "open", "File Path To .exe File", NULL, NULL, SW_SHOWNORMAL);
Your welcome, glad to help! Just remember that WinAPI has functions for all string types (in most cases, at least). So, for CreateProcess, there is CreateProcessA, CreateProcessW... For CreateWindow: CreateWindowA, CreateWindowW, and so on
Topic archived. No new replies allowed.