[英]C++ open link with ShellExecute
If I write like this: 如果我这样写:
ShellExecute(NULL, "open", "www.google.com", NULL, NULL, SW_SHOWNORMAL);
Everything's okay and is as it has to be. 一切都很好,就像它一样。
But I want so that user could enter a link where he wants to go. 但我希望用户可以输入他想去的链接。
std::cout<<"Enter the link: ";
char link;
std::cin>>link;
ShellExecute(NULL, "open", link, NULL, NULL, SW_SHOWNORMAL);
In this case I get an invalid conversion from 'char' to 'const CHAR*
error. 在这种情况下,我得到
invalid conversion from 'char' to 'const CHAR*
错误的invalid conversion from 'char' to 'const CHAR*
。
So, is there a way to do this properly? 那么,有没有办法正确地做到这一点?
Your code only gets one character in as the link. 您的代码只能获得一个字符作为链接。 You need to make link a type able to hold the value of the link and also read stdio in. Making link a std::string will do this but then you need to take care of how it is passed to ShellExecute
您需要使链接成为能够保存链接值的类型,并且还要读取stdio。使链接成为std :: string将执行此操作但是您需要处理它如何传递给ShellExecute
std::cout<<"Enter the link: ";
std::string link;
std::cin>>link;
ShellExecute(NULL, "open", link.c_str(), NULL, NULL, SW_SHOWNORMAL);
You should declare your input as char* 您应该将输入声明为char *
char *link = new char[2048];
...
delete[] link;
The const char* in ShellExecute is just a promise that it won't change the input. ShellExecute中的const char *只是一个不会改变输入的承诺。 After changing the declaration, everything should work as expected.
更改声明后,一切都应按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.