473,324 Members | 2,456 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,324 software developers and data experts.

String^ to LPCTSTR

How to convert String^ (managed) to LPCTSTR (unmanaged)?
Mar 5 '07 #1
3 10450
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:95**********************************@microsof t.com...
How to convert String^ (managed) to LPCTSTR (unmanaged)?
You can check on the usage of PtrToStringChars() here:

http://blogs.msdn.com/slippman/archi...02/147090.aspx

Regards,
Will
www.ivrforbeginners.com
Mar 5 '07 #2
Hi Joachim!
How to convert String^ (managed) to LPCTSTR (unmanaged)?
See: How to convert from System::String* to Char* in Visual C++ 2005 or
in Visual C++ .NET
http://support.microsoft.com/kb/311259/en-us

The "easysed to use" function is

struct StringConvA
{
char *szAnsi;
StringConvA(System::String* s)
:
szAnsi(static_cast<char*>(System::Runtime::Interop Services::Marshal::StringÂ*ToHGlobalAnsi(s).ToPoin ter()))

{}
~StringConvA()
{
System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}

};
struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String* s)
:
szUnicode(static_cast<wchar_t*>(System::Runtime::I nteropServices::Marshal::Â*StringToHGlobalUni(s).T oPointer()))

{}
~StringConvW()
{

System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}

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

int _tmain()
{
String *s = S"abc";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
_tprintf(_T("%s"), (LPCTSTR) StringConvT(s));
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Mar 5 '07 #3
Hi Joachim!
How to convert String^ (managed) to LPCTSTR (unmanaged)?
Upps the last post was for managed C++... so here for C++/CLI:

#include <windows.h>
#include <tchar.h>
#include <string>
using namespace System;
struct StringConvA
{
char *szAnsi;
StringConvA(System::String ^s)
:
szAnsi(static_cast<char*>(System::Runtime::Interop Services::Marshal::StringÂ*ToHGlobalAnsi(s).ToPoin ter()))

{}
~StringConvA()
{
System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};
struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String^ s)
:
szUnicode(static_cast<wchar_t*>(System::Runtime::I nteropServices::Marshal::Â*StringToHGlobalUni(s).T oPointer()))

{}
~StringConvW()
{
System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};

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

int _tmain()
{
String ^s = "abc";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
_tprintf(_T("%s"), (LPCTSTR) StringConvT(s));
}
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Mar 5 '07 #4

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...
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...
8
by: ppcdev | last post by:
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...
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 ); ...
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?...
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...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
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: 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: 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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.