Thread: Print computer/system name

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    54

    Print computer/system name

    I need my program to print out the name of the computer. Is this possible through C++ at all?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Code:
    #include<windows.h>
    char buffer[256];
    DWORD len = 256;
    GetComputerName(buffer, &len);
    cout<<buffer<<endl;
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by jinhao
    Code:
    #include<windows.h>
    char buffer[256];
    DWORD len = 256;
    GetComputerName(buffer, &len);
    cout<<buffer<<endl;
    Thank you VERY much. Worked perfectly.
    -Zack

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Next time, say which OS/Compiler you're using.
    It was just a lucky guess that you were using windows.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Salem
    Next time, say which OS/Compiler you're using.
    It was just a lucky guess that you were using windows.
    Ah. Thanks. I'll be sure to state that next time. .
    -Zack

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    heihei, i am so lucky
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    54
    Thanks for the previous help, guys. However, I must compile it this code on a Unix machine now. I'm using the standard Unix build, cc xxx.cpp.

    How should I print out the system name using that? I assume that I should include a different header file than windows.h.

    Thanks in advance.
    -Zack

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Hum.. I dunno .. but you could try to acess the environement variables and try to see if there's something that mathces the computer name
    Code:
    #include <iostream>
    
    int main(int argc, char *argv[], char *env[]){
        int i;
        for(i=0;env[i];i++){
            std::cout<<env[i]<<std::endl;
        }
        return 0;
    }
    NOTE: this main declaration ISN'T standard, although it's provided with many compilers as extension., I think. Please correct me if wrong.

    http://www.opengroup.org/onlinepubs/...bd/envvar.html
    http://www.cplusplus.com/ref/cstdlib/getenv.html

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by xErath
    Hum.. I dunno .. but you could try to acess the environement variables and try to see if there's something that mathces the computer name
    Code:
    #include <iostream>
    
    int main(int argc, char *argv[], char *env[]){
        int i;
        for(i=0;env[i];i++){
            std::cout<<env[i]<<std::endl;
        }
        return 0;
    }
    NOTE: this main declaration ISN'T standard, although it's provided with many compilers as extension., I think. Please correct me if wrong.

    http://www.opengroup.org/onlinepubs/...bd/envvar.html
    http://www.cplusplus.com/ref/cstdlib/getenv.html
    That looks good. Although I'm not sure whether it'll be fine for Unix. I will try it once I get to the office Friday.

    Any other ideas, guys? .
    -Zack

  10. #10
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by xErath
    Hum.. I dunno .. but you could try to acess the environement variables and try to see if there's something that mathces the computer name
    Code:
    #include <iostream>
    
    int main(int argc, char *argv[], char *env[]){
        int i;
        for(i=0;env[i];i++){
            std::cout<<env[i]<<std::endl;
        }
        return 0;
    }
    NOTE: this main declaration ISN'T standard, although it's provided with many compilers as extension., I think. Please correct me if wrong.

    http://www.opengroup.org/onlinepubs/...bd/envvar.html
    http://www.cplusplus.com/ref/cstdlib/getenv.html
    Yeah, that doesn't seem to be working on Unix machines . Any other ideas, guys? .
    -Zack

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    NAME
           uname − get name and information about current kernel
    
    SYNOPSIS
           #include <sys/utsname.h>
    
           int uname(struct utsname *buf);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Salem
    Code:
    NAME
           uname − get name and information about current kernel
    
    SYNOPSIS
           #include <sys/utsname.h>
    
           int uname(struct utsname *buf);
    I get this error - fatal error C1083: Cannot open include file: 'sys/utsname.h': No such file or directory
    Error executing cl.exe.
    -Zack

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh please - you asked for a unix answer and I gave you one.
    Then you turn around and try and compile it with a windows compiler!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Salem
    Oh please - you asked for a unix answer and I gave you one.
    Then you turn around and try and compile it with a windows compiler!
    It has to work on both .
    -Zack

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Tough - it won't, nor can it ever work on both.

    Certainly, there's no standard way to find out.

    I suppose you could find some very common library somewhere, which might happen to tell you which machine you're on...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM