When we want to use C + + project methods in C #, this can be done by invoking the DLL of the C + + project, which has both static and dynamic invocation methods.
DLL is a dynamic link library file, also known as an "application extension", which is a software file type. In Windows, many applications are not a complete executable, and they are split into relatively independent, dynamic-link libraries, DLL files, placed in the system. When we execute a program, the corresponding DLL file is called. An application can use multiple DLL files, and a DLL file may also be used by different applications, such that DLL files are called shared DLL files. [1] (Baidu Encyclopedia)
We wrote the following method in the C + + project:
Then just copy the DLL file in Debug under C + + project into our C # bin\debug\ folder.
Then it is called in the code.
Attention:
Add attribute: callingconvention=callingconvention.cdecl
is mainly to deal with
The "PInvoke function" occurs when unmanaged code is called with managed code usetwihikvisiondlltest! Usetwihikvisiondlltest.twihikvision::getfirstpic "calls result in stack asymmetry. The reason may be that the managed PInvoke signature does not match the unmanaged target signature. Please check that the calling convention and parameters of the PInvoke signature match the unmanaged target signature. ”
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceCSharp calls to static dll{ Public Partial classForm1:form {[DllImport ("TestDll01.dll", CallingConvention =callingconvention.cdecl)]Private extern Static intTestcount (intAintb); PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) {MessageBox.Show (Testcount ( A, -). ToString ()); } }}
Operation Result:
How C # Statically invokes a method in C + + (statically calling a DLL)