title: How to override a C++ virtual method (not in an interface class) in C# via C++/CLI
body: <p>For reference, this question deals with accessing the C++ NaturalPoint Camera SDK via C#.</p>
  
  <p>I am trying to pass along the implementation of a virtual method from C++ to C#. The trouble I am having is that the C++ virtual method is not in an interface class and I am not able to change that. Here is the class declaration:</p>
  
  <pre><code>class CLAPI cCameraListener
  {
  public:
      cCameraListener()  {};
      ~cCameraListener() {};
  
      virtual void FrameAvailable();
      virtual void FrameOverflow();
      virtual void ButtonPressed();
  };
  </code></pre>
  
  <p>The CLAPI is defined as:</p>
  
  <pre><code>#define CLAPI __declspec(dllexport)
  </code></pre>
  
  <p>I can't inherit from the cCameraListener class in a ref class since it isn't an interface class and haven't come up with any way to expose the virtual methods to a managed class. I also need to be able to register the listener which I talk about more below.</p>
  
  <p>Here is the working C++ implementation for listening for frames:</p>
  
  <pre><code>// process frames as they arrive
  class FrameListener: public cCameraListener
  {
  public:
    Camera *myCamera;
    FrameListener(Camera *cameraPtr)
    {
      myCamera = cameraPtr;
    };
  
    void FrameAvailable()
    {
      Frame *thisFrame = myCamera-&gt;GetFrame();
  
      // do something with the frame here
  
      thisFrame-&gt;Release();
    };
  };
  
  int _tmain(int argc, _TCHAR* argv[])
  {
    // startup camera
    CameraManager::X().WaitForInitialization();
    Camera *camera = CameraManager::X().GetCamera();
    if(camera==0)
    {
        // print an error
        return 1;
    }
  
    // set camera properties
    camera-&gt;SendEmptyFrames(false);
    camera-&gt;SendInvalidFrames(false);
    camera-&gt;SetVideoType(SegmentMode);
  
    // setup frame listener
    FrameListener *myListener = new FrameListener(camera);
    camera-&gt;AttachListener(myListener);
  
    // start the camera
    camera-&gt;Start();
  
    // while loop holding thread
  
    // clean up
    camera-&gt;Release();
    CameraManager::X().Shutdown();
  
    return 0;
  }
  </code></pre>
  
  <p>Here is some of the existing C++/CLI wrapper code:
  Link: <a href="https://code.google.com/p/npcamerasdkdotnet/" rel="nofollow">https://code.google.com/p/npcamerasdkdotnet/</a></p>
  
  <pre><code>public ref class MCamera{
  public:
  
    MCamera(Camera * camera){
      this-&gt;camera = camera;
  
    }
    ~MCamera(){
      delete this-&gt;camera;
    }
  
    void        Start(){camera-&gt;Start();} 
  
    void        SetExposure(int Value){camera-&gt;SetExposure(Value);}          
    void        SetThreshold(int Value){camera-&gt;SetThreshold(Value);}        
    void        SetIntensity(int Value){camera-&gt;SetIntensity(Value);}
  
    // TODO - WRAP THESE METHODS
    //void AttachListener(cCameraListener * Listener);
    //void RemoveListener(cCameraListener * Listener);
  
    // other wrapped methods here...
  
  private:
    Camera * camera;
  };
  </code></pre>
  
  <p>Notice that within the MCamera wrapper class, the AttachListener method needs to be implemented and requires a pointer to the cCameraListener class talked about above.</p>
  
  <p>Any suggestions would be appreciated! Thanks!</p>