char to LPCTSTR

I need to pass a char var (declared as  char   ty;)  to a function that expects LPCTSTR.
Who is Participating?
Karl Heinz KremerConnect With a Mentor Commented:
You cannot do this without changing your code. A char is just one character, whereas a LPCTSTR is a pointer to a character array, with a NULL byte indicating the end of the string.

You need to do something like this:

TCHAR dummy[2];

dummy[0] = ty;
dummy[1] = '\0';

YourFunction((LPCTSTR) &dummy);

You may use a string class if you don't want to use dummys.

#include <string>

   string s  =  ty;

Same works with CString of MFC

     CString s = ty;
     f(s);          // CString has a 'cast' operator LPCTSTR

Regards, Alex
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.