473,782 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::String ToHGlobalUni(st r);

c:\MyNetPrj\Prj 0001\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 12164
using StringToHGlobal Ansi should return a LPCTSTR.

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

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::String ToHGlobalUni(st r);

c:\MyNetPrj\Prj 0001\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::String ToHGlobalUni(st r);


try
LPCTSTR string2 =
reinterpret_cas t<LPCTSTR>(Mars hal::StringToHG lobalAnsi(ret). ToPointer());

don't forget to use FreeHGlobal when done.

hth
Ben
Nov 17 '05 #3
> try
LPCTSTR string2 =
reinterpret_cas t<LPCTSTR>(Mars hal::StringToHG lobalAnsi(ret). ToPointer());

don't forget to use FreeHGlobal when done.

for completeness:

String^ testString = gcnew String("Test");
IntPtr ptr = Marshal::String ToHGlobalAnsi(t estString);
LPCTSTR string = reinterpret_cas t<LPCTSTR>(ptr. ToPointer());

//do stuff with string

Marshal::FreeHG lobal(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 = PtrToStringChar s( 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 = PtrToStringChar s( 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::String ToHGlobalUni(st r);

c:\MyNetPrj\Prj 0001\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::Runt ime::InteropSer vices::Marshal: :StringToHGloba lAnsi(s)).ToPoi nter();
std::string str = c;
System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(Syste m::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.c om> wrote in message news:<40******* ********@nospam .com>...
ppcdev wrote:

Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::String ToHGlobalUni(st r);

c:\MyNetPrj\Prj 0001\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::Runt ime::InteropSer vices::Marshal: :StringToHGloba lAnsi(s)).ToPoi nter();
std::string str = c;
System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(Syste m::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.c om> wrote:
ppcdev wrote:

Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::String ToHGlobalUni(st r);

c:\MyNetPrj\Prj 0001\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::Run time::InteropSe rvices::Marshal ::StringToHGlob alAnsi(s)).ToPo inter();
std::string str = c;
System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(Syste m::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.c om> wrote:
ppcdev wrote:

Here's what I try :

LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::String ToHGlobalUni(st r);

c:\MyNetPrj\Prj 0001\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::Run time::InteropSe rvices::Marshal ::StringToHGlob alAnsi(s)).ToPo inter();
std::string str = c;
System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(Syste m::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(LPCTST R input);
SomeFunc(conver t(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
37889
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 DLL has some legacy code which requires char* in its method parameters. I also have a new file in the DLL which is basically an intermediary -
1
9443
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 to import a LPCTSTR ): In a native DLL: void TestFunc(LPCTSTR TestString) { TestString = TEXT("Text to transfer");
7
2171
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 NewDataBase(LPCTSTR strDbFileName)
2
1201
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 text data from System::String to another buffer memory, however, I couldn't find how to let my library access System::String text buffer directly. Please reply any answers. Thanks in advance.
10
25486
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 ); <------THIS IS MY ISSUE ....
2
12190
by: Abhishek | last post by:
how to do convert a std::string to LPTSTR (chat *) datatype regards Abhishek
14
10644
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? But I only get the first character. And when I try to do std::wstring l_s(convert(somestring)) I get really strange characters into l_s string representation, but when I check l_s individual characters they look ok. const wchar_t* convert(...
9
7993
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 MessageBox function and I believe I am adhering to it. I have also done a search for this issue, but have come up empty handed. Perhaps my search parameters were at fault ... #include <windows.h> // added to make MessageBox work (esp w/MB_<code>s)...
5
3153
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 * string::c_str();
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.