0
BYTE name[1000];

In my visual c++ project there is a variable defined name with the BYTE data type. If i am not wrong then BYTE is equivalent to unsigned char. Now i want to convert this unsigned char * to LPCTSTR.

How should i do that?

Amit Pal
  • 10,604
  • 26
  • 80
  • 160

2 Answers2

3

LPCTSTR is defined as either char const* or wchar_t const* based on whether UNICODE is defined or not.

  • If UNICODE is defined, then you need to convert the multi-byte string to a wide-char string using MultiByteToWideChar.

  • If UNICODE is not defined, a simple cast will suffice: static_cast< char const* >( name ).

This assumes that name is a null-terminated c-string, in which case defining it BYTE would make no sense. You should use CHAR or TCHAR, based on how are you operating on name.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

You can also assign 'name' variable to CString object directly like:

CString strName = name;

And then you can call CString's GetBuffer() or even preferably GetString() method which is more better to get LPCTSTR. The advantage is CString class will perform any conversions required automatically for you. No need to worry about Unicode settings.

LPCTSTR pszName = strName.GetString();
Vishwanath Kamath
  • 340
  • 2
  • 3
  • 14