469,933 Members | 2,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,933 developers. It's quick & easy.

How to convert String* to LPCTSTR ??? I found threads about that but don't work...

Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str);

c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot
convert from 'System::IntPtr' to 'LPCWSTR'
I really get mad with that !!!

thanks
Nov 17 '05 #1
8 11801
using StringToHGlobalAnsi should return a LPCTSTR.

"ppcdev" <pp****@hotmail.com> wrote in message
news:2f**************************@posting.google.c om...
Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str);

c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot
convert from 'System::IntPtr' to 'LPCWSTR'
I really get mad with that !!!

thanks

Nov 17 '05 #2
ppcdev wrote:
Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str);


try
LPCTSTR string2 =
reinterpret_cast<LPCTSTR>(Marshal::StringToHGlobal Ansi(ret).ToPointer());

don't forget to use FreeHGlobal when done.

hth
Ben
Nov 17 '05 #3
> try
LPCTSTR string2 =
reinterpret_cast<LPCTSTR>(Marshal::StringToHGlobal Ansi(ret).ToPointer());

don't forget to use FreeHGlobal when done.

for completeness:

String^ testString = gcnew String("Test");
IntPtr ptr = Marshal::StringToHGlobalAnsi(testString);
LPCTSTR string = reinterpret_cast<LPCTSTR>(ptr.ToPointer());

//do stuff with string

Marshal::FreeHGlobal(ptr);
Ben
Nov 17 '05 #4
and here's a different method taken from Stan Lippman's blog
(http://weblogs.asp.net/slippman/).

this converts to a char array, which you should easily be able to
convert to LPTCSTR

note that vcclr.h is included

#include <stdlib.h>
#include <vcclr.h>
#include <string>
using namespace System;

bool To_CharStar( String^ source, char*& target )
{
int len = (( source->Length+1) * 2);
target = new char[ len ];
pin_ptr<const wchar_t> wch = PtrToStringChars( source );
return wcstombs( target, wch, len ) != -1;
}

bool To_string( String^ source, string &target )

{

int len = (( source->Length+1) * 2);

char *ch = new char[ len ];

bool result ;

{

pin_ptr<const wchar_t> wch = PtrToStringChars( source );

result = wcstombs( ch, wch, len ) != -1;

}

target = ch;

delete ch;

return result;

}
Nov 17 '05 #5
ppcdev wrote:

Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str);

c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot
convert from 'System::IntPtr' to 'LPCWSTR'

I really get mad with that !!!

thanks


// Implementation
#include <string>
std::string convert(System::String * s)
{
const char * c = (const char
*)(System::Runtime::InteropServices::Marshal::Stri ngToHGlobalAnsi(s)).ToPointer();
std::string str = c;
System::Runtime::InteropServices::Marshal::FreeHGl obal(System::IntPtr((void
*)c));
return str;
}
// Usage:

LPCTSTR tst = convert(str).c_str();
Nov 17 '05 #6
Thank you very much for your help that I've read only today !!!
I've also found an alternative using the wsprintf("%S"...) function...

see you

Julie <ju***@nospam.com> wrote in message news:<40***************@nospam.com>...
ppcdev wrote:

Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str);

c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot
convert from 'System::IntPtr' to 'LPCWSTR'

I really get mad with that !!!

thanks


// Implementation
#include <string>
std::string convert(System::String * s)
{
const char * c = (const char
*)(System::Runtime::InteropServices::Marshal::Stri ngToHGlobalAnsi(s)).ToPointer();
std::string str = c;
System::Runtime::InteropServices::Marshal::FreeHGl obal(System::IntPtr((void
*)c));
return str;
}
// Usage:

LPCTSTR tst = convert(str).c_str();

Nov 17 '05 #7
On Tue, 06 Jul 2004 12:20:29 -0700, Julie <ju***@nospam.com> wrote:
ppcdev wrote:

Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str);

c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot
convert from 'System::IntPtr' to 'LPCWSTR'

I really get mad with that !!!

thanks


// Implementation
#include <string>
std::string convert(System::String * s)
{
const char * c = (const char
*)(System::Runtime::InteropServices::Marshal::Str ingToHGlobalAnsi(s)).ToPointer();
std::string str = c;
System::Runtime::InteropServices::Marshal::FreeHGl obal(System::IntPtr((void
*)c));
return str;
}
// Usage:

LPCTSTR tst = convert(str).c_str();


That has the same problem as the original code - tst becomes a
dangling pointer straight after the ;.

Tom
Nov 17 '05 #8
tom_usenet wrote:

On Tue, 06 Jul 2004 12:20:29 -0700, Julie <ju***@nospam.com> wrote:
ppcdev wrote:

Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str);

c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot
convert from 'System::IntPtr' to 'LPCWSTR'

I really get mad with that !!!

thanks


// Implementation
#include <string>
std::string convert(System::String * s)
{
const char * c = (const char
*)(System::Runtime::InteropServices::Marshal::Str ingToHGlobalAnsi(s)).ToPointer();
std::string str = c;
System::Runtime::InteropServices::Marshal::FreeHGl obal(System::IntPtr((void
*)c));
return str;
}
// Usage:

LPCTSTR tst = convert(str).c_str();


That has the same problem as the original code - tst becomes a
dangling pointer straight after the ;.

Tom


Duh, right.

I use it in function calls, and never keep a pointer around to the underlying
c_str(), it was merely for (flawed!) illustrative purposes, thanks for pointing
it out.

Updated usage:

(immediate)

void SomeFunc(LPCTSTR input);
SomeFunc(convert(str).c_str());

(persistent)
std::string s = convert(str);
Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by johnsto | last post: by
1 post views Thread by ORC | last post: by
7 posts views Thread by ORC | last post: by
10 posts views Thread by farseer | last post: by
2 posts views Thread by Abhishek | last post: by
14 posts views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by
5 posts views Thread by T. Crane | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.