如何使我的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中将文件名作为参数传递 传递命令行参数     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...