Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

Open a file in a managed class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Kremser - 03 Mar 2005 21:14 GMT
Hi NG!

In a managed C++ class, I have a function that expects a System::String
argument that specifies a file name. Now I want to use that filename for
CreateFile, but I don't know how to convert a System::String to a
LPCTSTR. (Yes, I know that I could use System::IO::StreamReader - I want
to do it that way anyway, just for testing purpose).

Can anyone give me a hint how to do this?

Thanks in advance!

Best regards,

Michael

Signature

http://www.mkcs.at/
The specified e-mail-address is valid and will be read.

Peteroid - 03 Mar 2005 22:05 GMT
I asked a similar question a few days ago here (but it is far down, so you
probably didnt see it). I asked how to convert a String* to a char[]. This
is how I ended up doing it:

char  char_array[32+1] ;
String* string_thing = new String("Hello") ;

for ( int c=0 ; c < length ; c++)
{
  char_array[c] = char(string_thing->chars[c]) ;
}

char_array[length] = '\0' ;

After this code is executed:  char_array contains null-terminated "Hello".

So, in your case the 'string_thing' is your filename argument and char_array
is similar to char* (it is easy to change to using char* instead, as
char_array is almost a char*....hehe).

Hope this helps... :)

[==Peteroid==]

> Hi NG!
>
[quoted text clipped - 11 lines]
>
> Michael
Steve McLellan - 04 Mar 2005 17:11 GMT
I think this should do it.. (first one is String * to stl string, second is
the opposite. You can extract the raw char pointer from an STL string using
c_str().

std::string STLStringFromDotNetString( System::String *str)
{
   //get a pointer to an array of ANSI chars
   char *chars = (char*) Marshal::StringToHGlobalAnsi(str).ToPointer();
   //assign the array to an STL string
   std::string stl = chars;
   //free the memory used by the array
   //since the array is not managed, it will not be claimed by the garbage
collector
   Marshal::FreeHGlobal(chars);
   return stl;
}

System::String * DotNetStringFromSTLString( const std::string &str)
{
   return Marshal::PtrToStringAnsi((int*) str.c_str());
}

Steve

> Hi NG!
>
[quoted text clipped - 11 lines]
>
> Michael

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.