如何解决如何使我的EXE可以在控制台上接受参数?
| 这是我用来玩的代码。我想使此代码可以接受来自控制台的参数。 现在,我只能运行带有硬编码参数的代码。在控制台上,我只键入Example1Client.exe
,然后按Enter。
我想像这样发送参数:Example1Client.exe http://www.website.com
int main()
{
RPC_STATUS status;
unsigned char* szStringBinding = NULL;
// Creates a string binding handle.
// This function is nothing more than a printf.
// Connection is not done here.
status = RpcStringBindingCompose(
NULL,// UUID to bind to.
reinterpret_cast<unsigned char*>(\"ncacn_ip_tcp\"),// Use TCP/IP
// protocol.
reinterpret_cast<unsigned char*>(\"localhost\"),// TCP/IP network
// address to use.
reinterpret_cast<unsigned char*>(\"4747\"),// TCP/IP port to use.
NULL,// Protocol dependent network options to use.
&szStringBinding); // String binding output.
if (status)
exit(status);
// Validates the format of the string binding handle and converts
// it to a binding handle.
// Connection is not done here either.
status = RpcBindingFromStringBinding(
szStringBinding,// The string binding to validate.
&hExample1Binding); // Put the result in the implicit binding
// handle defined in the IDL file.
if (status)
exit(status);
RpcTryExcept
{
visit(\"http://www.facebook.com\");
openNew(\"http://www.yahoo.com\");
}
RpcExcept(1)
{
std::cerr << \"Runtime reported exception \" << RpcExceptionCode()
<< std::endl;
}
RpcEndExcept
// Free the memory allocated by a string.
status = RpcStringFree(
&szStringBinding); // String to be freed.
if (status)
exit(status);
// Releases binding handle resources and disconnects from the server.
status = RpcBindingFree(
&hExample1Binding); // Frees the implicit binding handle defined in
// the IDL file.
if (status)
exit(status);
}
// Memory allocation function for RPC.
// The runtime uses these two functions for allocating/deallocating
// enough memory to pass the string to the server.
void* __RPC_USER midl_user_allocate(size_t size)
{
return malloc(size);
}
// Memory deallocation function for RPC.
void __RPC_USER midl_user_free(void* p)
{
free(p);
}
解决方法
将主电源的定义修改为:“ 3”而不是“ 4”。
在main内部,您可以具有以下代码:
std::string url = \"some default url\";
if (argc > 1) {
url = argv[1];
}
关于Stackoverflow的类似问题:
在C中将文件名作为参数传递
传递命令行参数
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。