Results 1 to 5 of 5

Thread: Get the local computer name usingQT4C++

  1. #1
    Join Date
    Oct 2008
    Beans
    68

    Get the local computer name usingQT4C++

    Hi
    I was wondering if someone knows how to get the user name of the computer using QT4c++
    I found something, but it will on work on windows, and it for c++,not using the QT4 functions
    Code:
    #include <windows.h>
    #include <string>
    #include <iostream.h>
    
    string GetLocalComputerName() { 
      TCHAR chrComputerName[MAX_COMPUTERNAME_LENGTH + 1]; 
      string strRetVal; 
      DWORD dwBufferSize = MAX_COMPUTERNAME_LENGTH + 1; 
      
      if(GetComputerName(chrComputerName,&dwBufferSize)) { 
        // We got the name, set the return value. 
        strRetVal = chrComputerName; 
      } else { 
        // Failed to get the name, call GetLastError here to get 
        // the error code. 
        strRetVal = ""; 
      } 
    
      return(strRetVal); 
    }
    I supposed there is a way in QT4c++ in where you can get the user name that will work for linux and windows

  2. #2
    Join Date
    Oct 2006
    Location
    Tucson, AZ
    Beans
    1,420
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Get the local computer name usingQT4C++

    IIRC, the getenv() function works correctly on both Windows and Linux. In the case of Linux, your login sets the USER and USERNAME environment variables correctly.

    You might see if Windows follows this convention, and if it does you have an easy solution.

    Lloyd B.
    Don't tell me to get a life.
    I had one once.
    It sucked.

  3. #3
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: Get the local computer name usingQT4C++

    Quote Originally Posted by lloyd_b View Post
    IIRC, the getenv() function works correctly on both Windows and Linux. In the case of Linux, your login sets the USER and USERNAME environment variables correctly.

    You might see if Windows follows this convention, and if it does you have an easy solution.

    Lloyd B.
    I have seen an equivalent in Windows, and have actually done something similar, but have lost the source code...... if memory serves correctly it wasn't through the getenv() function. Sorry I couldn't be of more help.
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  4. #4
    Join Date
    Apr 2006
    Location
    Atlanta, USA
    Beans
    427

    Re: Get the local computer name usingQT4C++

    Check man gethostname.


    Code:
    NAME
           gethostname, sethostname - get/set hostname
    
    SYNOPSIS
           #include <unistd.h>
    
           int gethostname(char *name, size_t len);
    DESCRIPTION
           gethostname() returns the null-terminated  hostname  in  the  character
           array  name,  which  has a length of len bytes.  If the null-terminated
           hostname is too large to fit, then the name is truncated, and no  error
           is  returned  (but  see  NOTES  below).  POSIX.1-2001 says that if such
           truncation occurs, then it is unspecified whether the  returned  buffer
           includes a terminating null byte.
    
    RETURN VALUE
           On  success,  zero is returned.  On error, -1 is returned, and errno is
           set appropriately.
    Here we are, trapped in the amber of the moment. There is no why.

  5. #5
    Join Date
    Feb 2008
    Location
    Boston, MA
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Get the local computer name usingQT4C++

    getenv() will do fine for what you need it to do.

    If you want to hack even more, set up a pipe to "whoami" and read that back.

    The only issue with this is that anyone can set their envvar to something other then their username ( if it is a security app ). In the case of a secure app, one might read the user ID, and lookup against the passwd file.

    Cheers Diego, you rock!

    Paul

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •