Click here to Skip to main content
13,049,172 members (68,585 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

12.7K views
2 bookmarked
Posted 17 May 2010

Converting TCHAR[] to string, while getting PC Name

, 17 May 2010
Rate this:
Please Sign up or sign in to vote.
Hi there,Its been a while since I wrote my last article. Since got myself into a problem of getting the system name of my PC.I dragged my C instincts into the MSDN world of asking how to get the PC name.After fiddling for a while I got hold of the PC name, not that difficult as a...
Hi there,

Its been a while since I wrote my last article. Since got myself into a problem of getting the system name of my PC.
I dragged my C instincts into the MSDN world of asking how to get the PC name.

After fiddling for a while I got hold of the PC name, not that difficult as a single line function needed to be called.
But the problem is where I had to convert TCHAR[] to a string. Wow, really never thought I would ever go round and round in circles.


So much for the background. This small code snippet actually helps converting TCHAR array to a simple string. With a delecate approach not to make use or leave a corrupted memory, I wrote down the code with some help from MSDN.
I am amazed that such a simple task usually not that easily available ofter lots of Googling and Yahooing.
Anyaway, here is the code that takes care of converting TCHAR to a simple string.
The code begins with a function that gets the PC name. This encloses the code where the I had to convert a TCHAR[] to a
string.
First, the correct lengths and storage sizes for TCHAR[] and a char* buffer had to be established. After correctly establishing
the correct lengths, all that was left to use the wcstombs_s() function that actually does the conversion. This is essentially
a data copying function which takes care of converting the TCHAR[] to a char*.
wcstombs_s(&size, pMBBuffer, (size_t)size,tchrSystemName, (size_t)size);
After so, a simple assignment of the storage string was performed with the following
line of code.
<br />
strPCName.assign(pMBBuffer);  <br />


<br />
// Here you need to include windows.h,string.h and iostream.h<br />
<br />
using namespace std;<br />
<br />
string GetSystemName()<br />
{  <br />
  string strPCName;// String to hold system name.<br />
  size_t size =  MAX_COMPUTERNAME_LENGTH + 1;  // maximum system name length + 1<br />
  DWORD dwBufferSize = size;  // size of storage buffer.<br />
  TCHAR tchrSystemName[MAX_COMPUTERNAME_LENGTH + 1];  // Allocate the TCHAR memory.<br />
    <br />
  // Get the system name.<br />
  if(GetComputerName(tchrSystemName,&dwBufferSize)) <br />
  {  <br />
	char    *pMBBuffer = (char *)malloc( size );<br />
    wcstombs_s(&size, pMBBuffer, (size_t)size,tchrSystemName, (size_t)size);// Convert to char* from TCHAR[]<br />
	strPCName.assign(pMBBuffer); // Now assign the char* to the string, and there you have it!!! :) <br />
    free(pMBBuffer);<br />
  } <br />
  else // Failed to find the system name.<br />
      strPCName.clear();  <br />
  return strPCName;  <br />
}<br />
<br />
<br />
void main(void)<br />
{<br />
	cout<<GetSystemName().c_str();<br />
}	<br />

License

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

Share

About the Author

wajih_ullah
Engineer -
Pakistan Pakistan
-I am a die hard fan of C/C++ programming. It is poetry and I love the poems of C++.
-I tell people that C# is "NOTHING" compared to C++ and I always say that!

My interest lie in Signal Processing, Image Processing,Video Processing, Pattern Recognition, DSP, Communications. Though I have no ONE stop in terms of interests.

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 17 May 2010
Article Copyright 2010 by wajih_ullah
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid