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