尝试使用可执行文件打开URL地址

时间:2015-04-14 17:22:09

标签: c++ shellexecute

我试图在我的c ++项目中使用ShellExecute打开用户输入网址,但是在项目中创建主要功能时遇到了困难。

目前我有

#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;
}

我尝试使用我的可执行文件并运行它但没有任何显示...我可以在接下来的步骤中获得一些建议吗?

该程序应该运行:

findIE.exe websitename.com

然后打开默认浏览器到websitename.com

感谢您的回复!

2 个答案:

答案 0 :(得分:2)

您需要初始化变量&#39; url&#39;。

例如:

int main()
{
const char* url = "www.google.com"
findIE(url);
return 0;
}

如果你想使用用户输入,你将不得不取消char变量的常量。

答案 1 :(得分:2)

  

该程序应该运行:

     

findIE.exe websitename.com

啊,那么你需要pass command line arguments to main

至少:

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