4

I need to fire an event written in C++ / CLI from an unmanned function in c++.

What is the best way to do this?

Thanks in advance.

coolshashi
  • 420
  • 1
  • 6
  • 19

1 Answers1

1

I figured out with some help from some help posts on codeproject

Thought of posting it here could be useful for others

#include "stdafx.h"
#include "windows.h"

using namespace System;
using namespace System::Runtime::InteropServices;

class Camera
{
public:
    Camera()
    {
    }
    ~Camera(){}
    void (*test)(void);
    void OnNewCameraData();
    void StartDataAcquisition();

};

void Camera::StartDataAcquisition()
{
    int i;
    while(i<10)
    {
        test();
        i++;
        Sleep(1000);
    }

}

delegate void FunctionToCallDelegate();

ref class CameraAdapter
{
private:
  Camera *_camera;
  FunctionToCallDelegate ^_Function;
public:
    CameraAdapter(FunctionToCallDelegate ^handler)
    {
        _Function = handler;
    }
    void Init()
    {
        _camera = new Camera();
        pin_ptr<FunctionToCallDelegate^> tmp = &_Function;
        _camera->test = (void (__cdecl *)(void))(Marshal::GetFunctionPointerForDelegate(_Function).ToPointer());
        _camera->StartDataAcquisition();
    }

    ~CameraAdapter()
    {
        delete _camera;
        _camera = 0;
    }
  void OnNewCameraData()
  {
      Console::WriteLine("Received Frame \n");

  }  
};


int main(array<System::String ^> ^args)
{

    FunctionToCallDelegate ^dsi;
    dsi += gcnew FunctionToCallDelegate(gcnew CameraAdapter(dsi), &CameraAdapter::OnNewCameraData);
    CameraAdapter ^camera = gcnew CameraAdapter(dsi);
    camera->Init(); 
    Console::ReadKey();
    return 0;
}
coolshashi
  • 420
  • 1
  • 6
  • 19