Link to home
Start Free TrialLog in
Avatar of MattC
MattC

asked on

Getting the machine name

Does anyone know a quick way to get the network name of the machine on which a C++ MFC program is running?
Avatar of jhance
jhance

BOOL GetComputerName(
  LPTSTR lpBuffer,  // computer name
  LPDWORD lpnSize   // size of name buffer
);
Example for putting into a CString.

CString LocalCompName;
char  CompName[128];
DWORD dwSize = sizeof(CompName);
GetComputerName(CompName,&dwSize);
LocalCompName=CString(CompName);
Avatar of MattC

ASKER

Please don't close the question, just use comments.

Jhance,

Would this work.

CString name;

GetComputerName(static_cast(LPTSTR)name,sizeof(name))

ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MattC

ASKER

what header needs including to access that function?
Also what is the maximum value for a machine name is it 128 or 255??
Don't you have the Windows SDK?


  Header: Declared in winbase.h; include windows.h.
  Library: Use kernel32.lib.

The maximum length for a computer name is:

MAX_COMPUTERNAME_LENGTH

also defined in windows.h
Avatar of MattC

ASKER

not been able to get to a PC that does recently, will try tonight...thanks