Skip to main content
gamedev.net
🔒 Locked

String to LPCTSTR

Started by kobingo 16 replies 23.9k views
Mathematix
Mathematix
LongPointertoCharacTerSTRing. Not sure what you're trying to do, but either use the old style cast

somethinglikethis = (LPCTSTR) somethinglikethat;

or the dynamic cast

somethinglikethis = dynamic_cast < LPCTSTR > somethinglikethat;

Leave out the spaces between the chevrons.

Beginners and experts will find answers here.


[edited by - mathematix on August 21, 2003 8:47:31 AM]
Lektrix
Lektrix
quote:
Original post by Mathematix
or the dynamic cast

somethinglikethis = dynamic_cast < LPCTSTR > somethinglikethat;


ROFL.          

No, you don't need any cast:

const char* str1 = "blergh";
char* str2 = "blah";
LPCTSTR str3 = str1;
LPCTSTR str4 = str2;


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on August 21, 2003 8:59:21 AM]
Wildfire
Wildfire
What kind of string are you talking about?
std::string or (c-style) char*-string?

In case of std::string call the .c_str() member-function

A little more info:

// WinNT.h


typedef CONST CHAR *LPCSTR, *PCSTR;
typedef CONST WCHAR *LPCWSTR, *PCWSTR;

#ifdef UNICODE
typedef LPCWSTR LPCTSTR;
#else
typedef LPCSTR LPCTSTR;
#endif


[edited by - Wildfire on August 21, 2003 9:01:08 AM]
How do I set my laser printer n stun? o
Fruny
Fruny
quote:
Original post by Mathematix
or the dynamic cast

somethinglikethis = dynamic_cast < LPCTSTR > somethinglikethat;



dynamic_cast is only for base-to-derived casts of polymorphic classes and is completely inappropriate here.

kobingo - I assume that, by string, you mean a C++ std::string object. Two possibilities :

  • You are only going to read the string at a single point - which appears to be the case with D3DXCreateTextureFromFile. If so, you can use the std::string::c_str() member function.

    SomeFunction( MyString.c_str() );

  • You need to create a C string out of the string object in a persistent manner - by copying it out in a char buffer. If so, you can use the std::string::copy() member function.

    char* buffer = new char[MyString.length()+1];
    MyString.copy( buffer, std::string::npos );
    buffer[MyString.length()] = 0;




[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
mputters
mputters

typedef basic_string<TCHAR> tstring;

void function( LPCTSTR );

..

tstring bleh;
function( tstring.c_str() );

Mathematix
Mathematix
Okay, guys.

I put up some possible solutions. No code samples provided, so I put down some possibilities - OKAY?!

Bloody hell!!! >

Beginners and experts will find answers here.
Colin Jeanne
Colin Jeanne
quote:
Original post by Mathematix
LongPointertoCharacTerSTRing



Actually that''s

Long Pointer to Const T STRing



Qui fut tout, et qui ne fut rien
Invader''s Realm
JonW
JonW
quote:
Original post by Invader X
Actually that's

Long Pointer to Const T STRing



What's a "T String"? Any relation to the "G String"? Har har.

[edited by - JonWoyame on August 21, 2003 5:26:52 PM]
noVum
noVum
Terminated
Mathematix
Mathematix
You guys are really taking the p*** now, but I ain''t trippin''.

Beginners and experts will find answers here.
glassJAw
glassJAw
quote:
Original post by Anonymous Poster
just to add fuel to the fire the T stands for TCHAR, which is associated with MS''s tchar.h #defines that help create "portable" code that can be used to target both ANSI and UNICODE character set based versions of Windows without recoding.

Aye. For "null-terminated" it''s a "Z"
rtw1701
rtw1701
Thanks I was having same trouble with loadimage.
LessBread
LessBread
quote:
Original post by Anonymous Poster
so, me being a C++ lib no-nothing, will MS''s implementation of std::string have c_str() return a pointer to a wide (2 byte) character buffer if a UNICODE char set is targeted for the build? according to the MSDN docs it will return a pointer to a nonmodifiable C string, which doesn''t tell me much.


That''s the decisive question.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.