Search code examples
c++shellexecute

Trying to open up a url address with an executable


I'm trying to open a user input url using ShellExecute in my c++ project but am finding difficulty in creating a main function within the project.

Currently I have

#include<Windows.h>
#include<stdlib.h>
#include<shellApi.h>

int findIE(const char*);

int main()
{
    const char* url;
    findIE(url);
    return 0;
}

/**
 * Open the default browser to the specified URL.
 * 
 * \param url     WebAddress to open a browser to.
 *  
 * \returns 
 * If the function succeeds, it returns a value greater than 32.
 * Otherwise,it returns an error code as defined in the ShellExecute   documentation.
 *
 * \remarks 
 * The function uses the headerfiles: windows.h, stdlib.h, shellapi.h.
 * The url is converted into a unicode string before being used.
 **/

int findIE(const char* cUrl)
{
    ShellExecute(NULL, "open", cUrl, NULL, NULL, SW_SHOWDEFAULT);
    return 0;
}

I try going to my executable and running it but nothing is showing up...Can I get some advice on my next steps?

The program should be run:

findIE.exe websitename.com

Then open up the default browser to websitename.com

Thanks for responding!


Solution

  • The program should be run:

    findIE.exe websitename.com

    Ah, then you need to pass command line arguments to main.

    At the very least:

    int main( int argc, char ** argv )
    {
        if ( argc >= 2 )
        {
            findIE( argv[1] );
        }
        return 0;
    }