Call SHELLEXECUTEINFO string error

I have the following piece of code in which I try to call the function runapplication in order to execute notepad.exe.
The error it gives me is:
D:\computing\c\resource\sysinfo\main.cpp|24|error: cannot convert 'std::string {aka std::basic_string<char>}' to 'LPCSTR {aka const char*}' in assignment|



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
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

int runapplication(string execpath);




int main()
{
    runapplication("notepad.exe");
}




int runapplication(string execpath)
{
    SHELLEXECUTEINFO rSEI = {0};
    rSEI.cbSize = sizeof(rSEI);
    rSEI.lpVerb = "open";
    rSEI.lpFile = execpath;
    rSEI.lpParameters = 0;
    rSEI.lpDirectory = "C:\\";
    rSEI.nShow = SW_SHOWNORMAL;
    rSEI.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShellExecuteEx(&rSEI);

    return 0;
}
rSEI.lpFile = execpath;

On the left is a parameter of type LPCSTR, which is an alias of const char*.

On the right, a string.

A string is not the same thing a const char*. You have givebn the compiler a string object, and told it to assign that value (that string) to a const char*. The compiler has no idea how to do this.

You could probably fix it by changing line 19 to this:

int runapplication(const char* execpath)
Topic archived. No new replies allowed.