Switch to Bing in English
Copilot
Your everyday AI companion
About 1,120,000 results
    Upvotes29edited Jan 6, 2017 at 8:18

    Windows

    You can try to use GetComputerName and GetUserName, here is a example:

    #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 );
    Content Under CC-BY-SA license
    Was this helpful?
  1. GetComputerNameA 関数 (winbase.h) - Win32 apps ...

  2. GetComputerNameA function (winbase.h) - Win32 apps

  3. People also ask
    The GetComputerName function retrieves the NetBIOS name established at system startup. Name changes made by the SetComputerName or SetComputerNameEx functions do not take effect until the user restarts the computer. If the caller is running under a client session, this function returns the server name.
    The winbase.h header defines GetComputerName as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors.
    You should be able to simply do if (get_computer_name () == "USER"), because the == operator is overloaded for parameters of type std::string and char*.
    Use gethostname() to get computer name, support both windows and linux. Regarding Denis's answer, note that getenv("HOSTNAME") for Linux may not always work because the environment variables may not be exported to the program. Multi-platform C++ code example to fetch just the computer name (this is what worked for my Win7 and CentOS machines):
  4. c++ - Get Computer Name and logged user name - Stack ...

  5. Win32API コンピュータ名を取得する GetComputerName - s-kita ...

  6. コンピュータ名を取得する - わびさびサンプルソース

  7. 自分のコンピューター名を表示する

  8. Retrieve the name of the computer and saved it in a variable

  9. GetComputerName でコンピュータ名を取得 - プロバイダー ...

  10. GetComputerNameExA function (sysinfoapi.h) - Win32 apps

  11. Some results have been removed
By using this site you agree to the use of cookies for analytics, personalized content, and ads.Learn more about third party cookies|Microsoft Privacy Policy