65.9K
CodeProject is changing. Read more.
Home

Converting TCHAR[] to string, while getting PC Name

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (3 votes)

Jun 9, 2010

CPOL
viewsIcon

12420

You might have considered using ::GetComputerNameA() which does the conversion for you:std::string GetSystemName(){ CHAR sBuf[MAX_COMPUTERNAME_LENGTH + 1] = {0}; DWORD dwLen = MAX_COMPUTERNAME_LENGTH; ::GetComputerNameA(sBuf, &dwLen); return std::string(sBuf);}Note...

You might have considered using ::GetComputerNameA() which does the conversion for you:
std::string GetSystemName()
{
    CHAR sBuf[MAX_COMPUTERNAME_LENGTH + 1] = {0};
    DWORD dwLen = MAX_COMPUTERNAME_LENGTH;
    ::GetComputerNameA(sBuf, &dwLen);
    return std::string(sBuf);
}
Note that you can do the same for any system function with a xxxA version. cheers, AR