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

PN: Managed and Unmanaged Strings

Hi All,
I have some older C++ code that reads databases via ODBC and converts
data to C++ null delimited strings. I amd writing a tool to read
these types of old C strings and update an XML file.

The XML file is being loaded by managed classes. I want to convert a
c string to a C++.net string class. Or, more correctly, take a C
string and put it into a C++.net string. Something like the following:

char* test_string1

strcpy(test_string1,"This is test string1")

and get it into a structure/array that has String data types....

I've been reading about __box etx but I have not been able to figure
this one out and get it to work. Any/all advice most welcome.

Peter Nolan
Nov 16 '05 #1
2 1629
Hi All,
just in case it helps someone else...

It turns out that
char* test_string = "This is some text" ;
String * test2 ;
test2 = test_string ;

moved the test_string to test2 string just fine...though I have not
managed to get a managed string back to a non_managed string but it
looks like I could write a function to copy uit our char by char...

Best Regards

Peter Nolan
www.peternolan.com

pe***@peternolan.com (Peter Nolan) wrote in message news:<f4**************************@posting.google. com>...
Hi All,
I have some older C++ code that reads databases via ODBC and converts
data to C++ null delimited strings. I amd writing a tool to read
these types of old C strings and update an XML file.

The XML file is being loaded by managed classes. I want to convert a
c string to a C++.net string class. Or, more correctly, take a C
string and put it into a C++.net string. Something like the following:

char* test_string1

strcpy(test_string1,"This is test string1")

and get it into a structure/array that has String data types....

I've been reading about __box etx but I have not been able to figure
this one out and get it to work. Any/all advice most welcome.

Peter Nolan

Nov 16 '05 #2
Peter,

Remember that System::Char is not the same as char. It's a 16-bit character
(or wchar_t). You might get away with copying the characters in a
System::String to a char[], but that would be luck. The code to do this
correctly is below. A System::Byte is an *unsigned* byte; I believe the C++
standard failed to define whether char is signed or unsigned, but it
shouldn't matter (corrections welcome!).

Tony

#using <mscorlib.dll>

using namespace System;

int _tmain()

{

System::String* name = S"Tony Nassar";

System::Char chars __gc[] = name->ToCharArray();

System::Text::ASCIIEncoding* encoding = new System::Text::ASCIIEncoding();

System::Text::Encoder* encoder = encoding->GetEncoder();

int numEncodedBytes = encoder->GetByteCount(chars, 0, name->Length, true);

System::Byte bytes __gc[] = new System::Byte[numEncodedBytes + 1];

encoder->GetBytes(chars, 0, chars->Length, bytes, 0, true);

bytes[numEncodedBytes] = 0; // null-termination, C-style; the CLR seems to
do this for you, actually.

return 0;

}
Nov 17 '05 #3

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

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
22
by: Alper AKCAYOZ | last post by:
Hello Esteemed Developers and Experts, I have been using Microsoft Visual C++ .NET for 1 year. During this time, I have searhed some topics over internets. Most of the topics about .NET is...
1
by: Zapbbx | last post by:
I have a 3rd party application that can reference external dll's. The dll's have to be written in unmanaged code with an exported function I can reference and call. I would like it to call a C# dll...
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
7
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....
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...
3
by: _BNC | last post by:
I have an old C DLL that I want to access via C#. I'm doing this via an outer DLL that wraps the old C DLL in an unmanaged C++ class, which is in turn wrapped in a Managed C++ class. Both these...
3
by: Tommy Svensson \(InfoGrafix\) | last post by:
I've been instructed to work againt a huge unmanaged C++ API from a C# application. Now, the only way, as I've understood it, is to go the Managed Extensions for C++ way. This means I have to...
3
by: Steve | last post by:
I have former VC++ 6.0 application which was ported to .NET. Now we continue to develop this app under VS.NET. The app now contains both managed and unmanaged classes. Now I have a problem with...
16
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.