-2

I have a timer, after which a local html file should be executed, but I hit some kind of error:

int delay = 120;
delay *= CLOCKS_PER_SEC;

clock_t now = clock();

while (clock() - now < delay);

string strWebPage = "file:///D:/project/site/scam.html";

strWebPage = "file:///" + strWebPage;
ShellExecute(NULL, NULL, NULL, strWebPage, NULL, SW_SHOWNORMAL);

return 0;

E0413 no suitable conversion function from "std::string" to "LPCWSTR" exists

I'm new to C++, so it might be an obvious solution.

Could anyone point me to how I can fix it?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 4
    Please don't post images. That said, you want `ShellExecute(NULL, NULL, NULL, strWebPage.c_str (), NULL, SW_SHOWNORMAL);`. And use `nullptr`, not `NULL`. `NULL` is an anachronism. – Paul Sanders Dec 28 '22 at 21:59
  • 1
    You'll also want to switch `std::wstring` since `ShellExecute` takes a wide character string. – Captain Obvlious Dec 28 '22 at 22:08
  • 2
    Why are you using *two* `file:///` prefixes? – ShadowRanger Dec 28 '22 at 22:33
  • 1
    @CaptainObvlious The `ShellExecute()` function comes in two variants: `ShellExecuteA()` and `ShellExecuteW()`. Unless OP has `#define UNICODE` hiding somewhere, he is using the correct string type for that function. (Oh, but now I’ve looked at the image.) – Dúthomhas Dec 28 '22 at 22:41
  • @Dúthomhas it is clear from the error message that `UNICODE` is actually defined. It doesn't have to be a `#define` in code, it can also be set via project settings instead (and likely is). – Remy Lebeau Dec 28 '22 at 23:38
  • @RemyLebeau Yes, I admit I missed that the first time through, because I don’t like clicking on pictures. I addressed that in my answer. – Dúthomhas Dec 28 '22 at 23:57

1 Answers1

1

You have two problems.

But first, you should always take the time to read the documentation. For Win32 functions, you can get to a known function by typing something like “msdn ShellExecute” into your favorite search engine and clicking the “Lucky” button.

Problem One

ShellExecute() is a C function. It does not take std::string as argument. It needs a pointer to characters. Hence:

std::string filename = "birds.html";
INT_PTR ok = ShellExecute( 
    NULL,             // no window
    NULL,             // use default operation 
    filename.c_str(), // file to open
    NULL,             // no args to executable files
    NULL,             // no start directory
    SW_SHOWNORMAL );
if (ok <= 32)
  fooey();

Notice that we pass a const char * to the function as the file to <default verb>.

Problem Two

From your image it would appear that you have your application declared as a Unicode application. In other words, somewhere there is a #define UNICODE.

This makes ShellExecute() expect a WIDE character string (const wchar_t *)as argument, not a narrow string (const char *).

You can still use a narrow string by simply specifying that you want the narrow version:

INT_PTR ok = ShellExecuteA(
    ...

I recommend you look at how you set up your project to figure out how you got things to think you were using wide strings instead of narrow strings.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39