2015-01-13 13 views
9

私はアプリケーションを開発中です。メソッドの1つは、マシンにログオンしたコンピュータ名とユーザーを取得してから、その両方をユーザーに表示する必要があります。 WindowsとLinuxの両方で実行する必要があります。これを行う最善の方法は何ですか?コンピュータ名を取得し、ログオンしたユーザー名を

答えて

6

のWindows環境では、Linuxのgetenv("COMPUTERNAME")getenv("USERNAME")
を使用することができます - getenv("USER")

getenv reference

14

のWindowsを参照してください、getenv("HOSTNAME")

あなたがGetComputerNameとを使用しようとすることができは、ここでの例です:

#define INFO_BUFFER_SIZE 32767 
TCHAR infoBuf[INFO_BUFFER_SIZE]; 
DWORD bufCharCount = INFO_BUFFER_SIZE; 

// Get and display the name of the computer. 
if(!GetComputerName(infoBuf, &bufCharCount)) 
    printError(TEXT("GetComputerName")); 
_tprintf(TEXT("\nComputer name:  %s"), infoBuf); 

// Get and display the user name. 
if(!GetUserName(infoBuf, &bufCharCount)) 
    printError(TEXT("GetUserName")); 
_tprintf(TEXT("\nUser name:   %s"), infoBuf); 

参照:GetComputerNameGetUserName

Linuxの

使用gethostnameログインユーザ名を取得するには、コンピュータ名(gethostnameを参照)、およびgetlogin_rを取得します。詳細はman page of getlogin_rをご覧ください。 シンプルな使用方法を次のようにPOSIXシステムで

#include <unistd.h> 
#include <limits.h> 

char hostname[HOST_NAME_MAX]; 
char username[LOGIN_NAME_MAX]; 
gethostname(hostname, HOST_NAME_MAX); 
getlogin_r(username, LOGIN_NAME_MAX); 
+1

(http://stackoverflow.com/questions/30084116/host-name-max-undefined-after-include-limits-h)。 – Antonio

7

あなたはgethostnamegetlogin機能を使用することができますが、両方はunistd.hで宣言されました。

/* 
    This is a C program (I've seen the C++ tag too late). Converting 
    it to a pretty C++ program is left as an exercise to the reader. 
*/ 

#include <limits.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 

int 
main() 
{ 
    char hostname[HOST_NAME_MAX]; 
    char username[LOGIN_NAME_MAX]; 
    int result; 
    result = gethostname(hostname, HOST_NAME_MAX); 
    if (result) 
    { 
     perror("gethostname"); 
     return EXIT_FAILURE; 
    } 
    result = getlogin_r(username, LOGIN_NAME_MAX); 
    if (result) 
    { 
     perror("getlogin_r"); 
     return EXIT_FAILURE; 
    } 
    result = printf("Hello %s, you are logged in to %s.\n", 
        username, hostname); 
    if (result < 0) 
    { 
     perror("printf"); 
     return EXIT_FAILURE; 
    } 
    return EXIT_SUCCESS; 
} 

可能な出力:

Hello 5gon12eder, you are logged in to example.com. 

これは常に存在していない環境変数に頼るよりも安全と思われます。

getlogin

  • manページが実際にgetenv("LOGIN")
  • の賛成でその使用を阻止するので、私は、私が実行したときに上記のプログラムでgetlogin_r呼び出しがENOTTYで失敗している最後の文を引き出しますよプログラムは対話型端末の代わりにEmacsから、getenv("USER")は両方の状況で動作していました。
2

使用gethostname()、コンピュータ名を取得windowslinuxの両方をサポートします。

3

Denisの回答に関しては、(Linuxの場合)may not always work because the environment variables may not be exported to the programに注意してください。

マルチプラットフォームC++のコード例は、単にコンピュータ名を取得するために(これは私のwin7のとCentOSのマシンのために働いていたものです):あなたはブーストを使用することができる場合

char *temp = 0; 
    std::string computerName; 

#if defined(WIN32) || defined(_WIN32) || defined(_WIN64) 
    temp = getenv("COMPUTERNAME"); 
    if (temp != 0) { 
     computerName = temp; 
     temp = 0; 
    } 
#else 
    temp = getenv("HOSTNAME"); 
    if (temp != 0) { 
     computerName = temp; 
     temp = 0; 
    } else { 
     temp = new char[512]; 
     if (gethostbyname(temp, 512) == 0) { // success = 0, failure = -1 
      computerName = temp; 
     } 
     delete []temp; 
     temp = 0; 
    } 
#endif 
1

は、あなたが簡単に取得するためにこれを行うことができますホスト名:[HOST_NAME_MAX]について

#include <boost/asio/ip/host_name.hpp> 
// ... whatever ... 
auto host_name = boost::asio::ip::host_name(); 
+0

これは、この質問に対する感謝の意を表します。 – oLen

関連する問題