473,769 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conversion from System::String^ to LPCTSTR

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(som estring)) 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(
System::String^ s)
{
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_twch = PtrToStringChar s(s);

return wch;
}
Mar 13 '07 #1
14 10641
Hi Joachim!
I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to
const wchar_t*.
No. LPCTSTR ist either const char* or const wchar_t* depending on
UNICODE macro.

My prefered way for cenversion is:

#include <windows.h>
#include <tchar.h>
using namespace System;
struct StringConvA
{
char *szAnsi;
StringConvA(Sys tem::String ^s)
:
szAnsi(static_c ast<char*>(Syst em::Runtime::In teropServices:: Marshal::String ToHGlobalAnsi(s ).ToPointer()))

{}
~StringConvA()
{

System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(IntPt r(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};

struct StringConvW
{
wchar_t *szUnicode;
StringConvW(Sys tem::String^ s)
:
szUnicode(stati c_cast<wchar_t* >(System::Runti me::InteropServ ices::Marshal:: StringToHGlobal Uni(s).ToPointe r()))

{}
~StringConvW()
{

System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(IntPt r(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};

#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif

And then use it with:
#include <string>
int _tmain()
{
String ^s = "abc";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
}

or

#include <stdio.h>
int _tmain()
{
String ^s = "abc";
printf("%s", (LPCSTR) StringConvA(s)) ;
wprintf(L"%s", (LPCWSTR) StringConvW(s)) ;
_tprintf(_T("%s "), (LPCTSTR) StringConvT(s)) ;
return 0;
}
Greetings
Jochen
Mar 13 '07 #2
Thanks Jochen,

I still only get the first character...

Regards,
Joachim

"Jochen Kalmbach [MVP]" wrote:
Hi Joachim!
I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to
const wchar_t*.

No. LPCTSTR ist either const char* or const wchar_t* depending on
UNICODE macro.

My prefered way for cenversion is:

#include <windows.h>
#include <tchar.h>
using namespace System;
struct StringConvA
{
char *szAnsi;
StringConvA(Sys tem::String ^s)
:
szAnsi(static_c ast<char*>(Syst em::Runtime::In teropServices:: Marshal::String ToHGlobalAnsi(s ).ToPointer()))

{}
~StringConvA()
{

System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(IntPt r(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};

struct StringConvW
{
wchar_t *szUnicode;
StringConvW(Sys tem::String^ s)
:
szUnicode(stati c_cast<wchar_t* >(System::Runti me::InteropServ ices::Marshal:: StringToHGlobal Uni(s).ToPointe r()))

{}
~StringConvW()
{

System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(IntPt r(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};

#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif

And then use it with:
#include <string>
int _tmain()
{
String ^s = "abc";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
}

or

#include <stdio.h>
int _tmain()
{
String ^s = "abc";
printf("%s", (LPCSTR) StringConvA(s)) ;
wprintf(L"%s", (LPCWSTR) StringConvW(s)) ;
_tprintf(_T("%s "), (LPCTSTR) StringConvT(s)) ;
return 0;
}
Greetings
Jochen
Mar 13 '07 #3
const wchar_t* convert(
System::String^ s)
{
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_twch = PtrToStringChar s(s);

return wch;
}
Complete misuse of pin_ptr here. You must never use a pin_ptr as a return
value.

OTOH, this will work, because the wstring constructor is called while the
string is still pinned:

std::wstring convert(System: :String^ const s)
{
return std::wstring(pi n_ptr<const wchar_t>(PtrToS tringChars(s))) ;
}
Mar 13 '07 #4
Thank you Ben,

However, that still doesn't solve my problem (and by the way I get
compilation error with it:

error C3834: illegal explicit cast to a pinning pointer; use a pinned local
variable instead f:\Development\ Source\Test\MCP P\MCPP\MCPPImpl .cpp 54)

Regards,
Joachim
"Ben Voigt" wrote:
const wchar_t* convert(
System::String^ s)
{
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_twch = PtrToStringChar s(s);

return wch;
}

Complete misuse of pin_ptr here. You must never use a pin_ptr as a return
value.

OTOH, this will work, because the wstring constructor is called while the
string is still pinned:

std::wstring convert(System: :String^ const s)
{
return std::wstring(pi n_ptr<const wchar_t>(PtrToS tringChars(s))) ;
}
Mar 13 '07 #5

"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:12******** *************** ***********@mic rosoft.com...
Thank you Ben,

However, that still doesn't solve my problem (and by the way I get
compilation error with it:

error C3834: illegal explicit cast to a pinning pointer; use a pinned
local
variable instead f:\Development\ Source\Test\MCP P\MCPP\MCPPImpl .cpp 54)
ok, yeah, pin_ptr needs an explicit local, so split the one-liner:

std::wstring convert(System: :String^ const s)
{
pin_ptr<const wchar_twch = PtrToStringChar s(s);
return std::wstring(wc h);
}
Regards,
Joachim
"Ben Voigt" wrote:
const wchar_t* convert(
System::String^ s)
{
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_twch = PtrToStringChar s(s);

return wch;
}

Complete misuse of pin_ptr here. You must never use a pin_ptr as a
return
value.

OTOH, this will work, because the wstring constructor is called while the
string is still pinned:

std::wstring convert(System: :String^ const s)
{
return std::wstring(pi n_ptr<const wchar_t>(PtrToS tringChars(s))) ;
}

Mar 13 '07 #6
Ben,

Yes that did compile, but it didn't solve the problem

Regards,
Joachim

"Ben Voigt" wrote:
>
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:12******** *************** ***********@mic rosoft.com...
Thank you Ben,

However, that still doesn't solve my problem (and by the way I get
compilation error with it:

error C3834: illegal explicit cast to a pinning pointer; use a pinned
local
variable instead f:\Development\ Source\Test\MCP P\MCPP\MCPPImpl .cpp 54)

ok, yeah, pin_ptr needs an explicit local, so split the one-liner:

std::wstring convert(System: :String^ const s)
{
pin_ptr<const wchar_twch = PtrToStringChar s(s);
return std::wstring(wc h);
}
Regards,
Joachim
"Ben Voigt" wrote:
const wchar_t* convert(
System::String^ s)
{
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_twch = PtrToStringChar s(s);

return wch;
}

Complete misuse of pin_ptr here. You must never use a pin_ptr as a
return
value.

OTOH, this will work, because the wstring constructor is called while the
string is still pinned:

std::wstring convert(System: :String^ const s)
{
return std::wstring(pi n_ptr<const wchar_t>(PtrToS tringChars(s))) ;
}


Mar 13 '07 #7

"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:19******** *************** ***********@mic rosoft.com...
Ben,

Yes that did compile, but it didn't solve the problem

Regards,
Joachim
How about:

std::basic_stri ng<TCHARconvert (System::String ^ const s)
{
pin_ptr<const wchar_twch = PtrToStringChar s(s);
#if _UNICODE
return std::wstring(wc h);
#else
int needed = wcstombs(nullpt r, wch, 0) + 1;
char* pch = alloca(needed);
wcstombs(pch, wch, needed);
// optional, unpin early with wch = nullptr;
return std::string(pch );
#endif
}
>
"Ben Voigt" wrote:
>>
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:12******* *************** ************@mi crosoft.com...
Thank you Ben,

However, that still doesn't solve my problem (and by the way I get
compilation error with it:

error C3834: illegal explicit cast to a pinning pointer; use a pinned
local
variable instead f:\Development\ Source\Test\MCP P\MCPP\MCPPImpl .cpp 54)

ok, yeah, pin_ptr needs an explicit local, so split the one-liner:

std::wstring convert(System: :String^ const s)
{
pin_ptr<const wchar_twch = PtrToStringChar s(s);
return std::wstring(wc h);
}
Regards,
Joachim
"Ben Voigt" wrote:

const wchar_t* convert(
System::String^ s)
{
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_twch = PtrToStringChar s(s);

return wch;
}

Complete misuse of pin_ptr here. You must never use a pin_ptr as a
return
value.

OTOH, this will work, because the wstring constructor is called while
the
string is still pinned:

std::wstring convert(System: :String^ const s)
{
return std::wstring(pi n_ptr<const wchar_t>(PtrToS tringChars(s))) ;
}



Mar 13 '07 #8
No, it is unicode.

"Ben Voigt" wrote:
>
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:19******** *************** ***********@mic rosoft.com...
Ben,

Yes that did compile, but it didn't solve the problem

Regards,
Joachim

How about:

std::basic_stri ng<TCHARconvert (System::String ^ const s)
{
pin_ptr<const wchar_twch = PtrToStringChar s(s);
#if _UNICODE
return std::wstring(wc h);
#else
int needed = wcstombs(nullpt r, wch, 0) + 1;
char* pch = alloca(needed);
wcstombs(pch, wch, needed);
// optional, unpin early with wch = nullptr;
return std::string(pch );
#endif
}

"Ben Voigt" wrote:
>
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:12******** *************** ***********@mic rosoft.com...
Thank you Ben,

However, that still doesn't solve my problem (and by the way I get
compilation error with it:

error C3834: illegal explicit cast to a pinning pointer; use a pinned
local
variable instead f:\Development\ Source\Test\MCP P\MCPP\MCPPImpl .cpp 54)


ok, yeah, pin_ptr needs an explicit local, so split the one-liner:

std::wstring convert(System: :String^ const s)
{
pin_ptr<const wchar_twch = PtrToStringChar s(s);
return std::wstring(wc h);
}

Regards,
Joachim
"Ben Voigt" wrote:

const wchar_t* convert(
System::String^ s)
{
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_twch = PtrToStringChar s(s);

return wch;
}

Complete misuse of pin_ptr here. You must never use a pin_ptr as a
return
value.

OTOH, this will work, because the wstring constructor is called while
the
string is still pinned:

std::wstring convert(System: :String^ const s)
{
return std::wstring(pi n_ptr<const wchar_t>(PtrToS tringChars(s))) ;
}



Mar 13 '07 #9
The (3rd party) function which I am passing the LPCTSTR on to takes and
LPCTSTR as argument and is working in a native C++/ATL/COM environment. In
that environment it is passed as a TCHAR*. But in this Managed C++
environment, even if I directly before the call to the function specifies
TCHAR* l_s(_T("test.mp g")) it only comes out as the filename "t".

"Joachim" wrote:
No, it is unicode.

"Ben Voigt" wrote:

"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:19******** *************** ***********@mic rosoft.com...
Ben,
>
Yes that did compile, but it didn't solve the problem
>
Regards,
Joachim
How about:

std::basic_stri ng<TCHARconvert (System::String ^ const s)
{
pin_ptr<const wchar_twch = PtrToStringChar s(s);
#if _UNICODE
return std::wstring(wc h);
#else
int needed = wcstombs(nullpt r, wch, 0) + 1;
char* pch = alloca(needed);
wcstombs(pch, wch, needed);
// optional, unpin early with wch = nullptr;
return std::string(pch );
#endif
}
>
>
>
"Ben Voigt" wrote:
>
>>
>"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
>news:12******* *************** ************@mi crosoft.com...
Thank you Ben,
>
However, that still doesn't solve my problem (and by the way I get
compilation error with it:
>
error C3834: illegal explicit cast to a pinning pointer; use a pinned
local
variable instead f:\Development\ Source\Test\MCP P\MCPP\MCPPImpl .cpp 54)
>
>>
>ok, yeah, pin_ptr needs an explicit local, so split the one-liner:
>>
>std::wstring convert(System: :String^ const s)
>{
> pin_ptr<const wchar_twch = PtrToStringChar s(s);
> return std::wstring(wc h);
>}
>>
Regards,
Joachim
>
>
"Ben Voigt" wrote:
>
const wchar_t* convert(
System::String^ s)
{
// Pin memory so GC can't move it while native function is called
pin_ptr<const wchar_twch = PtrToStringChar s(s);
>
return wch;
}
>>
>Complete misuse of pin_ptr here. You must never use a pin_ptr as a
>return
>value.
>>
>OTOH, this will work, because the wstring constructor is called while
>the
>string is still pinned:
>>
>std::wstring convert(System: :String^ const s)
>{
> return std::wstring(pi n_ptr<const wchar_t>(PtrToS tringChars(s))) ;
>}
>>
>>
>>
>>
>>
>>
Mar 13 '07 #10

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

Similar topics

27
51732
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found how to convert from system::string to char but I can't figure out how to go the other way!!
15
10833
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
8
2311
by: Ioannis Vranos | last post by:
In .NET (and C++/CLI) there is an overloaded String == operator for handles. That is when we do comparison of two String handles the contents of the Strings are compared instead of their addresses. However how can we do a handle comparison when we want to determine if we have to do with the same object? In the style
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.
1
18202
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it is serialized correctly, and the server returns a response (I've captured the response and it's correct!) but when the .NET deserialize this response, it throws the exception "System.InvalidOperationException: There is an error in XML
24
17490
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET managed C++. It seems to work fine, but everyone knows that programs with errors can still appear to "work fine" :) I am working with VS .NET 2003; I am unable to upgrade to 2005 at this time, so I cannot use the newer syntax or features. ...
2
5043
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it because in further editions of .net this will be deprecated or so on.. . so, please, anybody can tellme how to convert from System::String::^ to char* and viceversa?
2
13593
by: John Smith | last post by:
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol { public XmlDocument validate(string url, XmlDocument xmlDocument) { this.Url = url;
4
3883
by: John Smith | last post by:
How can I allow someone to cast my C# class into System.String? Is it possible in C#? In C++ I used "operator" keyword to mark C++ member function.
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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...
1
9990
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
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
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...
1
3956
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
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
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.