(Original) C ++ string large character libraries: C-style string, STL string and. net string mutual neighbors (. net) (C/C ++) (C ++/CLI) (STL)

Source: Internet
Author: User

Abstract
Currently, Visual C ++ 9 has at least three types of strings: C-style string, STL string, and. net string. How can we interoperate these three strings?

Introduction
Use environment: Visual C ++ 9.0/Visual Studio 2008

When Momo receives a hardware card dealer's API for using C ++, he wants to go. net C # usage, of course, there is a good solution, because C ++/CLI is the only one that can understand. net and C ++. Finally, I suggest using C ++/CLI to import C ++ APIs, and then package them. NET component is used by C #. This raises a question: how can c-style string (char * s, char s []) be executed in C ++/CLI? success. net System: string?

Scenario 1:
C-style string, STL string character. Net string

This situation is more complicated than others. Generally, C ++ development dll must be packaged into. Net DLL for C.

Cstring2netstring. CPP/C ++/CLI

1 /*  
2 (C) oomusou 2007 Http://oomusou.cnblogs.com
3
4 Filename: cstring2netstring. cpp
5 Compiler: Visual C ++ 9.0/C ++/CLI 2.0
6 Description: Demo how to use string in C ++/CLI 2.0
7 Release: 07/23/2008 1.0
8 */
9
10 # Include " Stdafx. h "
11 # Include < String >
12
13 Using   Namespace System;
14 Using   Namespace STD;
15
16 Int Main (Array < String ^>   ^ ARGs ){
17 // C string & STL string to. Net string
18 Char C_s [] =   " Hello C " ;
19 String Stl_s =   " Hello STL " ;
20
21 String ^ Net_s1 = Gcnew string (c_s );
22 String ^ Net_s2 = Gcnew string (stl_s.c_str ());
23
24 Console: writeline (net_s1 );
25 Console: writeline (net_s2 );
26 }
27

Results

Hello C
Hello STL

You can use gcnew () to convert C-style string to STL string. net string, although it seems very simple, but at that time it took some time for research to discover.

Scenario 2:
. Netstring character C-style string, STL string

I haven't met this requirement yet. I just want to take a look at it.

Netstring2cstring. CPP/C ++/CLI

1 /*  
2 (C) oomusou 2007 Http://oomusou.cnblogs.com
3
4 Filename: netstring2cstring. cpp
5 Compiler: Visual C ++ 9.0/C ++/CLI 2.0
6 Description: Demo how to use string in C ++/CLI 2.0
7 Release: 07/23/2008 1.0
8 */
9
10 # Include " Stdafx. h "
11 # Include < String >
12 # Include " Stdio. h "
13 # Include < Iostream >
14 # Include < Msclr / Marshal. h > // . Net string to C-style string
15 # Include < Msclr / Marshal_cppstd.h >   // . Net string to STL string
16
17
18 Using   Namespace System;
19 Using   Namespace STD;
20 Using   Namespace Msclr: InterOP;
21
22 Int Main (Array < String ^>   ^ ARGs ){
23 // . Net string to C string
24 String ^ Net_s =   " Hello. net " ;
25 Marshal_context ^ Context = Gcnew marshal_context ();
26 Const   Char * C_s = Context -> Marshal_as < Const   Char *> (Net_s );
27 Printf ( " % S \ n " , C_s );
28
29 // . Net string to STL string
30 String Stl_s = Context -> Marshal_as < STD :: String > (Net_s );
31 Cout < Stl_s < Endl;
32 Delete context;
33 }

Results

Hello. net
Hello. net

This program requires the following three lines to be used, mainly by using al_as <t>. This is a unique usage of vc9 and cannot be used before vc8.

# Include < Msclr / Marshal. h > // . Net string to C-style string
# Include < Msclr / Marshal_cppstd.h >   // . Net string to STL string

Using   Namespace Msclr: InterOP;

25 rows

Marshal_context ^ Context = Gcnew marshal_context ();
Const   Char * C_s = Context -> Marshal_as < Const   Char *> (Net_s );

The. NET string can be converted to C-style string by using marshal_as <t>.

30 rows

// . Net string to STL string
String Stl_s = Context -> Marshal_as < STD :: String > (Net_s );

If you want to convert it to STL string and export al_as <t>, you only need to change it to STD: string.

All the string types that the financial_as <t> can interact with each other are sorted as follows.

Conclusion
C ++/CLI is a hypervisor.. NET and C/C ++. I really admire those coders who use C ++/CLI compiler, let the code of the two completely different worlds crash together. net, C, C ++, and STL merge methods can even run out of the results, it is very difficult to think.

Reference
How to convert from system: string * To char * in Visual C ++
STL/CLR: 2008 C ++/CLI tool of Zuse Cheng code blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.