Convert a char array to a managed system string

J

Jack Klein

JKop said:
Trep posted:

Hi there!

I've been having a lot of difficult trying to figure out a way to
convert a terminated char array to a system::string for use in Visual
C++ .NET 2003.

This is necessary because I have some legcay C code that needs to
process a string taken from a textbox, then I need to re-display the
string as the textbox->Text. I easily found how to convert from
system::string to char[] but I can't figure out how to go the other
way!!

Can someone point me in the right direction? I'm pretty new to VC++
managed programming and the whole GUI thing! :S


I haven't a clue about .NET, nor have I had a peek at the definition of
"system::string", but... were I to guess:

system::string ConvertStringToSysString(char const blah[])
{
return system::string(blah);
}


Close enough for C++/CLI:


inline System::String ^ConvertStringToSysString(cli::array<wchar_t> ^blah)
{
return gcnew System::String(blah);
}

This is not C++. There is no 'gcnew' in C++. Stop posting this
off-topic crap here. Go find a 'CLI' group if you like it so much.
 
I

Ioannis Vranos

Rob said:
Nope, CLI will *not* be available *everywhere* C++ is so it will not
be part of C++ and so will remain off topic.



Here are some mainstream CLI compliant engines:



..NET framework (Windows): http://msdn.microsoft.com/netframework

Mono (GNU/Linux, Unix, Mac OS X and Windows): http://www.mono-project.com

DotGNU (GNU/Linux, Mac OS X, Windows): http://www.gnu.org/projects/dotgnu


Check the OSes mentioned.



You can make a similar argument about extension's to g++ which are
currently far more portable (*) then CLI extension's, and are likely to
remain so. You can extend this argument to a large number of GNU
libraries (as if this counter needs to be made any more ridiculous :).

*) This is a *massive* under statement as they are actually available.

Or Qt ...

Or Borlands VCL ... (They did get it working on two platform's).

Only thing's that are part of the The C++ Standard are on topic and
*nobody* (except perhaps you) is considering incorporating CLI/C++
into The C++ Standard (future or current).


No, C++/CLI is a separate standard of extensions to ISO C++ for taking
advantage of a CLI machine where one is available.

CLI VMs are already becoming widespread.


If you want a (non-Microsoft) newsgroup to discuss CLI/C++ I suggest
you investigate setting one up.

Until such time please redirect to:


They're currently the only vendor with a CLI/C++ compiler and
even that hasen't been released yet.


C++/CLI code has binary compatibility even if you compile it under Windows.


C++/CLI is not a standard yet, so how could there be a C++ compiler with
support for it?
 
I

Ioannis Vranos

Trep said:
Ahhh I understand completely now! I started tunning into lots of problems
with my first "solution" anyway, because of the obvious differences
between wide chars and chars.

The main confusion in my code was that I was using a buffer of type
unsigned char, and since there is no String constructor for unsigned
chars, it was erroring and telling me it needed a wchar.

In any case, changing my buffer to use chars (there was no reason that I
required unsigned chars) was the easiest solution, and it allowed me to
use the constructor that you instructed.

Thanks again for all of your help!

And just for reference, I used the following:

To convert from a System::String to a char[]:
char * charArray = (char *)(void
*)Marshal::StringToHGlobalAnsi(System::String __gc *);



Marshal class is for:

"Provides a collection of methods for allocating unmanaged memory,
copying unmanaged memory blocks, and converting managed to unmanaged
types, as well as other miscellaneous methods used when interacting with
unmanaged code."


You do not want to convert from managed to unmanaged.


Just you the member function String::ToCharArray:



#using <mscorlib.dll>


int main()
{
using namespace System;

String *s= __gc new String("Test");


wchar_t p __gc[] = s->ToCharArray();
}



It returns a managed array of wchar_t.




And to convert from a char[] to a System::String I used:
String *newString = __gc new String(char *);


Correct.
 
R

Rob Williscroft

Ioannis Vranos wrote in in
comp.lang.c++:
Here are some mainstream CLI compliant engines:

Doesn't have anything to do with CLI/C++ portablity, The C++ Standard
or the topic of this newsgroup.

No, What ?
C++/CLI is a separate standard of extensions to ISO C++ for taking
advantage of a CLI machine where one is available.

So it *isn't* part of The C++ Standard or the topicality of
this newsgroup.
CLI VMs are already becoming widespread.

Again that has nothing to do with portablity, The C++ Standard or
the topic of this newsgroup.
C++/CLI code has binary compatibility even if you compile it under
Windows.

Doesn't make it topical here. I can write all my code in MSWORD and
compile it anywhere, so is MSWORD on topic now ?
C++/CLI is not a standard yet, so how could there be a C++ compiler
with support for it?

More importantly, how can you possibly think its on topic here ?

Rob.
 
I

Ioannis Vranos

Rob said:
Doesn't make it topical here. I can write all my code in MSWORD and
compile it anywhere, so is MSWORD on topic now ?


Well, C++/CLI is a standard. Also there is communication between ISO C++
and C++/CLI committees.


Also check a list of C++/CLI participants:


C++/CLI Participants and Timeline

Participants:
• Convener: Tom Plum
• Project Editor: Rex Jaeschke
• Subject Matter Experts: Bjarne Stroustrup, Herb Sutter
• Participants: Dinkumware, EDG, IBM, Microsoft, Plum Hall…
• Independent conformance test suite: Plum Hall


Ecma + ISO process, estimated timeline:
• Oct 1, 2003: Ecma TC39 plenary. Kicked off TG5.
• Nov 21, 2003: Submitted base document to Ecma.
• Dec 2003 – Sep 2004: TG5 meetings (7).
• Dec 2004: Vote on whether to adopt as Ecma standard.
• Q1 2005: If successful, submit for ISO fast-track process.
• Q1 2006: If ready, vote on whether to adopt ISO standard.
 
K

Karl Heinz Buchegger

Ioannis said:
Well, C++/CLI is a standard.

Oh, come on.
Ada has a standard, Prolog has a standard, Fortran has a standard.
xxx has a standard.

According to you all of them would be topical now.
Also there is communication between ISO C++
and C++/CLI committees.

And?
As for now CLI is not in the C++ standard. End of story.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top