How to call the DLL written in C ++ in C sharp

Source: Internet
Author: User

WPF is widely used, and the Development user interface is indeed very convenient. Sometimes it is necessary to call a lot of DLL libraries previously written in C ++, which involves how to call the DLL compiled in C ++ in C sharp. After some attempts, we found that the call was actually relatively simple. The procedure is as follows:

1: In this example, the DLL name is test. dll. The provided interface function is also very simple: the return value of the two numbers (INT addfunc (int A, int B ))

Copy the DLL to the folder to be generated by the exe.

2: Remember to add using system in C sharp code. runtime. interopservices (otherwise, use the [dllimport ("test. DLL ")], the following error message is displayed: The type Oro namespace name 'dllimpt' could not be found.

3: The WPF test code is as follows. Click the test button to go to The button_click_test () function. One-Step debugging shows that testvalue is 8, indicating that the DLL call is successful.

 

 

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using system. windows; using system. windows. controls; using system. windows. data; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. imaging; using system. windows. navigation; using system. windows. shapes; using system. runtime. interopservi CES; // Add namespace wpfapplication {// <summary> // interaction logic for mainwindow. XAML // </Summary> Public partial class mainwindow: window {public mainwindow () {initializecomponent ();} [dllimport ("test. DLL ")] // load test. DLL public static extern addfunc (int A, int B); // you can directly call the interface function provided by the DLL. Private void button_click_test (Object sender, routedeventargs e) {int testvalue = 0; testvalue = addfunc (3, 5); Return ;}}}

 

 

Related Article

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.