Problem creating overlay plug in in C# forms application

In a Windows Forms application I use the following code to add an overlay plugin to my display control:

unsafe
{
  var pt = new Point[2];
  pt[0].X = 0;
  pt[0].Y = 0;
  pt[1].X = 100;
  pt[1].Y = 100;
  fixed (Point* p = &pt[0])
  {
    int pAsInt = (int)p;
    int nought = 0;
    axCVdisplay1.AddOverlayObject("Line", "my line", false, false, 255, 255, false, 0, ref pAsInt, ref nought);
  }
}

I tried different plug ins but none worked :cry:

Sorry, AddOverlayObject won’t work (and might in fact even produce undefined behavior) with C#. The reason is that the parameters called Vertices and ObjectData are declared as pointers to 32 bit integers, but the CLR has no way of knowing how much data to marshal to the unmanaged code (in fact the amount of data varies with the overlay plugin that is being used).

That’s why the display control also has the AddOverlayObjectNET method which has a signature that the CLR can digest. To use AddOverlayObjectNET the vertices for the overlay plugin will need to be packaged in a Common Vision Blox pixel list:

Cvb.SharedPixelList pl;
Cvb.Image.CreatePixelList(2, out pl);
var tmp = new double[2];
tmp[0] = 0.0;
tmp[1] = 0.0;
Cvb.Image.AddPixel(pl, tmp);
tmp[0] = 100.0;
tmp[1] = 100.0;
Cvb.Image.AddPixel(pl, tmp);
axCVdisplay1.AddOverlayObjectNET("Line", "my line", false, false, 255, 255, false, 0, pl, IntPtr.Zero);
pl.Dispose();

If you’re using an overlay plugin that supports (or even requires) a plugin data structure (defined in iCVCPlugin.dll): The plugin data structs all have a member function ToIntPtr() than can be used for the last parameter of AddOverlayObjectNET (see for example the VBOverlayObject.NET demo).

By the way: The same is true for AddUserObject versus AddUserObjectNET.