View Full Version : How to /actually/ open a web page from win32 code?
Applewood
10-08-2011, 05:32 AM
I'm currently calling this on a button click in my win32 c++ app:
ShellExecute(NULL,"open",Entry.ShopURL,NULL,NULL,SW_SHOWNORMAL);
Just like it says in various MS and other websites.
My problem is that I would like that piece of code to actually open the default browser at the given location, as opposed to nothing happening, getting a 5 return from the call and a debug spew of "First-chance exception at 0x7c812afb in test.exe: 0x80040155: Interface not registered."
*sigh* Anyone know more than Microsoft about windows programming?
Thanks! (I'm testing on XP btw)
BantamCityGames
10-08-2011, 06:32 AM
Nope, mine looks just like yours...
ShellExecute(NULL, "open", "http://www.invadazoid.com", NULL, NULL, SW_SHOWMAXIMIZED);
This seems to work at least for IE and Firefox for me.
Applewood
10-08-2011, 06:57 AM
Tiresome!
When I get an ACCESS_DENIED from this call, I fallback to the below which actually works. (Assuming you actually have iexplore)
ShellExecute(NULL,"open","iexplore",Entry.ShopURL,NULL,SW_SHOWNORMAL);
Adrian Lopez
10-08-2011, 09:29 AM
Both calls work fine on my system, but here's something you should try, just in case:
Because ShellExecute can delegate execution to Shell extensions (data sources, context menu handlers, verb implementations) that are activated using Component Object Model (COM), COM should be initialized before ShellExecute is called. Some Shell extensions require the COM single-threaded apartment (STA) type. In that case, COM should be initialized as shown here:
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)
There are certainly instances where ShellExecute does not use one of these types of Shell extension and those instances would not require COM to be initialized at all. Nonetheless, it is good practice to always initalize COM before using this function.
I don't know if that would make any difference in your case (it works fine for me without it), but at least it's something to try.
BantamCityGames
10-08-2011, 10:43 AM
Could the access denied be because the user has limited account privileges?
Applewood
10-08-2011, 01:16 PM
Tried that thanks Adrian, didn't help.
Bruno, I thought this might be it as well, but I'm on XP with a single full-access account. It's presumably gonna fail even harder for me on Vista, lol
Bad Sector
10-08-2011, 05:03 PM
Well, assuming Windows NT or greater, this might work
WinExec("cmd.exe /c start http://maps.google.com/", SW_HIDE);
(cmd.exe is only found in Windows NT and later... :-P)
Adrian Lopez
10-08-2011, 06:49 PM
Could it be a problem with shell32.dll not being properly registered (http://www.pcworld.com/article/126116/windows_tips_fix_windows_glitches_by_reregistering _your_dlls.html)? ShellExecute resides in shell32.dll, so try the following command:
regsvr32 /i shell32.dll
Applewood
10-09-2011, 05:28 AM
Tried that as well :(
It must just be something about my pc, i'll try it on others when I get back to work. Bizarre.
cliffski
10-09-2011, 06:49 AM
I do this, if it helps?
void LaunchWebPage(char* url)
{
//launches the default browser and sends it to given URL
SHELLEXECUTEINFO sh;
memset(&sh, 0L, sizeof(SHELLEXECUTEINFO));
sh.cbSize = sizeof(SHELLEXECUTEINFO);
sh.lpVerb = "open";
sh.lpFile = url;
sh.nShow = SW_NORMAL;
if(!ShellExecuteEx(&sh))
{
DWORD dw = GetLastError();
char errorbuffer[256];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,NULL,
dw,0,
(LPTSTR)errorbuffer,0,NULL);
GetDebug()->DebugOut(errorbuffer);
}
}
Applewood
10-09-2011, 08:03 AM
Thanks Cliff, I'll give that a bash shortly.
Similar result. I actually got an error box from Firefox which at least signifies that Firefox tried to work.
ManuTOO
10-17-2011, 12:30 AM
I use this :
ShellExecute(MainWindow, "open", "http://www.managames.com", NULL, NULL, Show);
MainWindow is the HWND to my program window.
If you did a CoInitialize() before, it won't work, though.
So in this case, I use :
ShellExecute(NULL, "open", "Data\\OpenURL.exe", "http://www.managames.com", NULL, SW_HIDE);
OpenURL.exe is just a lil program that does :
ShellExecute(MainWindow, "open", pCommand, NULL, NULL, Show);
Applewood
10-17-2011, 01:56 AM
I think that sounds like the answer, thanks. I do the CoInitialise thing as part of starting up XAudio2.
Shame it requires such a hack to work though. What a shower, lol
cliffski
10-17-2011, 02:42 AM
You can't be too hard on microsoft. They have a backwards compatibility nightmare going back to windows 3.11 and before.
Imagine if you were forced to use the code you wrote fifteen years ago, for your latest project.
I bet everyone in microsoft would give their right arm for the chance to scrub windows and start again, but it can never happen.
Applewood
10-17-2011, 02:44 AM
See windows 8 :)
Bad Sector
10-17-2011, 04:56 AM
Windows 8 is still the old Win32 API with yet another layer piled on top. I tried the 64bit public beta and i could even run a sample of OpenWatcom (which i installed because the bundled Visual Studio Express somehow couldn't create C/C++ projects) that was written for Win16 :-P
Applewood
10-17-2011, 05:08 AM
Oh well. Maybe by windows 9 we'll all be running iOS anyway...
Powered by vBulletin™ Version 4.1.3 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.