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

cast System::String* to LPTSTR

Help please... i am VERY new to the managed C++ and i need to take a
System::String* and cast it to an LPTSTR how might i do this? I have
searched the web and groups but cannot find any examples.

-thanks
Nov 16 '05 #1
3 4348
i'll reply to myself since i found the answer...
System::String* Principals

LPTSTR secPrincipals =
(LPTSTR)System::Runtime::InteropServices::Marshal: :StringToCoTaskMemAnsi(Pri
ncipals).ToInt32();

"Mark" <po******@news.group> wrote in message
news:Oc**************@TK2MSFTNGP09.phx.gbl...
Help please... i am VERY new to the managed C++ and i need to take a
System::String* and cast it to an LPTSTR how might i do this? I have
searched the web and groups but cannot find any examples.

-thanks

Nov 16 '05 #2
Also, if you just need a temporary pointer to it:

#include <vcclr.h>
System::String *managedString = new String("Hello");
const wchar_t __pin *unmanagedString = PtrToStringChars(managedString);

Note that since the returned string is const, you can't (and definitely
shouldn't) manipulate it. The __pin locks down the managed memory so the
memory manager doesn't move it around on you, and once the pointer goes out
of scope it's gone. Don't save a reference or anything like that. If you
need to save it or manipulate it, the method you came up with below is the
way to go. This suggestion is just more efficient if you only need to
"glance" at the string.

Brian

"Mark" <po******@news.group> wrote in message
news:uF**************@TK2MSFTNGP10.phx.gbl...
i'll reply to myself since i found the answer...
System::String* Principals

LPTSTR secPrincipals =
(LPTSTR)System::Runtime::InteropServices::Marshal: :StringToCoTaskMemAnsi(Pri ncipals).ToInt32();

"Mark" <po******@news.group> wrote in message
news:Oc**************@TK2MSFTNGP09.phx.gbl...
Help please... i am VERY new to the managed C++ and i need to take a
System::String* and cast it to an LPTSTR how might i do this? I have
searched the web and groups but cannot find any examples.

-thanks


Nov 16 '05 #3
Mark wrote:
i'll reply to myself since i found the answer...
System::String* Principals

LPTSTR secPrincipals =
(LPTSTR)System::Runtime::InteropServices::Marshal: :StringToCoTaskMemAns
i(Pri ncipals).ToInt32();


This will only work, if you have an ANSI build. For unicode-builds you have
to use the corresponding Uni-Version of the method (or for use
PtrToStringChars).
--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/useritems/leakfinder.asp
Nov 16 '05 #4

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

Similar topics

3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
1
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...
2
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) {...
4
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.
3
by: keithb | last post by:
What could be causing this? this code: String Com = ""; if (Com != (String)rw.ItemArray) fails at runtime with the error message: Unable to cast object of type 'System.Int32' to type...
2
by: elena | last post by:
Hi, All Please, i need help with cast string to DateTime: DateTime dt = new DateTime(2006,09,17); it works i can update Field in Access DB Next field is Short Time data type, how i can cast...
1
by: =?Utf-8?B?U2lzbmF6?= | last post by:
I'm having a very strange problem I can't seem to figure out and am hoping maybe somebody has seen it before. I get an exception "Cast string to date is invalid" with this chunk of code: '...
4
by: archu007 | last post by:
hi i'm using the following statement in my application string strTArray = new string; strTArray = (string)(Session); when i run the application its giving the below error {"Unable to cast...
2
by: Bigi | last post by:
Hi, Please help, this has been driving me nuts for nearly 2 days now. This vb6 code works: Public oEng As New ebizEngine Public oMsg As ebizMessage Function EbizGetFromQueue() As String
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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,...

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.