ShellExecute() Variable Problem

Hi,

im trying to create a program that executes an .exe file.
It reads the path from an .ini file.
The ini file contents are read and saved to a variable.
Now: How do I get ShellExecute() to use the variable as the path?
Thanks in advance !

Heres the part of my code:

1
2
3
4
//path is declared as a string variable, <string.h> is included
ShellExecute(GetDesktopWindow(), TEXT("open"), TEXT(path), NULL, NULL, SW_SHOWNORMAL);; 

//The TEXT(path) part is the problem, as it cant use a variable :( 
ShellExecute expects LPCSTR (that is char*) and you give it a std::string.
Use path.c_str()
Ok...but when I add .c_str() It doesnt change anything...
And when I cange the variable to char it (of course) only reads the first letter one; a loop doesnt help either.
Where sould I use the .c_str()? It doesnt work in the declarition.
Thanks again!

ps.: Im sorry if this is kind of a "noob" topic; but this is my first use of ShellExecute().
ShellExecute(GetDesktopWindow(), TEXT("open"), path.c_str(), NULL, NULL, SW_SHOWNORMAL);
I've tried that but I get this error when trying to compile: (I use Visual Studio 2010)
error C2664: 'ShellExecuteW' : cannot convert parameter 3 from 'const char *' to 'LPCWSTR' 
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


This is the code I used to test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include "windows.h"
#include <shellapi.h>
#pragma comment(lib,"shell32.lib") 
static HWND hBut; 

using namespace std;

int main()
{

	string path;
	cin >> path;
	cout << path << endl;

	ShellExecute(GetDesktopWindow(), TEXT("open"), path.c_str(), NULL, NULL, SW_SHOWNORMAL);

}


Could anyone please tell me what Im doing wrong ?
Last edited on
Use ShellExecuteA instead.
@ vexer :: thanks a lot that worked!! :)

Heres my final code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include "windows.h"
#include <shellapi.h>
#pragma comment(lib,"shell32.lib") 
static HWND hBut; 

using namespace std;

int main()
{

	string path = "d:\\test1.exe";

	ShellExecuteA(GetDesktopWindow(), ("open"), LPCSTR(path.c_str()), NULL, NULL, SW_SHOWNORMAL);

}


Thank you !!
Last edited on
I thought i'd offer a bit more explanation as to why that worked.

Windows.h defines 2 types of almost every function in the header. One is Unicode(hence the W for wide at the end of the name), and the other is ANSI(hence the A at the end of the name).

Windows.h checks to see if you have defined Unicode, and then picks the appropriate function for you.

If you are using Visual Studio, go into your project settings and turn Unicode off to fix this permanently.
@ vexer :: Thanks again :)

With your explanations i have managed to create a program that reads the file path from an ini file, saves it to a string variable and then executes it. :)
Topic archived. No new replies allowed.