Inheriting from native C++ in C# and passing the inherit class backward to c++ -


i've engine in native c++ dll , need use in c# project.

i'm totally new on doing that, i've been googling hours , know more or less how achieve it. know i've write c# or c++/cli wrapper class.

but haven't found how wrap aspects of dll c++ class. engine has class centity whith important part:

class centity   {       void sendmsg(centity *receiver);       virtual void receivemsg(msg msg); } 

that works follows: inherit class centity overrides receivemsg function implementing whants, , inheriting objects comunicate sending messages.

what need use functionality in c#: "inheriting" centity, overriding receivemsg on way c++ code can call it, , been able send messages other c# "inheriting" centity objets throw sendmsg c++ implementation. or in other words need c++ unmanaged code calls c# managed code. c++ code calling receivemessage() , need "override" or redirect call c# code.

is way of doing whithout changing dll? can't acces directly c++ code, if needed can ask dll modifications. if not, what'll minimum dll modifications?

thanks lot

it tricky:

ilogger.h

#pragma once  using namespace system;  namespace alpdfgenv4  {     public interface class ilogger      {     public:         virtual void log( string^ ltxt ) = 0;     }; } 

then lrlog.h

#pragma once  #include "cmsysstring.h" #include "clrilogger.h"  #include <vcclr.h>  using namespace system;  class cntvlogger;  namespace alpdfgenv4  {     public ref class logger     {     public:         logger(void);         virtual ~logger(void);          ilogger^ extlogger;             cntvlogger *ntv;              void log( string^ txt )             {                 extlogger->log( txt );             }         };      } 

and lrlog.cpp

#include "stdafx.h" #include "lrlog.h"  using namespace alpdfgenv4;  logger::logger(void) {     ntv = new cntvlogger;     ntv->clrlogger = this; }   logger::~logger(void) {     delete ntv; }  class cntvlogger : cmsyslogger { public:     gcroot<alpdfgenv4::logger ^> clrlogger;  protected:     void _internallog( lpctstr txt)     {         string ^str = gcnew string( txt );          clrlogger->log( str );     }  public:     bool init(void * obj)     {         return true;     }  };  

hope helps.

in example, class logger bridge tha allows keep link between native logger (used native code) , interface ilogger (used managed code pass class receives log output).

you'll need class in c++/cli, no need change dll approach.


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -