Link to home
Start Free TrialLog in
Avatar of ranadhir
ranadhir

asked on

converting to and from TCHAR

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?
Avatar of rajeev_devin
rajeev_devin

This way

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

SomeFunction(std::string& password,std::string& encryptPwd)
{
}
Or,

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

SomeFunction(std::wstring& password,std::wstring& encryptPwd)
{
}
ASKER CERTIFIED SOLUTION
Avatar of rajeev_devin
rajeev_devin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ranadhir

ASKER

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.
Got you
std::string password = szPassword;
std::string encryptPwd = szEncryptPwd;

Use password.c_str()
Avatar of ikework
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
sorry rajeev, didn't see your last post, when writing mine ... ;-)
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!
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));
}
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?
>>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.
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?
>>>> 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

Hi,

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

Best Regards,
DeepuAbrahamK