25 {
26 std::string ipAddress = "";
28 std::string command = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"" + scriptPath.string() + "\"";
29
30 FILE* pipe = _popen(command.c_str(), "r");
31 if (!pipe)
32 {
33 std::cerr << "Erro: Falha ao executar o script PowerShell. Comando: " << command << std::endl;
34 return ipAddress;
35 }
36
37 std::array<char, 256> buffer;
38 std::string line;
39
40 if (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
41 {
42 line = buffer.data();
43 line.erase(0, line.find_first_not_of(" \t\r\n"));
44 line.erase(line.find_last_not_of(" \t\r\n") + 1);
45 ipAddress = line;
46 }
47
48 _pclose(pipe);
49 std::regex ipv4_format_regex("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$");
50 if (!std::regex_match(ipAddress, ipv4_format_regex))
51 {
52 std::cerr << "Aviso: O script PowerShell retornou algo que não parece um IPv4 válido ou nada: '" << ipAddress << "'" << std::endl;
53 return "";
54 }
55
56 return ipAddress;
57 }
static fs::path getResPath()