Wednesday, 15 April 2015

clr - Managed C++ to form a bridge between c# and C++ -


I'm a bit wild, actually in fact my C ++ is cut off since the Freshman year of the college It has not been touched, so it's been a while.

Anyway, I'm doing the things that most people do, behind it. Calling C # code from C + code I have done some research online and it seems that I need to create some managed C ++ to create a bridge using __declspec (dllexport) and then create a dll and cover it Use the whole thing as.

But my problem is - for example, I really have a hard time. I found some basic things where a C # version wanted to be used in the string. Toopper (), but it was very original and only had a small code snippet.

Anyone have any ideas, where can I find something a little more concrete? Note, I do not want to use COM, not to touch the target C # code at all.

I'll just post it anyway ...

to your library The process of writing a cover is similar to reaching a standard .NET library.

Example C # class code in a project named CsharPProject:

  using the system; Namespace CsharpProject {public class CsharpClass {public string name {get; Set; } Public int value {get; Set; } Public string GetDisplayString () {return string.Format ("{0}: {1}", this.Name, this.Value); }}}  

You can create a managed C ++ class library project (eg CsharpWrapper) and add its C # project as its reference. In order to use the same header file in internal use and referenced projects, you need to use the correct declayspec by defining a preprocessor instruction (in this case CSHARPWRAPPER_EXPORTS ) and Using a #ifdef to set the export macro in the header file in your C / C + interface. Unmanaged interface header file should contain unmanaged content (or it is filtered by the preprocessor).

Unmanaged C ++ interface header file (CppInterface.h):

  #pragma once #include & lt; String & gt; // sets the decoration of the interface function as export or import #ifdef CSHARPWRAPPER_EXPORTS #define EXPORT_SPEC __declspec (dllexport) #else #define EXPORT_SPEC __declspec (dllimport) #endif // Unencrypted interface functions all unmanaged types EXPORT_SPEC std :: string GetDisplayString (const Char * pName, int iValue);  

Then you can create an internal header file to include in your managed library files. This will add to the namespace statement statement and it may include helpful functions that you require.

Managed C ++ interface header file (CsharpInterface.h):

  #program once #include  

Then you just type your interface code which does the wrapping.

Managed C ++ interface source file (CppInterface.cpp):

  #include "CppInterface.h" #include "CsharpInterface.h" std :: string GetDisplayString ( Const char * pName, int iValue) {CsharpClass ^ oCsharpObject = gcnew CsharpClass (); OCsharpObject-> Name = ToManagedString (pName); OCsharpObject-> Value = iValue; Return ToStdString (oCsharpObject-> GetDisplayString ()); }  

Then include unmanaged headers in your unmanaged project, tell the link file to be used when linking the linker and make sure the net and the casing DLL are in the same folder. As an unmanaged application

  #include & lt; Stdlib.h & gt; // wrapper header # include "CppInterface.h" void main () {// unmanaged wrapper function std :: string strDisplayString = GetDisplayString ("Test", 123); // Do something with printf ("% s \ n", strDisplayString.c_str ()); }  

No comments:

Post a Comment