How C # Statically invokes a method in C + + (statically calling a DLL)

Source: Internet
Author: User

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)

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.