kihwaa

kihwaa.egloos.com

포토로그 마이가든



<ATL> Funtion Pointer define C++/CLI

외부 C++ project 또는 VB / C# 의 경우 COM으로 모듈을 배포 시 Callback interface를 정의하는 방법은 아래와 같다.

먼저 Callback interface를 생성한다.
[
    object,
    uuid(D072EB42-D7BD-46f2-85C0-A7B285061859),
    oleautomation,
    helpstring("ICallbackEntry Interface"),
    pointer_default(unique)
]
interface ICallbackEntry : IUnknown
{
    HRESULT Proc();
};

Callback interface Server 의 Proc()는 Callback 대상 함수 이다. 함수 정하는것은 암거나 해도 된다.

두번째로
Main 에서 아래와 같이 Entry를 적용할 수 있는 함수를 생성하여 준다.
[
 object,
 uuid(B005BB06-DD4C-4918-A5C5-04C64D04D705),
 dual,
 nonextensible,
 pointer_default(unique)
]
interface IManager : IDispatch{
 [id(1)] HRESULT Example([in] BSTR _name, BSTR _chartNum);
 [id(2)] HRESULT SetProc(ICallbackEntry* pEntry);
};

implementation에서는
ICallbackEntry* m_pEntry; 와 같이 함수 포인터 변수를 저장할 공간을 가지고 있으면 된다.

만약 Managed - Unmanaged 연동에 대해서는 IntPtr  type 와 Delegate을 사용하여 아래와 같이 CLI로 연동할 수도 있다.

namespace cppcli {

 public ref class Class1
 {
 public:
  Class1()
  {
   _Native = new Ccpp();
  }

  delegate int SomeEventHandler(int a, int b);

  event SomeEventHandler^ SomeEvent
  {
   void add(SomeEventHandler^ d)
   {
    System::IntPtr ptr = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(d);
    _Native->addEvent((Ccpp::CallbackType)ptr.ToPointer());
   }
   void remove(SomeEventHandler^ d)
   {
    System::IntPtr ptr = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(d);
    _Native->removeEvent((Ccpp::CallbackType)ptr.ToPointer());
   }
  }

  void doSomething()
  {
   _Native->go();
  }

 private:
  Ccpp* _Native;
 };
}


트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://kihwaa.egloos.com/tb/390357 [도움말]

덧글

댓글 입력 영역