Thread: Using WINNT variables in app.

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

    Exclamation Using WINNT variables in 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? Last night I looked through a whole bunch of header files, specifically lmaccess.h and others, but I could find what I was looking for. Thanks for ANY help guys!

    -Mark

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    #define MAX_NAME 256
    
    
    int main()
    {
    	DWORD dwLen = MAX_NAME;
    	char lpUser[MAX_NAME],
    		 lpSystem[MAX_NAME];
    
    	GetUserName(lpUser,&dwLen);
    	GetComputerName(lpSystem,&dwLen);
    
    	cout << "User " << lpUser;
    	cout << " is logged onto " << lpSystem;
    		
    
    	return 0;
    }

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

    Lightbulb THANKS!!!

    Thanks for the assistance! It's working great, except the lpSystem variable isn't returning the legitimate info. I've been scouring the Internet for info regarding these functions, but haven't found much "usable" info. Any ideas?

    -Mark

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: THANKS!!!

    Originally posted by Util_Mark
    Thanks for the assistance! It's working great, except the lpSystem variable isn't returning the legitimate info. I've been scouring the Internet for info regarding these functions, but haven't found much "usable" info. Any ideas?

    -Mark
    info @ MSDN

    Strange the API didnt get the proper PC name..............Are you sure you were expecting the correct name? The name recieved will be the name that the PC uses to identify itself on a LAN....

    The docs say the name is read from the registry.....my guess.....on Windows2000...that name would be under
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro l\ComputerName\ActiveComputerName

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

    Arrow

    Thanks, I'll look at MSDN. I looked at your web site by the way. You know Assembly too, eh? I've always wanted to take a class on it and learn it. Thanks again,

    Mark

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

    Exclamation Borland 5.02?

    Does it matter that I'm using Borland C++ version 5.02???
    Just checking...

    Mark

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

    Re: Re: THANKS!!!

    Originally posted by Fordy


    info @ MSDN

    Strange the API didnt get the proper PC name..............Are you sure you were expecting the correct name? The name recieved will be the name that the PC uses to identify itself on a LAN....

    The docs say the name is read from the registry.....my guess.....on Windows2000...that name would be under
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro l\ComputerName\ActiveComputerName
    Can anyone suggest any other site with good network programming information?

    Mark
    Last edited by Util_Mark; 07-02-2002 at 03:42 PM.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Cross-Posting = evil, read the sticky at the very top that states the rules.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

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

    Arrow

    Sorry 'bout that, but it helped. Swoopy answered my question and it worked the first time! Thanks Swoopy! See below:

    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;
    }
    --------------------------------------------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. How do I make my Linux app standalone?
    By Joda in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2002, 04:53 AM