VC ++ directly uses C # class library for basic operations,

Source: Internet
Author: User

VC ++ directly uses C # class library for basic operations,

C # is simple and easy to operate, and there are a wide variety of class libraries to facilitate the processing of various situations, such as processing strings, I think it is more convenient to use C #, or use java, etc, it feels complicated to use C ++ for processing, so it is a pity to leave C # in the process of programming with VC. Well, let's not talk much about it. Let's get started!

 

Development Environment: VS2005, VC ++

Project: VC ++ MFC

1. Environment Conditions used

Create a VC ++ project:

First, configure the environment, right-click the project, choose Properties> Configure Properties> General> Find the right:

Character Set to: Use the multi-Byte Character Set

Support the public Language Runtime Library and set it to: support the public Language Runtime Library (/clr)

 

2. namespace reference

REFERENCE The namespace of C # in the file that requires C #

For example, reference the C # System Class Library:

Using namespace System;

But what if I want to reference my own class library?

There is also a way to implement such calls. First, the C # will generate a dll

Then reference this dll in VC ++ as follows:

# Using "../xx/mydll. dll"

After the dll is referenced, the system library is referenced to reference its own library.

Using namespace mydll;

 

Note:

The reference of VC ++ only supports the reference of level-1 package. for level-2 and above, the following method cannot be used for reference:

For example, you need to use the System. Data Error Reference Method: using namespace system. Data; this is an absolute error and the compilation will produce the following error:

. \ Tew.fccsdlg. cpp (8): error C2143: syntax error: ";" missing (before)

So remember!

Then how do you reference the method above the second-level package!

 

3. Call the C # method.

After the reference, it is C # that supports the call of library classes and class methods!

 

1) Simple C # print statement call

For example, use C # To print "Hello World!" on the console !"

Console: WriteLine ("Hello" + "World! ");

Result


Here we can see that this syntax is slightly different from that of C #. Here we use the C ++ call method to call the C # method.

":" Rather than "." is used for method calling!

Note that ":" is a static method (Class Method) that calls C #. It is described after calling C # non-static method (Object method!

 

2) create a C # object

For example, create a String object of C # and print the object on the console:

String^ str = gcnew String("String");Console::WriteLine(str);

Result

 

Here we can also create a C # object in VC ++, which is different from the C # environment,

First, a "^" symbol is added to the object declaration. Second, "gcnew" is used to create an object instead of "new"

 

3) Call the method of the C # object (non-static method)

For example, call the Equals method of String and print the comparison result:

String^ str = gcnew String("String");bool b = str->Equals("String");Console::WriteLine("str = " + str+ "--------->str.equals('String')?:" + b);

Result

 

From this we can see that the method for calling the C # object is similar to that of the C language, and "->" is used to call the object method;

 

4) calls to C # second-level packages and libraries above

For example, create a StringBuilder object under the System. Text package and print it out:

Text::StringBuilder^ sb = gcnew Text::StringBuilder("StringBuilder");Console::WriteLine("sb = " + sb);

Result

 

From this we can see that, after referencing a level-1 package, you need to use (level-2 package name: Object Name) to create a level-2 Package for object reference, for a second-level or above, you can create a second-level package: (second-level package name: third-level package name: Object Name) to reference the object, and then push it like this!

 

5) convert the String object of C # To the string object of C ++

Reference from: https://msdn.microsoft.com/zh-cn/library/1b4az623%28VS.80%29.aspx

First, two methods are provided here. The specific conversion methods are provided here. If you need to understand the principles, you can refer to the original text or method body.

// Convert s to osvoid externalstring (String ^ s, string & OS) {using namespace Runtime: InteropServices; const char * chars = (const char *) (Marshal :: stringToHGlobalAnsi (s )). toPointer (); OS = chars; Marshal: FreeHGlobal (IntPtr (void *) chars);} // converts s to osvoid MarshalString (String ^ s, wstring & OS) {using namespace Runtime: InteropServices; const wchar_t * chars = (const wchar_t *) (Marshal: StringToHGlobalUni (s )). toPointer (); OS = chars; Marshal: FreeHGlobal (IntPtr (void *) chars ));}

Test example:

String a = "test"; wstring B = L "test2"; String ^ c = gcnew String ("abcd"); cout <"before conversion:" <endl; cout <"a =" <a <endl; wcout <"B =" <B <endl; Console :: writeLine ("c =" + c); MarshalString (c, a); MarshalString (c, B); cout <"after conversion:" <endl; cout <"String to string: c --> a: a =" <a <endl; wcout <"String to wstring: c --> B: B = "<B <endl;

Result

 

The String is successfully converted to string and wstring.

 

6) in MFC, convert String to CString and CString to String.

Example: The function I wrote is provided here, which is a simple conversion.

// String to CStringCString StringToCString (String ^ str) {CString cStr = CString (String ^) str); return cStr ;} // convert CString to StringString ^ CStringToString (CString cStr) {String ^ str = gcnew String (cStr); return str ;}


 

4. Summary

Now, it's time for VC ++ to call C!

Make a note to record this process and reduce the number of people in need!

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.