Retrieving %COMPUTERNAME% variable in my app.

This is a discussion on Retrieving %COMPUTERNAME% variable in my app. within the C++ Programming forums, part of the General Programming Boards category; In Windows NT you can use %USERNAME% and %COMPUTERNAME% in scripts, extracting this info off of PCs. I need to ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    18

    Exclamation Retrieving %COMPUTERNAME% variable in my app.

    In Windows NT you can use %USERNAME% and %COMPUTERNAME% in scripts, extracting this info off of PCs. I need to do the same thing in my simple C++ program. Basically, I need to do this:

    char *thisComputer = %COMPUTERNAME%;
    char *thisUser = %USERNAME%;

    YES, I *know* this isn't correct syntax, - I'm showing this to illustrate what I need my end result to do, not the actual coding itself. Does anyone know if this is possible? Or can point me in the right direction? I've looked through a whole bunch of header files, specifically lmwksta.h, lmbrowsr.h, and others, but all the lm*.h header files require you to use the NETCONS.H file, which I don't have and Microsoft quit supporting/selling quite a while back. Fordy helped me out with the USERNAME part, using lpUser, and that works great, but the lpSystem function isn't giving me the ComputerName as it should. It returns back garbage. Thanks for ANY help guys!

    -Mark
    Last edited by Util_Mark; 07-02-2002 at 08:40 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
       char *thisComputer;
       char *thisUser;
    
       thisComputer = getenv("COMPUTERNAME");
       thisUser = getenv("USERNAME");
       cout << "COMPUTER NAME:" << thisComputer << endl;
       cout << "USER NAME:" << thisUser << endl;
    
       return 0;
    }
    Last edited by swoopy; 07-02-2002 at 12:58 PM.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    18

    Lightbulb

    So simple!!!! Don't I feel stupid now... Thanks a million!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  2. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  3. pasword app
    By GanglyLamb in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 10:28 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 02:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21