gpt4 book ai didi

c++ - 如何让我的 EXE 可以在控制台接受参数?

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:45 24 4
gpt4 key购买 nike

这是我用来玩弄的代码。我想让这段代码可以接受来自控制台的参数。

现在我只能运行带有硬编码参数的代码。我在控制台输入 Example1Client.exe然后按 Enter。

我想发送这样的参数:Example1Client.exe <a href="http://www.website.com" rel="noreferrer noopener nofollow">http://www.website.com</a>

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

最佳答案

将 main 的定义修改为:int main(int argc, char *argv[]) 而不是 int main()。然后在 main 里面,你可以有如下代码:

std::string url = "some default url";

if (argc > 1) {
url = argv[1];
}

Stackoverflow 上的类似问题:

  1. Passing filename as arguments in C
  2. Passing command line arguments

关于c++ - 如何让我的 EXE 可以在控制台接受参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6093523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com