Article source: blog.csdn.net/jarvischu/a…

WPF runs on the CLR, and its code is managed code.

DLL code written in C++ is unmanaged.

To invoke DLL code written in C++ in WPF, use:

 

[csharp]  view plain copy

  1. using System.Runtime.InteropServices;  
  2.    
  3.  [DllImport(“Dll.dll”, EntryPoint = “add”,CallingConvention=CallingConvention.Cdecl)]  
  4.  public static extern int add(int a, int b);  


\

 

The details are described below.

Compile and generate DLL files

In Visual Studio 2010, File –> New –> Project –> Visual C++ –> Win32 –> Win32 Project

For example, if we name the project Dll, in the dialog box that pops up, select the Dll and click Finish.

\

Add the code in dlL.cpp

 

[cpp]  view plain copy

  1. #include “stdafx.h”  
  2.    
  3. int add(int a,int b)  
  4. {  
  5.          returna+b;         
  6. }  

\

 

In order not to change the name when exporting, we add a Module definition File dll. def by right clicking on project name aAddaNew Item and selecting module-difinition File(. Def) from the pop-up dialog box.

Add the code in dlL.def

 

[cpp]  view plain copy

  1. LIBRARY Dll.dll  
  2. EXPORTS  
  3. add  


\

 

Build project is ok. The Dll. Dll file is generated in the Debug directory.

 

[note] for this section on C++ DLL, please refer to my previous posts.

 

Use DLLS in WPF

Create a new WPF project.

Copy the dll. Dll file to the Debug directory of the WPF project.

\

[csharp]  view plain copy

  1. // Some other namespaces
  2. using System.Runtime.InteropServices;  
  3.    
  4. namespace Wpf  
  5. {  
  6.     /// <summary>  
  7.     /// Interaction logic for MainWindow.xaml  
  8.     /// </summary>  
  9.     public partial class MainWindow : Window  
  10.     {  
  11.         [DllImport(“Dll.dll”, EntryPoint = “add”,CallingConvention=CallingConvention.Cdecl)]  
  12.         public static extern int add(int a, int b);  
  13.    
  14.         public MainWindow()  
  15.         {  
  16.             InitializeComponent();  
  17. Inta = MainWindow. Add (12, 14);
  18.             MessageBox.Show(a.ToString());  
  19.         }  
  20.     }  
  21. }  


\

Matters needing attention

1. Dll. Dll must be copied to the Debug directory of the WPF project

2. Be sure to pay attention to the calling conventions of the stack

The default calling convention for Visual C++ code is the C calling convention (__cdecl).

Instead of the standard calling convention (__stdCall) or Pascal calling convention (__pascal)

So in DllImport, the CallingConvention parameter must be set to CallingConvention.Cdecl.

 

Of course, we can also modify the Dll. Dll calling convention, such as

[cpp]  view plain copy

  1. int WINAPI  add(int a,int b)  

Set the add calling convention to WINAPI, which is the standard calling convention,

This way, when introduced in WPF, the DllImport’s CallingConvention argument can be omitted because the default is the standard CallingConvention

[csharp]  view plain copy

  1. [DllImport(“Dll.dll”, EntryPoint = “add”]  

\