LPCTSTR conversion

F

fooshm

Hello,
I'm trying to convert a String^ to LPCTSTR.
When using :
LPCTSTR str =
static_cast<LPCTSTR>(Marshal::StringToHGlobalAnsi(gcString).ToPointer());
in order to convert how ever I'm receiving junk string.

When using a longer cast example I found on the net:
lpsConverted = static_cast<LPCTSTR>(const_cast<void*>(static_cast<const

void*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(sString))));

I'm also receiving junk.

Currently the only way to do it is using CString cast:
CString* tmpStr = new CString(sString);
return (LPCTSTR)(*tmpStr);

I'm probably missing something on my Marshal code, does anyone have a
clue ?

Thank you,
Efi
 
F

fooshm

Hi,
Thank you for your help, it works.
I guess my error was trying to static cast it to LPCTSTR and not to
char*.

I wonder why is it so important ?

Efi


Jochen Kalmbach [MVP] כתב:
 
S

SvenC

Hi fooshm,
Hi,
Thank you for your help, it works.
I guess my error was trying to static cast it to LPCTSTR and not to
char*.

I wonder why is it so important ?

It might be a wrong assupmtion about your string encoding being Ansi or
Unicode. Check you project properties if your ar using multi byte or unicode
encoding.

Looking at your original post your use:

LPCTSTR str =
static_cast<LPCTSTR>(Marshal::StringToHGlobalAnsi(gcString).ToPointer());

StringToHGlobalAnsi as the name indicates will return Ansi encoding but your
cast LPCTSTR will cast to either const char* or const wchar_t* depending on
your Unicode setting.

The link given by Jochen shows code which checks the _UNICODE define to also
create a T-version which calls the correct function based on that define.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top