Click here to Skip to main content
15,867,834 members
Articles / General Programming / String
Alternative
Tip/Trick

Converting TCHAR[] to string, while getting PC Name

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
8 Jun 2010CPOL 11.4K   1   3
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)


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSee the allowed characters for lpComputerName in MSDN: http:... Pin
Alain Rist29-Jul-10 11:00
Alain Rist29-Jul-10 11:00 
GeneralI presume GetComputerNameA returns only ansi characters so a... Pin
cpsj1729-Jul-10 9:02
cpsj1729-Jul-10 9:02 
GeneralWell, probably easier to do. Thanks Pin
WajihUllahBaig9-Jun-10 5:55
WajihUllahBaig9-Jun-10 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.