Solved

char to LPCTSTR

Posted on 2004-03-24
2
1,557 Views
Last Modified: 2012-08-13
I need to pass a char var (declared as  char   ty;)  to a function that expects LPCTSTR.
How?
0
Comment
Question by:jd9288
2 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 50 total points
ID: 10669169
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);

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10670297
You may use a string class if you don't want to use dummys.

#include <string>

   ...
   string s  =  ty;
   f(s.c_str());
   ...

Same works with CString of MFC

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

Regards, Alex
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

12 Experts available now in Live!

Get 1:1 Help Now