Solved

char to LPCTSTR

Posted on 2004-03-24
2
1,555 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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

14 Experts available now in Live!

Get 1:1 Help Now