i have a problem conversion from char to LPTSTR as parameter,
could anyone help me,,

for example

char temp[10]="abcde";

how convert to LPTSTR from char above??

thanks

Recommended Answers

All 4 Replies

The problem is that TSTR can be either char or wchar_t, so there's not really much choice beyond allocating an array of TCHAR and copying one into the other with a function that recognizes TCHAR and handles it accordingly:

TCHAR *ttemp = new TCHAR[strlen(temp) + 1]
_tcscpy(ttemp, temp);

I assume you're working with the Windows API which include types like LPSTR or UINT or LPTSTR.

These are just normal types you're familiar with but given a different name to be used specifically on a windows machine.

ie.
UINT = unsigned int
LPSTR = char*
LP stands for "Long Pointer" (just ignore the Long part, so it's just a pointer)
LPCSTR = const char* (a capital C denotes const keyword)

When you see a capital T (as in LPTSTR) it means the code works with Unicode. You can search up Unicode to find out more but in simple terms it allows for many languages and symbols to be supported.

You can do what deceptikon suggested or just declared the string as

TCHAR temp[10] = "abcd";

and depending on the character set you're working with, TCHAR evaluates to char or wchar_t

You can do what deceptikon suggested or just declared the string as

Almost right, you need to surround string literals with _TEXT() macro or _T() macro, they are both the same.

TCHAR temp[10] = _TEXT("abcd");

When you see a capital T (as in LPTSTR) it means the code works with Unicode.

Rather, it means that the code accepts either narrow or wide characters. The Win32 API treats wide characters as UTF-16 encoded Unicode, so while it's technically correct to say that it means you're working with Unicode, I still think it's important to make the distinction between wide characters and Unicode characters as well as the variant nature of TCHAR which depends on a compilation setting. ;)

You can do what deceptikon suggested or just declared the string as

Oh, if only it were always that simple. Yes, if you take the question at face value then always using TCHAR is certainly the better option. But I treated it like an example of the issue rather than the actual code and included the possibility that the OP doesn't have any control over the type of temp, which isn't an unreasonable assumption in my view.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.