#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    166
    Rep Power
    12

    GetComputerName ?


    Hi. I'm kinda new to C++, i'm just now switching to it from perl so excuse me if my answer is trivial.
    Why does this code not work? I'm trying to get the host name of a computer and store it in hn

    char hn;
    BOOL GetComputerName(hn);
    cout << hn << endl;
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    >> char hn;
    This will declare a variable to hold a single character only. What you want is an array of characters (i.e. C's idea of a string). So you should declare something like this:
    char hn[500];
    This will declare that hn is an array of 500 characters. However, if you really want to be standards-compliant, you would declare it like this:
    char hn[MAX_COMPUTERNAME_LENGTH + 1];

    Also, GetComputerName takes two arguments, not one. See http://msdn.microsoft.com/library/de...mputername.asp for documentation.

    With that said, your code would be rewritten as:
    Code:
    char hn[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD dwLen = MAX_COMPUTERNAME_LENGTH;
    if (GetComputerName(hn, &dwLen) {
        cout << hn << endl;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    166
    Rep Power
    12
    thanks! it works

    man, i love you guys


    ps, just had to add another ) after &dwLen)

IMN logo majestic logo threadwatch logo seochat tools logo