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

Trying to open up a url address with an executable

本文关键字:url 地址 可执行文件      更新时间:2023-10-16

我正在尝试在我的 c++ 项目中使用 ShellExecute 打开用户输入 url,但在项目中创建 main 函数时遇到困难。

目前我有

#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

感谢您的回复!

该程序应运行:

findIE.exe websitename.com

啊,那你需要将命令行参数传递给main.

至少:

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

您需要初始化变量 'url'。

例如:

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

如果你想使用用户输入,你将不得不去掉char变量的恒常性。