473,803 Members | 3,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4373
i'll reply to myself since i found the answer...
System::String* Principals

LPTSTR secPrincipals =
(LPTSTR)System: :Runtime::Inter opServices::Mar shal::StringToC oTaskMemAnsi(Pr i
ncipals).ToInt3 2();

"Mark" <po******@news. group> wrote in message
news:Oc******** ******@TK2MSFTN GP09.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 *unmanagedStrin g = PtrToStringChar s(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******** ******@TK2MSFTN GP10.phx.gbl...
i'll reply to myself since i found the answer...
System::String* Principals

LPTSTR secPrincipals =
(LPTSTR)System: :Runtime::Inter opServices::Mar shal::StringToC oTaskMemAnsi(Pr i ncipals).ToInt3 2();

"Mark" <po******@news. group> wrote in message
news:Oc******** ******@TK2MSFTN GP09.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::Inter opServices::Mar shal::StringToC oTaskMemAns
i(Pri ncipals).ToInt3 2();


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
PtrToStringChar s).
--
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
13012
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'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
1
18213
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
2
13597
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
3884
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
10259
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 'System.String'.
2
13267
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 string myTime = "13:30"; elena
1
2462
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: ' class member Private m_invDate As Date ' this code is not exact from the program, but essentially does the same thing. This is were the exception occurs:
4
11288
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 object of type 'System.String' to type 'System.String'."}
2
6545
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
9564
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
10546
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
10292
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
9121
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...
1
7603
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
6841
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
5498
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
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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

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.