C # calls the C + + export (dllexport) method

Source: Internet
Author: User

Development environment:

Visual Studio 2013+win10

One: Building C + + projects

To build C++project (Win32 project) in VS, be aware that you want to tick:

Application Type:dll

Additional Options:export Symbols

Put the code in the project header file:

#define WAOCVDLL_API __declspec (dllexport)//custom method Extern_c Waocvdll_api int _stdcall Fit (int width, int height, wchar_t*i Mage, Float*firstpoint, int pointslength);

Then implement the method in the CPP file:

Waocvdll_api int _stdcall Fit (int imagewidth, int imageheight, wchar_t*image, float*firstpoint, int pointslength) {//implementation code }

Ps:waocvdll_api different for each project

You need to set the project properties before compiling:

Compile As:compile as C + + code

The platform structure of the Ps:c++ project must be consistent with the invocation of the project (if x64 is x64 and vice versa).

Ok,build project, get DLL

Two: Build the Calling Project (C # Console)

What you should be aware of when calling:

[DllImport (@ "Your DLL storage path", EntryPoint = "method name defined in C + +")]extern static unsafe int Fit (int width, int height, char* image, Floa t* firstpoint, int pointslength);

After that, the unmanaged code is called in C #, and it is important to note that:

1. Set project properties: Allow unsafe code

2. Add unsafe code blocks to your code

OK, run the console project, the project is working properly!

C # calls the C + + export (dllexport) method

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.