473,288 Members | 1,710 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,288 software developers and data experts.

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 12090
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: johnsto | last post by:
I'm really stuck - can someone help me! I've got a basic setup consisting of two things: 1. A C# web service 2. An unmanaged C++ DLL The WS is intended to call some functions in the DLL. The...
1
by: ORC | last post by:
Hi, I have been Googling a lot trying to solve following problem, but without success, so now I hope someone here will be able to help me (I have done a lot of DLLImports but never where I had...
7
by: ORC | last post by:
Hi, How to transfer strings between a C# .NET application and a native DLL that uses Unicode like: #ifndef UNICODE #define UNICODE #endif The function header in the DLL: BOOL...
2
by: Bae,Hyun-jik | last post by:
Hi, My managed C++ library frequently takes LPCTSTR from managed exe. Due to the fact that my library doesn't modify string buffer if its parameter type is LPCTSTR, it won't be required to copy...
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 ); ...
2
by: Abhishek | last post by:
how to do convert a std::string to LPTSTR (chat *) datatype regards Abhishek
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?...
9
by: sovht | last post by:
System: Intel, Windows XP Pro, SP2 IDE: VC++ 6.0 Problem: *Very* simple program to create a MessageBox only ever displays the first character of the given string. I checked the spec for the...
5
by: T. Crane | last post by:
Help! I am trying to figure this out, and I have the impression that it should be really easy, but I'm at a loss. Here's my problem: I want to take the output from const char *...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...
1
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: 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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.