C # indirectly calls native C + + DLLs using clr/c++ DLLs

Source: Internet
Author: User

C # indirectly calls native C + + DLLs using clr/c++ DLLs

Development Environment: Win 7 VS2010

Summary: the EXE for C # uses clr/c++ to indirectly invoke native C + + Dlls.

First step: Create a C # console application project-named "ConsoleApplication1".

Step two: Create a clr/c++ project, right-click on "ConsoleApplication1" above "solution ' ConsoleApplication1 '"-->add-->newproject--> Clr-->class Library---name "netcpp".

Step three: Create a native C + + project, ditto, Right-click-->add-newproject-->win32-->win32 Project

--name "nativecpp"-->next--> Select "DLL"-->export symbols.

To create three projects above, start writing code and setting up the Environment:

Fourth step: open "NativeCpp.h" in which some member functions and variables are added;

#defineNativecpp_api __declspec(dllexport)

This class is exported from the NativeCpp.dll

class nativecpp_api cnativecpp {

public :

Cnativecpp (void);

Todo:add Your methods Here.

int Geta ()

{

Return 20;

}

};

Fifth step: refer to Nativecpp DLL in Netcpp project;

Right-click Netcpp Engineering-->properties







Sixth step: Open the Clr/c++ project "netcpp.h ", Adding #include   "NativeCpp.h"

#include  < Span style= "color: #a31515; font-family:consolas; font-size:small; " > "NativeCpp.h"

using namespace System;

namespace netcpp {

public ref class Class1

{public:

int Getb ()

{

Cnativecpp a;

return A.geta ();

}

};

}

Seventh Step: Right-click ConsoleApplication1 Project References-->add reference-->projects--> Select "netcpp"

then copy the DLL generated by native C + + into the bin\\debug\\ directory of the C # project . (if you want to dynamically update native C + + DLLs after modifying the nativec++ code , you need to set them in the properties of the C # project: "propertiesàBuild Eventsà Post-build event Command line: write command: copy $ (solutiondir) debug\nativecpp.dll $ (TargetDir) ) /c7>

Eighth Step: Open ConsoleApplication1 engineering program.cs.

using System;

using System.Collections.Generic;

using system.linq;

using system.text;

namespace ConsoleApplication1

{

class program

{

Static void Main (string[] Args)

{

Netcpp. Class1 cl = New netcpp. Class1 ();

Console . WriteLine ("geta ()" + c1.getb (). ToString ());

Console . Read ();

}

}

}

Original Address: http://blog.sina.com.cn/s/blog_a50d2d7401018rxr.html

C # indirectly calls native C + + DLLs using clr/c++ DLLs

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.