Program C + + to C # interaction

Source: Internet
Author: User

Calling C/C + + generated DLL files for the first time is a bit fresher, and it's actually just something that implements "unmanaged code" that executes outside the control of the Common Language execution library (CLR), which is called "managed code" in the control of the Common Language execution library (CLR). How to use the unmanaged under the trusteeship? Now for those who are interested in the beginning of learning simply write an implementation of the whole process it (what questions do not laugh):

1. Use VS2008 to select a different language (C + +) to create a console application named Mydll1, and then select the application type as DLL, OK

Project

 

Add the following declaration under header file stdafx.h such as:

#definelibexport_api extern "C" __declspec (dllexport)
Libexport_apiintAdd (intA,intb);

in the MyDll.cpp This function is implemented in:



#include"stdafx.h"

intAdd (intA,intb
{
returna+b;
}

Note that the assumption of the implementation of the method is declared in the other header file, be sure to add # include "Xxx.h" to refer to this declaration of the function header file.

build mydll.dll and mydll.lib

2. referencing dll files in Visual C #. NET

The new Visual C # console application is named Testimportdll;

will be MyDll.dll and the MyDll.lib Copy to the executable file folder ():

adding references in Praogram.cs using System.Runtime.InteropServices;

declare one that will be referenced by, for example, the following MyDll.dll class for the function in:

  

 classTest
{
//[DllImport (". \\.. \\lib\\CppDemo.dll ")]
//Public static extern void Function ();

//[DllImport (". \\.. \\lib\\CppDemo.dll ")]
//public static extern int Add (int i, int j);
[DllImport (".. \\.. \\Lib\\Mydll1.dll")]
Public Static extern intAdd (intA,intb);
}

Finally, the output of this class is called in the main function:

 Static voidMain (string[] args)
{

Console.WriteLine ("Result:" +test. ADD (2, 3). ToString ());

Console.ReadLine ();
}The following is the code for Program.cs:END ():

Program C + + to C # interaction

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.