Language barriers: C ++ calls C # DLL

Source: Internet
Author: User

A long time ago, a wide discussion of language emerged in the blog Park. In fact, language disputes have never stopped in the entire programmer circle. In my opinion, language is just a tool. When you need to write the underlying layer, you can use C ++. When you need to write dazzling desktop applications, you can use C # WPF, when you need web development for large websites, you can select JAVA. Of course there are many other languages. This is just a choice for different projects. each language has a stage that it is good at, and what remains unchanged for years is data structures and algorithms. The words of the family are light. Below is the question: the obstacle to crossing the language: C ++ calls C # DLL when C # projects need to reference C ++ dll, you can directly use DLLIMPORT for calling. In reverse mode, C ++ projects cannot simply reference C # DLL. The default configuration of the C ++ project is not supported by the public Language Runtime Library. Therefore, we need to modify some configurations to call C # dll. C ++/cli to call c # dll: [Thank you for adding me: this example is only used by developers for research, c ++ clr conflicts with some c ++ compilation options, and does not support some mfc external link sources.] First, complete C # dll development: copy the code namespace Csharp {public class Class1 {public string Name = string. Empty; public Class1 () {Name = "We can use C #!! ";}}} Copy the code and compile it to obtain the Csharp dll. Next, configure the C ++ project attributes: add the common language runtime support/clr (c ++/cli). Next, add a new reference and select the Csharp compiled in step 1. # using and using namespace must be used in the dll code to call the dll. When allocating memory, pay attention to using gcnew. gc is required to allocate memory to it. The hosted object must be declared using ^. Copy the Code # include "stdafx. h "# using" Csharp. dll "using namespace Csharp; int _ tmain (int argc, _ TCHAR * argv []) {Class1 ^ a = gcnew Class1 (); printf (" % s/n ", a-> Name); return 0 ;}

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.