Click here to Skip to main content
15,886,362 members
Articles / Mobile Apps
Article

Convertion from managed String* data type to LPCTSTR

Rate me:
Please Sign up or sign in to vote.
1.79/5 (8 votes)
6 Feb 20051 min read 69.5K   12   6
a class to convert from VC++.NET String* data type to leagcy LPCTSTR

Introduction

As a VC++ programmer. and if you are using VC.NET you may try to use your old leagcy code under the .NET framwork.

When I tried to use some of my old C++ code, I encountered the problem of data types conversion in .NET framework. I thought that Microsoft will handle the data types issue in .NET in a better manner.

Any way, one of the problems I had, is to use a function that receives a parameter of LPCTSTR data type. In .NET you can use the managed String* data type but the compiler will complain as it can't convert managed String* data type to LPCTSTR data type.

The problem has been handled through the StringToHGlobalAnsi method of Marshal class provided in the .NET framework, but it's not that easy.You have to make some static casts.

I thought there might some other people who encounter the same problem, so I decided to make a small class that has just two methods to convert from managed String* data type to unmanaged LPCTSTR data type. any way I have put the code in a class, but you can use it in your manner.

The two methods are:

  1. LPCTSTR AnsiStrings::ConvertStringToLPCTSTR(String* sString)<BR><BR>you only need to pass your String* variable to it and receives a LPCTSTR variable.
  2. void AnsiStrings::ReleaseLPCTSTR(LPCTSTR lpsString)<BR><BR>this method will release the memory holded for our leagacy LPCTSTR varaible.

It's a small class, but I hope the begginers in VC.NET can make best use of it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalmany casts... Pin
HumanOsc6-Oct-04 3:48
HumanOsc6-Oct-04 3:48 
I think a smarter solution... when u use them like this...

LPCTSTR str = static_cast<LPCTSTR>(Marshal::StringToHGlobalAnsi(gcString).ToPointer());

// using str

Marshal::FreeHGlobal(const_cast<LPTSTR>(str));

best regards...Big Grin | :-D

QuestionWhy not use CString? Pin
George L. Jackson5-Oct-04 11:44
George L. Jackson5-Oct-04 11:44 
QuestionWhy a class? Pin
Nemanja Trifunovic5-Oct-04 2:09
Nemanja Trifunovic5-Oct-04 2:09 
AnswerRe: Why a class? Pin
toxcct5-Oct-04 2:30
toxcct5-Oct-04 2:30 
AnswerRe: Why a class? Pin
Philippe Mori5-Oct-04 3:55
Philippe Mori5-Oct-04 3:55 
GeneralRe: Why a class? Pin
Nemanja Trifunovic5-Oct-04 4:48
Nemanja Trifunovic5-Oct-04 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.