473,385 Members | 1,908 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,385 software developers and data experts.

Open a file in a managed class

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

--
http://www.mkcs.at/
The specified e-mail-address is valid and will be read.
Nov 17 '05 #1
2 835
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==]

"Michael Kremser" <mk************@yahoo.de> wrote in message
news:un**************@TK2MSFTNGP15.phx.gbl...
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

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

Nov 17 '05 #2
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

"Michael Kremser" <mk************@yahoo.de> wrote in message
news:un**************@TK2MSFTNGP15.phx.gbl...
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

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

Nov 17 '05 #3

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

Similar topics

2
by: Martin Lucas-Smith | last post by:
I am trying to use PHP's COM support to open a URL from within MS Word then save the document. I am using PHP5.0.3/Apache2/WindowsXP. phpinfo() confirms that COM support is enabled. ...
4
by: Richard | last post by:
Hi all, Is there any class in .NET 1.x that will help me set file & folder permissions for a user? Aka - I would like to do the in code equivalent of right clicking a folder in file explorer...
7
by: Lev | last post by:
Hi, I have an unmanaged pointer to a class that I want to hold in a managed class. I pass the pointer (from unmanaged code) in the constructor of the managed class, and at that point it has...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
2
by: Martin Zenkel | last post by:
Dear VS Team, using the Beta 2 of VS 2005 I've encontered the following problem. Let's assume threre are three Dll's, one unmanaged and two managed. In the unmanaged we put a simple unmanged...
1
by: bonk | last post by:
I was told that it is possible to set the /clr compiler switch on a per file basis. I find this option in the project settings for the .cpp files. However that does not affect the header files. So...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
1
by: Sarwan | last post by:
Hello everybody I have a solution in which there is an unmanaged MFC application. Then i added a new managed .Net Windows Form application. I was trying to refer an header file of unmanaged...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...
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...

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.