Как сделать так, чтобы мой 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);
}

person karikari    schedule 23.05.2011    source источник
comment
Какая книга, по которой вы учитесь, не охватывает этого?   -  person    schedule 23.05.2011


Ответы (1)


Измените определение основного на: int main(int argc, char *argv[]) вместо int main(). Таким образом, внутри main вы можете иметь следующий код:

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

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

Подобные вопросы по Stackoverflow:

  1. Передача имени файла в качестве аргументов в C
  2. Передача аргументов командной строки
person Ozair Kafray    schedule 23.05.2011