?
Solved

char to LPCTSTR

Posted on 2004-03-24
2
Medium Priority
?
1,567 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

862 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