Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

char to LPCTSTR

Posted on 2004-03-24
2
Medium Priority
?
1,566 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

647 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