(C++ windows) Open default web browser

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

thx it works perfectly but the app freezes for like 4 seconds then the browser (yeah even firefox) opens ...

greets,
Halan

edit: only if i call it at the beginning of my app so thats no problem
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post by xDan »

I found a bug with this, appName should be the *second* parameter of CreateProcess, not the first, this was causing it to use ShellExecute every time :oops: Probably why it was freezing for you Halan! Also with CreateProcess working you don't seem to have any problems with firewalls (ZoneAlarm asked you for permission before)
(updated code)
CuteAlien
Admin
Posts: 9926
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

I found another bug in your code yesterday and it is so evil that I won't completely spoil the fun telling you what it is. I just give the lines:

Code: Select all

// otherwise if no quotes, 2nd point is the first space after the last \\
       else 
gcc with -Wall will also spoil the fun.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post by xDan »

I'll use -Wall in the future... 8)
bob
Posts: 57
Joined: Fri Jun 08, 2007 4:17 am
Location: Jacksonville, Fl (USA)
Contact:

Post by bob »

No freezing...

Code: Select all

static DWORD WINAPI _RunApp_ThreadProc( LPVOID pData )
{
    TCHAR *pUrl = (TCHAR*)pData;
    if ( !pUrl ) return -1;
 
    // Lowest priority
    ::SetThreadPriority( ::GetCurrentThread(), THREAD_PRIORITY_LOWEST );

    // Start app
    BOOL bSuccess =
        ( ( (DWORD)::ShellExecute( NULL, "open", pUrl, NULL, NULL, SW_SHOWNORMAL ) ) > 32 );

    delete [] pUrl;

    if ( !bSuccess )
        return -2;

    return 0;
}

BOOL RunApp_NoFreeze( LPCTSTR pUrl )
{
    if ( !pUrl || !*pUrl )
        return FALSE;

    TCHAR *pUrlBuf = new TCHAR[ _tcslen( pUrl ) + 1 ];
    strcpy( pUrlBuf, pUrl );

    // Create a thread
    DWORD dwThreadId = 0;
    HANDLE hThread = ::CreateThread( (LPSECURITY_ATTRIBUTES)NULL,
                                     0,
                                     _RunApp_ThreadProc,
                                     (LPVOID)pUrlBuf,   
                                     0,
                                     &dwThreadId );

    if ( NULL == hThread )
        return FALSE;

    ::CloseHandle( hThread );

    return TRUE;
}
*Edit - Forgot to close thread handle

Just so we're clear on proper procedure...

Write Code -> Post Code -> Test Code -> Edit Post
3ddev
Posts: 169
Joined: Tue Sep 19, 2006 6:51 am
Contact:

Post by 3ddev »

This is unnecessarily complex... here is the code which does the same thing. It works on all versions of Windows

Code: Select all

string url = "http://irrlicht.sf.net";
system("iexplore " + url);
Simple and easy. :wink:
JRowe47
Posts: 55
Joined: Sat Jun 30, 2007 9:09 am

Post by JRowe47 »

internet explorer is the devil. Although, your code is good :P
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post by xDan »

3ddev wrote:This is unnecessarily complex... here is the code which does the same thing. It works on all versions of Windows

Code: Select all

string url = "http://irrlicht.sf.net";
system("iexplore " + url);
Simple and easy. :wink:
Genius, pure genius.

@Bob: You still should use CreateProcess instead of ShellExecute. I think. Does CreateProcess even freeze?
bob
Posts: 57
Joined: Fri Jun 08, 2007 4:17 am
Location: Jacksonville, Fl (USA)
Contact:

Post by bob »

I didn't try CreateProcess(), because ShellExecute() opens URLs directly, and always uses the default browser.

You can just do this ... but will likely freeze until the page opens.

Code: Select all

ShellExecute( NULL, "open", "http://irrlicht.sourceforge.net", NULL, NULL, SW_SHOWNORMAL )
I've used the thread template above for lots of things, dowloading files, copying huge files, etc... usually eliminates freezing :)
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post by xDan »

Please read the entire thread, and then contemplate the reasons for your existence. :D
bob
Posts: 57
Joined: Fri Jun 08, 2007 4:17 am
Location: Jacksonville, Fl (USA)
Contact:

Post by bob »

Yeah, I guess that was covered :oops:

I was narrow-minded in addressing your comment. Some of us just have narrow minds you know ... not our fault ... we're just freaks of nature ... fat mindedly challenged. :?

As to my existence, ask the mighty goddess Ate. :)
Post Reply