Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2006

Tip: Looking for answers? Try searching our database.

to set a native callback to managed member function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mot133t@gmail.com - 21 Jul 2006 22:55 GMT
I will quickly try to write a code that shows my intention. I will pass
unnecessary code.

typedef void(FP*)(LONG _param1, LONG _param2, BYTE* _pData);
class CNative
{
public: FP m_CallBack;

private: void JobDone()
  {
     for(iter i = m_Jobs.begin();i!=m_Jobs.end();++i)
     {
        // WORKS TWICE THAN CRASH
        if(m_CallBack!=NULL) (*m_CallBack)(i->param1,i->param2, NULL);
     }
  }
};

ref class Managed
{
private: delegate void Handler(LONG _param1, LONG _param2, BYTE*
_pData);

private: CNative* m_Unmanaged;
private: Handler^ m_Handler;

private: /*USELESS static*/ void /*USELESS __stdcall  */ Celebrate
  (LONG _param1, LONG _param2, BYTE* _pData) {
  // Invoke a nice event
  }

public: void DoIt() {
   m_Handler = gcnew Handler(this,&Managed::Celebrate);
   // USELESS: pin_ptr<Handler^> pin(&m_Handler);
  m_Unmanaged->m_CallBack =
     Marshall::GetFunctionPointerForDelegate(m_Handler).ToPointer();
  m_Unmanaged->DoYourJob();
  }
};

I do get the right values from (*m_CallBack) twice. But then I beleive
stack is corrupted during function call, and BOOM :)

Can someone please tell me that this can not be done? I even don't know
how does it work twice.
And if you can tell the reason why is really appreciated. You can try
to explain me why do I try to do this :)
mot133t@gmail.com - 22 Jul 2006 10:44 GMT
Hi, again it's me.

Here is another attemp that failed but in a much nicer way:) This time
my function pointer cast is a used trick, which should work.

Now, I do have an exception at least : Cannot pass a GCHandle across
AppDomains.

// NO CLR

class CBase;
typedef void(CBase::*FP)(LONG _param1, LONG _param2, BYTE* _pData);
class CBase
{
public:
  FP JobDoneCallBack;
  CBase(){}
  ~CBase(){}
}

class CNative: public CBase
{

private: void JobDone()
  {
     for(iter i = m_Jobs.begin();i!=m_Jobs.end();++i)
     {
        // THIS->* is required, and I dont know why, CBase::* does not
work either
        if(CBase::m_JobDoneCallBack!=NULL)
           (this->*m_JobDoneCallBack)(i->param1,i->param2, NULL);
     }
  }

};

// CLR

delegate void Handler(LONG _param1, LONG _param2, BYTE* _pData);

class CDerived: CBase
{
private:
  CBase* m_pSource;
  gcroot<Handler^> m_Event;
public:
  CDerived(CBase* _pSource, Handler^ _event)
  :m_pSource(_pSource),m_Event(_event)
  {
     m_pSource->JobDoneCallBack = static_cast<FP>(&CDerived::Raiser);
  }
private:
  void Raiser(LONG _param1, LONG _param2, BYTE* _pData)
  {
     // THIS IS WHERE THE EXCEPTION OCCURS
     // Cannot pass a GCHandle across AppDomains.
     m_Event->Invoke(_param1, _param2, _pData);
  }

}

ref class Managed
{

private: CNative* m_Unmanaged;
private: CDerived* m_Raiser;

private: void Celebrate(LONG _param1, LONG _param2, BYTE* _pData) {
  // Invoke a nice event
  }

public: void DoIt() {
   // REGISTERING FOR EVENT
   m_Raiser = new CDerived(m_Unmanaged,
                                                  gcnew
Handler(this,&Managed::Celebrate));
  m_Unmanaged->DoYourJob();
  }

};

Thanks to everyone who has read the entire code:) As always, your
thoughts are really appreciated.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.