Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
Is there a portable way in standard C++ to retrieve hostname?

I'm working on a C++ program that needs to use the hostname of the computer it is running on. My current method of retrieving this is by mangling a C API like this:

char *host = new char[1024];
gethostname(host,1024);
auto hostname = std::string(host);
delete host;

Is there a portable modern C++ method for doing this, without including a large external library (e.g., boost)?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

No, there is no standard C++ support for this. You'll either have to make your own function, or get a library that has this functionality.

over 4 years ago · Santiago Trujillo Relatório

0

#include <string>
//#include <winsock.h>      //Windows
//#include <unistd.h>       //Linux

std::string GetHostName(void)
{

    std::string res = "unknown";
    char tmp[0x100];
    if( gethostname(tmp, sizeof(tmp)) == 0 )
    {
        res = tmp;
    }
    return res;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda