Thread: How do I get a computer to detect its own name and hardware?

  1. #1
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24

    How do I get a computer to detect its own name and hardware?

    Hello all. I'm still learning the basics of C++ and at the moment I'm working on a nice little program that lets the user 'have a conversation' with the computer. The computer asks the user various questions and saves the answers as variables etc.

    I was wondering if there is a way to get the computer name? For example, my computer is called KING (named it after my xbox - did you know every xbox has a name?). If the program was running on my computer it should go like this:

    (computer) Hi there, what's your name?
    (me) Edd.
    (computer) Hello Edd, my name's KING.

    etc etc, you get the picture. Is it possible to get C++ to read the name of the machine it is running on? It'd be quite a nice program if the computer can tell the user about itself. It's like 2 people getting to know each other, only one is a machine.

    At the end of the program it tells the user that if they learned C++ then they could have a much more in-depth conversation, as it doesn't know English too well.

    The whole idea is inspired by me learning C++, I was thinking how it is a language, a way to communicate with a computer to get it to do what you want. Writing this program will hopefully inspire more people to start learning C++.

    Also, if anyone has any suggestions of things to include that'd be good too. I can get the program to ask the user whatever I want it to, but I don't really know how to get the computer to tell the user about itself. I don't suppose a machine knows how old it is? Or how long ago the OS was installed or something? How to I get it to detect what hardware it has? It'd be nice for the computer to be able to say things like "I have a 256 megabyte graphics card" or stuff.

    Also, I leave my PC on 24/7 and reboot it about once a week, does my computer know this? Does it know how long it's been 'awake'?

    Basically I'd like to know what sort of things the computer knows about itself and what I could easily get it to say. Any help would be greatly appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Location
    San Juan, Puerto Rico, USA
    Posts
    2
    Hi there!

    I am reading your post and was facinated by your idea of interactive dialogue with your computer. I have no answers for you at present but will look for some amongst my geek friends. :-)

    ttyl

  3. #3
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    hehe, thanks, glad you like the idea.
    ill quite happily host the exe and the source once its done.
    its only a simple program but its a nice idea and would hopefully inspire others to code.

    edit: hmm... this is starting to look like a much more complicated task that i thought it wouldve been, i cant seem to find anything on it.
    Last edited by eddwills; 02-19-2006 at 05:32 PM.

  4. #4
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Hi.
    It is not hard to find it but you just have to get used to MSDN.
    You probably would have more result on the windows forum, this is a windows function, anyway:

    PHP Code:
    #include <iostream>
    #include <windows.h>

    using namespace std;

    string Name;

    char Computer_Name 256 ];

    DWORD Size sizeof Computer_Name );

    int main()
    {

        
    GetComputerName Computer_Name, & Size );

        
    cout<< "Hi, what is your name?: ";
        
    cin>> Name;
        
    cout<< "Hello "<< Name<< ", my name is "<< Computer_Name;
        
    cin.ignore();
        
    cin.get();

    -Yuri

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    On Windows 95-Vista, you might consider GetComputerName.

    :: Edit
    Meh...
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by eddwills
    It's like 2 people getting to know each other, only one is a machine.
    uhh yeah...

    edit: hmm... this is starting to look like a much more complicated task that i thought it wouldve been, i cant seem to find anything on it.
    Yes, it's easy enough to create a program that knows how to respond to a specific user input but there are many ways to ask the same question. Natural language processing is quite complicated if you want to learn some more try googleing "turing test"

  7. #7
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    thanks guys, i was just coming back to say i found this example...
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
      char buffer[MAX_COMPUTERNAME_LENGTH + 1]; 
      
      DWORD size = MAX_COMPUTERNAME_LENGTH + 1 ;
      
      GetComputerName( buffer, &size );
      
      printf( "Computer name = %s\n", buffer );
      printf( "Characters copied = %d\n", size );
      
      getchar();
    }
    ...for GetComputerName.

    Cheers for the MSDN link, I'll have to remember to look there again in future. Oh, and yeah I forgot to say I'm running windows xp, sorry about that. Any ideas on what else the computer knows about itself?

    edit:
    Quote Originally Posted by Quantum1024
    Yes, it's easy enough to create a program that knows how to respond to a specific user input but there are many ways to ask the same question. Natural language processing is quite complicated if you want to learn some more try googleing "turing test"
    im fine with the user input and whatnot, its just finding out computer specs that's baffling me. im still a total beginner at this, although i do know a fair bit of basic.
    Last edited by eddwills; 02-19-2006 at 05:55 PM.

  8. #8
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    wow, msdn is pretty good! theres loads of stuff!
    GetKeyboardType, GetSystemInfo, GetUserName, GetVersion, loads of stuff! Thanks for showing me bud

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by eddwills
    Also, I leave my PC on 24/7 and reboot it about once a week, does my computer know this? Does it know how long it's been 'awake'?
    Here's code I've found and used for calculating uptime.

    Code:
    //time.h
    class TIME
    {
    public:
    	char szUptime[128];
    
    	DWORD ticks;
    
    	DWORD days;
    	DWORD hours;
    	DWORD minutes;
    	DWORD seconds;
    	DWORD millisecs;
    
    	DWORD thours;
    	DWORD tminutes;
    	DWORD tseconds;
    	DWORD tmillisecs;
    
    	DWORD udays;
    	DWORD uhours;
    	DWORD uminutes;
    	DWORD useconds;
    	DWORD umillisecs;
    
    	TIME();
    	void GetUptime();
    };
    
    TIME::TIME()
    {
    	ZeroMemory(szUptime, 128);
    
    	ticks = 0;
    
    	days = 0;
    	hours = 0;
    	minutes = 0;
    	seconds = 0;
    	millisecs = 0;
    
    	thours = 0;
    	tminutes = 0;
    	tseconds = 0;
    	tmillisecs = 0;
    
    	udays = 0;
    	uhours = 0;
    	uminutes = 0;
    	useconds = 0;
    	umillisecs = 0;
    }
    
    void TIME::GetUptime()
    {
    	DWORD tTicks = 0;
    
    	ticks = GetTickCount();
    	tTicks = ticks;
    
    	udays = tTicks/(1000*60*60*24);
    	tTicks -= udays*(1000*60*60*24);
    
    	uhours = tTicks/(1000*60*60);
    	thours = uhours + (udays*24);
    	tTicks -= uhours*(1000*60*60);
    
    	uminutes = tTicks/(1000*60);
    	tminutes = uminutes + (thours*60);
    	tTicks -= uminutes*(1000*60);
    
    	useconds = tTicks/(1000);
    	tseconds = useconds + (tminutes*60);
    	tTicks -= useconds*(1000);
    
    	umillisecs = tTicks;
    	tmillisecs = ticks;
    }
    Code:
    //main.cpp
    
    #include <windows.h>
    #include "time.h"
    
    int WinMain(HINSTANCE hInst, HINSTANCE hPInst, LPSTR CmdLine, int nCmdShow)
    {
    	char uptime[100];
    	TIME crUptime;
    	crUptime.GetUptime();
    	wsprintf(uptime, "%d days %d hours %d minutes %d seconds %d milliseconds",
     crUptime.udays, crUptime.uhours, crUptime.uminutes, crUptime.useconds, crUptime.umillisecs);
    	MessageBox(NULL, uptime, "UPTIME", MB_OK);
    	return 0;
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    GetTickCount() is supposed to give you the milliseconds since system launch, but it wraps around every once in a while. There might be a better function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    neandrake, that works a treat, thanks! Now to figure out how to incorporate it into my existing code
    It's hard to believe that 2 days ago i had no knowledge of C++ and already im making this, I love it!
    Thanks again for everyone's help.

    It turns out my PC has been on for 3 days and 4 hours now

    Oh, and neandrake, before you posted that I hadn't learned how to use one file in another, but it was completely self explanatory, so thanks for that too.

    edit: I managed to get the time to work now, the computer now tells the user how long it has been 'awake' in the console.
    It really makes things a lot cooler, I can't thank you enough for that, I'll have to credit you or something.
    Last edited by eddwills; 02-19-2006 at 08:19 PM.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    It's hard to believe that 2 days ago i had no knowledge of C++ and already im making this, I love it!
    You aren't learning C++. Functions like GetKeyboardType, GetSystemInfo, GetUserName, GetVersion are not C++, and you will not find them in an C++ text book.

  13. #13
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    Quote Originally Posted by 7stud
    You aren't learning C++. Functions like GetKeyboardType, GetSystemInfo, GetUserName, GetVersion are not C++, and you will not find them in an C++ text book.
    Well... thanks for that...

    I dont really care, at the end of the day im making my own Windows application and having fun. the only functions like those that I used were GetUserName and GetComputerName.

    Just because those aren't in a C++ textbook doesn't mean im 'not learning C++'. Thanks to this I have more knowledge of C++ than I did before, so as far as I'm concerned I am learning C++.
    I'm quite offended to be honest.

  14. #14
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Well, you really shouldn't be concearning yourself with non-standard Windows only code so early on...
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  15. #15
    edd of all trades
    Join Date
    Feb 2006
    Location
    Stafford, England
    Posts
    24
    Quote Originally Posted by SirCrono6
    Well, you really shouldn't be concearning yourself with non-standard Windows only code so early on...
    Yeah, I'll agree to that. I needed it for what I wanted to do though, and it all works, so its not like it's such a bad thing. I'll go back to the tutorials after this. The program's pretty much done and works, I'm just adding in lots of Sleep() to make the computer feel more human, and to make the program feel more friendly. Getting barraged with text isnt fun, and doesn't feel like a conversation. It's taking a while cause I have to keep testing and altering it, and theres a series of if, else if, and else statements with nine outcomes for one question.
    Last edited by eddwills; 02-19-2006 at 09:25 PM.

Popular pages Recent additions subscribe to a feed