473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

LPCTSTR conversion

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

Oct 7 '06 #1
3 2588
Hi fooshm!
Currently the only way to do it is using CString cast:
CString* tmpStr = new CString(sString);
return (LPCTSTR)(*tmpStr);
See:
http://www.c-plusplus.de/forum/viewt...is-158664.html

(It's in german, but the code should be readable for you...)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Oct 7 '06 #2
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] כתב:
Hi fooshm!
Currently the only way to do it is using CString cast:
CString* tmpStr = new CString(sString);
return (LPCTSTR)(*tmpStr);

See:
http://www.c-plusplus.de/forum/viewt...is-158664.html

(It's in german, but the code should be readable for you...)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Oct 7 '06 #3
Hi fooshm,
><fo****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
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.

--
SvenC
>Efi
Jochen Kalmbach [MVP] ???:
Hi fooshm!
Currently the only way to do it is using CString cast:
CString* tmpStr = new CString(sString);
return (LPCTSTR)(*tmpStr);

See:
http://www.c-plusplus.de/forum/viewt...is-158664.html

(It's in german, but the code should be readable for you...)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Oct 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I...
1
by: ebobnar | last post by:
I need to call the function LoadImage which take a LPCTSTR argument to specify the path to the image to load. However, I need to create this path dynamically from user input by concatenating...
8
by: Fred Hebert | last post by:
I have a function that requires a LPCTSTR parameter. I have the value I want to pass to it in a TextBox->Text field. Is there any way to do it in a single assignment. e.g. DWORD...
1
by: fheinstein | last post by:
Hello, I am a newbies in .NET and i am followin a tutorial to create my first MFC program but when a compile a have this problem: See the file attach! Please help Compiling......
10
by: farseer | last post by:
How can i do this? i'd like to call the following code: .... string url = <my urld>; TCHAR* urlParams = GetParams( ); url.append( (char * ) urlParams ); GotoURL( ( LPCTSTR ) url ); ...
10
by: Lucy Ludmiller | last post by:
Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem to finda nyting (that dosen't blab on for several pages) on the net about how to do this
14
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right?...
7
by: justin001 | last post by:
In my _UNICODE & _MBCS build project, I have LPCTSTR string (i.e.it will be wchar_t*) .The following function is not giving proper result and instead it is producing junk data. I have confirmed it...
4
by: gw7rib | last post by:
I'm using a system in which TCHAR is typedef-ed to represent a character, in this case a wchar_t to hold Unicode characters, and LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.