VC + + Direct operation using C # class library basic operations

Source: Internet
Author: User

C # is simple and easy to operate, and has a relatively rich class library, easy to handle a variety of situations, such as the processing of strings, I think in C # is more convenient, or java and so on, with C + + It feels more cumbersome to handle, so it's used VC when you are programming, keep C # do not feel a pity, well, nonsense not much to say, into the topic!

Development environment:VS2005,VC + +

Project:VC + + MFC

1, the environmental conditions used

Create a new VC + + Project:

First environment configuration, right-click Items-- properties -- Configuration Properties -- general -- Find the right:

Character set, setting it to: using multibyte character sets

Common language Runtime support, setting it to: Common language Runtime support (/clr)

2, a reference to a namespace

namespaces that reference C # in files that need to use C #

For example, a class library that references C # 's System :

using namespace System;

But what if you want to quote your own library of classes?

There is also a way to implement this kind of invocation, first to generate a dll for C #

Then refer to this dll in VC + + below

#using ".. /xx/mydll.dll "

End Quote DLLs refer to their libraries as referenced in the system libraries above.

using namespace Mydll;

Summary Note:

VC + + reference only support to the first-level package reference, for more than the second class can not be used similar to the following method for reference

For example, you need a System.Data Error Reference method:using the namespace System. Data; This is an absolute error, and the compilation will produce the following error:

. \testmfccsdlg.cpp (8): Error C2143: syntax error : missing ";" ( in the "." the front )

So remember!

Then how do you refer to the two-level package above the class Method!

3, callingC #the method

After the reference, that's the Call of C # support for the library's class and class methods!

1) A simpleC #Print Statement Invocation

Example: print "Hello world!" in the console using C #

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

Results


From here we can see that this syntax is slightly different from that of C # , where calls to C # methods are made using C + + invocation

Here is the use of "::" rather than ". "To make a call to the method!

It is also important to note that "::" is a static method (class method) that calls C # , which is explained after a call to a C # non-static method (object method)!

2) CreateC #Object

For example: Create a C # String object and print out the object in the console:

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

Results


From here we can also create C # objects in VC + + in the C # environment, the difference

First, the object's declaration has to add a "^" symbol, followed by "gcnew" to create the object instead of "new"

3) CallC #method of the 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);

Results


From here we can see that the method of invoking C # objects is to use a similar C Language Reference, with"--" to make the invocation of the object method;

4) forC #Two-level packages and calls to 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);

Results


From here we can see, after referring to the first level package, for the two-level package object creation needs to use: (Two Class Package name:: Object name) to make the object reference, for more than two can be created: (two-level package name:: Three-level package name:: Object name) to make the object reference, and so on!

5) theC #of theStringobject is converted intoC + +of thestringObject

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

First of all, here are two methods, the specific conversion method is given here in the method, you need to understand the principle of the original text or method of the body

Convert S to osvoid marshalstring (String ^ s, string& os) {    using namespace runtime::interopservices;    Const char* chars = (const char*) (Marshal::stringtohglobalansi (s)). ToPointer ();    OS = chars;    Marshal::freehglobal (IntPtr ((void*) chars));} Convert 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 examples:

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<< "converted:" <<endl;cout<< "String to String:c-->a:a =" << a << endl;wcout<< "String to wstring:c-->b:b =" << b << Endl;

Results


As seen from here, the string was successfully converted to string and wstring

6) inMFC, you willStringconverted intoCStringand theCStringconverted toString

Example: Here I write the function directly, a very simple conversion

The String is converted to cstringcstring stringtocstring (string^ str) {    CString cStr = CString ((string^) str);    return CSTR;} CString converted to stringstring^ cstringtostring (CString cStr) {    string^ str = gcnew String (cStr);    return str;}


4, summarize

OK,VC + + call C # to the end of this!

Make a note to record the process, but also let the needs of the people less detours!

VC + + Direct operation using C # class library basic operations

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.