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 ;}}}