Solved

convert stl string to a LPTSTR

Posted on 2000-03-23
9
571 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
  • 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
 

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
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.

 

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

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

867 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

21 Experts available now in Live!

Get 1:1 Help Now