473,761 Members | 10,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert a char array to a managed system string

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

Jul 22 '05
27 51728
On Wed, 20 Oct 2004 17:47:39 +0300, Ioannis Vranos
<iv*@guesswh.at .grad.com> wrote in comp.lang.c++:
Trep wrote:
But... I was under the impression that VC++ .NET used managed strings,
which evolved from CStrings???

.NET uses the System::String type of the CLI standard:

http://www.ecma-international.org/pu...s/Ecma-335.htm


And it is completely off-topic here, dipstick. Do not provide
off-topic answers to off-topic questions.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #21
On Wed, 20 Oct 2004 18:17:18 +0300, Ioannis Vranos
<iv*@guesswh.at .grad.com> wrote in comp.lang.c++:
JKop wrote:
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::strin g 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 ConvertStringTo SysString(char const blah[])
{
return system::string( blah);
}

Close enough for C++/CLI:
inline System::String ^ConvertStringT oSysString(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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #22
Rob Williscroft wrote:
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:

news:microsoft. public.dotnet.l anguages.vc

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?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #23
Trep wrote:
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::Stri ngToHGlobalAnsi (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::ToCharA rray:

#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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #24
Ioannis Vranos wrote in news:1098327645 .822082@athnrd0 2 in
comp.lang.c++:
Rob Williscroft wrote:
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:


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

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,


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.


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

Until such time please redirect to:

news:microsoft. public.dotnet.l anguages.vc

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.


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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #25
Rob Williscroft wrote:
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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #26
Ioannis Vranos wrote:

Rob Williscroft wrote:
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.


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.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #27
seesenk
1 New Member
For converting a String to char you use the following code.

char __nogc* myName1 = static_cast<cha r*>(Marshal::St ringToHGlobalAn si(strName).ToP ointer());
after the above one you will get your char *. once u finished free the memory allocated for the char*, as below.

Marshal::FreeHG lobal((int)myNa me1);

By marshalling only u can convert ur managed string to unmanaged char.

Regards,
Senthil
Feb 22 '06 #28

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

Similar topics

15
34602
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
5
10041
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
7
7568
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it. Also, I was wondering could there be issues in not making copies when converting??? I was thinking, when "pointing" to the same data with both managed and unmanaged strings/arrays couldn't there be issues such as the garbage collector trying to...
1
4383
by: joye | last post by:
Hello, How to convert an unmanaged string with char array type to a managed string with char array type? Thanks. Regards, Tsung-Yu
5
4718
by: joye | last post by:
Dear All, Many old C standard library is useful for string operation such as strcpy. But I meet some problem, a managed char array which declared in a structure and this structure has a instance declared in a__gc class. Sample code as following, namespace Configure { using namespace System ;
8
5910
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How can I convert this Byte() in this IntPtr ?
0
3703
by: Madhu_TN | last post by:
Hi All, I am new to this board. I am trying to create a Crystal Report viewer into a VS C++ Dot NET 2003 app ( This uses both managed and unmanaged code). I get the following compilation error: C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\ cstringt.h(875): error C2664: 'PtrToStringChars' : cannot convert parameter 1 from 'unsigned char *' to 'const System::String __gc *' The code segemnt is:
4
4660
by: Peter | last post by:
Does anyone know how to convert the following VB6 code to C# code? Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, Source As Any, ByVal bytes As Long) Dim sngArray(0) As Single strString = Chr$(107) & Chr$(62) & Chr(139) & Chr$(65) CopyMemory sngArray(0), ByVal strString, Len(strString) After running the code sngArray(0) = 17.40548
12
13550
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
0
9521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
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,...
1
9900
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
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8768
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...
0
6599
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
5214
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
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.