Solved

converting to and from TCHAR

Posted on 2006-10-23
14
6,114 Views
Last Modified: 2013-12-14
We have a function which has the following signature
SomeFunction(TCHAR* szPassword,TCHAR* szEncryptPwd)
Now we need to pass in a std::string as the first parameter,and convert the second parameter back into a std::string on return.
How is the conversion from std::string to TCHAR* and back to std::string done?
0
Comment
Question by:ranadhir
  • 4
  • 3
  • 3
  • +3
14 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
This way

TCHAR* szPassword = "test";
std::string password = szPassword;
std::string encryptPwd = szEncryptPwd;

SomeFunction(std::string& password,std::string& encryptPwd)
{
}
0
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
Or,

TCHAR* szPassword = "test";
std::wstring password = szPassword;
std::wstring encryptPwd = szEncryptPwd;

SomeFunction(std::wstring& password,std::wstring& encryptPwd)
{
}
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 50 total points
Comment Utility
Which one to select depends on your project settings.
If your project is UNICODE, then TCHAR* is wchar_t*, else TCHAR*is char*.
std::wstring is for unicode string std::string is for ascii string.
0
 

Author Comment

by:ranadhir
Comment Utility
i think you did not get the question.We cannot change the function interface.So if i pass in std::string it says
cannot convert parameter 1 from 'std::string' to 'TCHAR *'
How do i pass in the std::string?
The project build is not unicode.
0
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
Got you
std::string password = szPassword;
std::string encryptPwd = szEncryptPwd;

Use password.c_str()
0
 
LVL 20

Expert Comment

by:ikework
Comment Utility
hi ranadhir,

std::string param1 = "blablabla";
std::string param2 = "blablabla";

// use c_str() member of std::string to convert to char*, which is TCHAR*
// in a non-unicode project, as rajeev said already

SomeFunction( param1.c_str(), param2.c_str() );


// rajeev showed already, how to convert back to std::string

std::string SomeFunction(TCHAR* szPassword,TCHAR* szEncryptPwd)
{
    ....
    // return std::string
    return std::string( szEncryptPwd );
}


hope it helps,
ike
0
 
LVL 20

Expert Comment

by:ikework
Comment Utility
sorry rajeev, didn't see your last post, when writing mine ... ;-)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
Comment Utility
Hi ranadhir,
> Now we need to pass in a std::string as the first parameter,and convert
> the second parameter back into a std::string on return.

If you want to do this so it works when TCHAR is of UNICODE type and when TCHAR is of ANSI type, then you really want to use a template wrapper function, or an overloaded function.

//Overloaded functions
void ConvertToString(const char* Src, std::string &target)
{
    target = Src;
}

void ConvertToString(const wchar_t* Src, std::string &target)
{
  //Use conversion routine here
}

Example usage:

TCHAR* src = _T("Hello World");
std::string Target;

ConvertToString(src, Target);



David Maisonave (Axter)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Here's implementation for the conversion:


void ConvertToString(const wchar_t* Src, std::string &target)
{
    target.resize(wcslen(Src)+1, 0);
    wcstombs(target.begin(), Src, Src + wcslen(Src));
}
0
 

Author Comment

by:ranadhir
Comment Utility
Well i had tried that(using c_str member) earlier and i get the error cannot convert const char* to TCHAR*.
Is a simple cast safe?
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>Is a simple cast safe?

IMHO, cast is never safe, and there are many well known C++ authors that will tell you that.

If you use the ConverToString overloaded functions which I posted, you will not need to cast.

You should not cast with a TCHAR, because a TCHAR can be an ANSI type or UNICODE.
0
 

Author Comment

by:ranadhir
Comment Utility
i need not cast tot tchar- i am just casting to char* (castign away constness)- and since we would not need to build unicode at any time(definitive) ,this should be fine,right?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> The project build is not unicode.

If you don't intend to go to UNICODE you should change TCHAR to char, LPCTSTR to LPCSTR, LPTSTR to LPSTR and omit all _T and TEXT macros. That "T" stuff is made to easily (??) switch between UNICODE and non-UNICODE but is nonsense if used in an environment that doesn't apply it consequently for all sources...

>>>> How do i pass in the std::string?

if the required argument is a TCHAR* you shouldn't pass a std::string. You would need to make a writeable copy of the internal char array std::string supplies via cstr():

TCHAR* string2TCHARPtr(const std::string& s, int requiredSize)
{    
     const char* psz = s.c_str();
     if (requiredSize <= s.length())
         requiredSize = s.length()+1;
     TCHAR* ptsz = new TCHAR[requiredSize ];
     if (sizeof(TCHAR) == 1)
     {
           strcpy(ptsz, psz);
     }
     else
     {
           mbstowcs(ptsz, psz, requiredSize );
     }
     return ptsz;
}

However, you should check whether you really need a writeable TCHAR* buffer. If not change the argument type to const TCHAR*. If additionally you know for sure that you never will use it with UNICODE strings you simply can pass s.c_str() when a const TCHAR* is required.

>>>> You should not cast with a TCHAR,

... because std::string.c_str() is a pointer to the internal string buffer which might get corrupted if the callee writes to the casted buffer.

>>>> and convert the second parameter back into a std::string on return

std::string TCHARPtr2string(const TCHAR* ptsz)
{
     if (sizeof(TCHAR)==1)
           return ptsz;
     int len = wcslen(ptsz);
     char* psz = new char[2*len + 1];
     wcstombs(psz, ptsz, 2*len + 1);
     std::string s = psz;
     delete [] psz;
     return s;
}

Regards, Alex

0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
Hi,

Maccros like A2T() & T2A() can also do the trick.

Best Regards,
DeepuAbrahamK
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now