Recently I made a little program that could open files/exes/links. (facebook, vs 2012, ect ect) But it was in the command prompt. Worked perfect.

I decided to make a forms application to do just the same, except through buttons instead of user input. But I keep running across this problem...

error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'

when using

ShellExecute(NULL, "open", "www.facebook.com", NULL, NULL, SW_SHOWNORMAL);

So I did some google'n and came across advice that suggested that I go to project menu->properties->general->character set and change that to not set from unicode.

then i got even more errors. then was told to add .c_str() to the URL,

ShellExecute(NULL, "open", "www.facebook.com".c_str(), NULL, NULL, SW_SHOWNORMAL);

but got

Form1.h(83): error C2228: left of '.c_str' must have class/struct/union
1> type is 'const char [17]'

I have been trying to defeat this issue, but I fail. I have searched nearly everything. If anyone could either point me in the right direction or just help me, I'll be happy.

just incase...im trying to just open whatever i want. so a button for facebook to open firefox with fb open. or a button for starcraft for example, then it can open starcraft.

Sorry for poor spelling/grammar...lack of sleep/idc

thanks for your time and help in advance!

-chris

You really should have posted the errors at each step, then you could have tried to help with the original problem rather than the problems with the non-working solutions to the original problem.

For starters "www.facebook.com".c_str() the problem here is that "www.facebook.com" is not a string object so you can call the c_str method. You could correct this by creating a temporary string object std::string("www.facebook.com").c_str() however c_str returns const char* and a string constant is const char* so std::string("www.facebook.com").c_str() and "www.facebook.com" are functionally equivilent, it would not solve any problems.

Your initial problem appears to be you are trying to pass an ASCII string to a function that expects a UNICODE string, the easiest solution might be to use UNICODE string constants by pre-pending an L to them

ShellExecute(NULL, L"open", L"www.facebook.com", NULL, NULL, SW_SHOWNORMAL);

rather than messing around with your compiler settings.

Hello,

Thanks for taking the time to help. I did as you said, but got an error. As of right now, there is one button on this form. Im trying to just get it to open facebook for now so I can at least do that. So heres what under the button

#pragma endregion
	private: System::Void btnFacebook_Click(System::Object^  sender, System::EventArgs^  e) {
				 ShellExecute(NULL, L"open", L"www.facebook.com", NULL, NULL, SW_SHOWNORMAL);
			 }
	};
}

and I got

1>error C2664: 'ShellExecuteA' : cannot convert parameter 2 from 'const wchar_t [5]' to 'LPCSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

And i have this exact program but is cmd prompt. works perfect, but use a switch case for users input.

Maybe this helps?

Is there another way to do this?

Thanks again!

-Chris

Edited 4 Years Ago by Keeame: n/a

You still have the altered setting of "menu->properties->general->character set", change it back to UNICODE.

Notice your errors are on ShellExecuteA and ShellExecuteW when you call ShellExecute? ShellExecute is a macro and is defined to the real functions ShellExecuteA or ShellExecuteW depending on your character set chosen for compilation. ShellExecuteA is the ASCII version of the function ShellExecuteW is the unicode version of the function.

You input parameters need to match the input parameters expected by the function you are calling.

Edited 4 Years Ago by Banfa: n/a

This article has been dead for over six months. Start a new discussion instead.