Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

convert stl string to a LPTSTR

Posted on 2000-03-23
9
Medium Priority
?
584 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

636 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