Solved

convert stl string to a LPTSTR

Posted on 2000-03-23
9
575 Views
Last Modified: 2011-10-03
I need to pass a stl string to a function that takes a LPTSTR.  I've tried:

string junk1 = "howdy";

TextOut(dc, RectLeft, RectTop, (LPTSTR) junk1.c_str, 5);

but it doesn't compile.
0
Comment
Question by:abulka
[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
  • 6
  • 3
9 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 2651599
junk1.c_str()

Note that it won't compile for Unicode version.
0
 

Author Comment

by:abulka
ID: 2651613
Actually how come

  string text1 = "hello";
  SIZE res;
  GetTextExtentPoint32(rDC.m_hDC, text1.c_str(), (int) text1.length(), &res);

works, but

  TextOut(rDC.m_hDC, RectLeft, RectTop,  text1.c_str, (int) text1.length());

does not?  The error is
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(644) : error C2664: 'TextOutA' : cannot convert parameter 4 from 'const char *(void) const' to 'const char *'
        There is no context in which this conversion is possible
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(653) : error C2664: 'TextOutA' : cannot convert parameter 4 from 'const char *(void) const' to 'const char *'
        There is no context in which this conversion is possible

Note the definitions in the API

BOOL GetTextExtentPoint32(

    HDC hdc,      // handle of device context
    LPCTSTR lpString,      // address of text string
    int cbString,      // number of characters in string
    LPSIZE lpSize       // address of structure for string size  
   );      
 
and

BOOL TextOut(

    HDC hdc,      // handle of device context
    int nXStart,      // x-coordinate of starting position  
    int nYStart,      // y-coordinate of starting position  
    LPCTSTR lpString,      // address of string
    int cbString       // number of characters in string
   );      
 

the relevant parameter is the same - a LPCTSTR.


0
 

Author Comment

by:abulka
ID: 2651619
Adjusted points from 5 to 10
0
Industry Leaders: 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!

 

Author Comment

by:abulka
ID: 2651620
How do I know if unicode affects me?  Is it a compiler switch?  What stops working in unicode mode?
0
 

Author Comment

by:abulka
ID: 2651625
See my second & third comments for better context for the required answer.
0
 
LVL 23

Accepted Solution

by:
chensu earned 20 total points
ID: 2651725
You use c_str() for GetTextExtentPoint32 while you use c_str for TextOut.
0
 

Author Comment

by:abulka
ID: 2651726
Adjusted points from 10 to 20
0
 
LVL 23

Expert Comment

by:chensu
ID: 2651728
Clear enough?

You miss out ().
0
 

Author Comment

by:abulka
ID: 2651737
Yeah - they should both be .c_str()
Thanks.
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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

724 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