Solved

convert stl string to a LPTSTR

Posted on 2000-03-23
9
570 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

12 Experts available now in Live!

Get 1:1 Help Now