We help IT Professionals succeed at work.

Getting the machine name

MattC
MattC asked
on
Does anyone know a quick way to get the network name of the machine on which a C++ MFC program is running?
Comment
Watch Question

Commented:
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);

Author

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

Jhance,

Would this work.

CString name;

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

Commented:
No, you can't do that becuase you cannot directly access the data of a CString in this manner.

Do this:

TCHAR lpszComputerName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD dwSize = MAX_COMPUTERNAME_LENGTH;
GetComputerName(lpszComputerName, &dwSize);

// If you then need it in a CString...
CString name(lpszComputerName);

Author

Commented:
what header needs including to access that function?
Also what is the maximum value for a machine name is it 128 or 255??

Commented:
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

Author

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.