Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / string

Converting TCHAR[] to string, while getting PC Name

4.67/5 (3 votes)
9 Jun 2010CPOL 12.4K  
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)