Thread: how to find the computer name

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    20

    how to find the computer name

    I'm pretty new to this, trying to write a program that compairs 2 lists, unfortunatly the list is created by another program that saves the list as "computername".txt

    this program will be run on several computers and it will need to compair the list on each computer, so I need my program to find the computername so I can put the name in a variable to load the text document.

    How do I get my program to find the workstation name?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That would depend on which Operating System and compiler you're using.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    20
    Im using windows XP and Microsoft visual c++ 6.0... all the computers it will be retrieving the workstation names from are xp

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    One way perhaps
    char *host = getenv( "HOSTNAME" );

    Type 'set' in a console and see which name corresponds to the host name or computer name.
    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
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You may also find GetComputerName of some interest. If the function itself is no good for what you're after then refer to its description for other api's that may be of some use.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    20
    Thanks for you help its working now. I used salem's method, only I changed char to a string and hostname to computername.

    I tryed using the getcomputername way.. I tryed the example on the site.. but I couldn't figure out how to use it, never used an api before although im gonna keep tryin to get it to work.

    well, I appreciate the help!!

    PEACE!
    Last edited by Sephiroth222; 08-13-2005 at 10:20 AM.

  7. #7
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Did you include <windows.h>?

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Shamest why do you think C++ > Java, that liek saying
    apples > oranges, there not the same thing there no
    comparing them.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    20
    yeah I included windows.h .. I can't remember what the error I was getting.. it may not have errored, it was possably that I didn't know how to get the computername from it.

    the site gives this example

    BOOL GetComputerName(
    LPTSTR lpBuffer,
    LPDWORD lpnSize
    );

    Parameters
    lpBuffer
    [out] Pointer to a buffer that receives a null-terminated string containing the computer name or the cluster virtual server name. The buffer size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.
    lpnSize
    [in, out] On input, specifies the size of the buffer, in TCHARs. On output, the number of TCHARs copied to the destination buffer, not including the terminating null character.
    If the buffer is too small, the function fails and GetLastError returns ERROR_BUFFER_OVERFLOW. The lpnSize parameter specifies the size of the buffer required, not including the terminating null character.

    Windows Me/98/95: GetComputerName fails if the input size is less than MAX_COMPUTERNAME_LENGTH + 1.


    I have no clue what to do with that LOL, tryed printing it to the screen and all I could get is a 1. no clue how to use the function.

  10. #10
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Here's an example for GetComputerName.

    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();
    }
    Shamest why do you think C++ > Java, that liek saying
    apples > oranges, there not the same thing there no
    comparing them.
    Wait. You mean if they were the same, you would compare them? :|

    And it's true that apples taste better than oranges. For a given value of true.

    To be honest, I put "C++ > Java" in my title a long long time ago, and haven't thought of a new title. Hmm, maybe "Apples > Oranges"...
    Last edited by Dante Shamest; 08-13-2005 at 12:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  2. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  3. Which distro should I run on my old computer?
    By joshdick in forum Tech Board
    Replies: 5
    Last Post: 04-09-2003, 01:37 AM
  4. Computer engineering - programming.
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 07-15-2002, 02:37 PM
  5. computer sex
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-17-2001, 07:09 PM