0

Let's say I have a C# program which needs some implementation done in C++(machine learning for example). For this I want a DLL which opens a video stream, processes "things" and then my C# program can grab those "things".

I find countless examples on how to create a DLL and calling functions of the DLL like "ShowImage()" or "GetImage(image_struct *theImage)"...where the latter you allocate memory in C# and let the C++ DLL read into it.

Here a DLL fills the data object with something...data object is created inside C# code

C++ code

extern "C" void __declspec(dllexport) __stdcall GetRawImageBytes(unsigned char* data, int width, int height)
{
    unsigned char* final = someAwesomeDataGenerated();
    std::memcpy(data, final.data, final.total() * final.elemSize());
}

C# Code

//Declarations
[DllImport("OpenCVDLL")]
private static extern void GetRawImageBytes(IntPtr data, int width, int height);
private GCHandle pixelHandle;
private IntPtr pixelPtr;
private Color32[] pixel32;

//later in some function...
pixel32 = tex.GetPixels32();
pixelHandle = GCHandle.Alloc(pixel32, GCHandleType.Pinned);
pixelPtr = pixelHandle.AddrOfPinnedObject();
GetRawImageBytes(pixelPtr, tex.width, tex.height);

This is an example of how I handle receiving images currently.

Now I explain which use case I find no real example/explanation of how to do:

I want objects to be created and hold by the DLL, meaning that the lifetime of objects is longer than only the single function call I make.

In a pure C++ environment I would do something like the following:

pseudo code

    main()
{
    Dll.createSomeObject();
    Dll.InitializeObject();
    float valueFromDLL = Dll.GiveMeSomething();
    Dll.DeleteObject();
}

But how do I model this with C# ? Note that my main concern is about the lifetime of "SomeObject" in the Dll example here.

Captain GouLash
  • 1,257
  • 3
  • 20
  • 33
  • We don't know how you currently obtain that `IntPtr` that you pass to your DLL. If you use `AllocHGlobal` or similar, then you have nothing to worry about. You can also use `GCHandle` to pin managed objects, e.g. to pin a `byte[]`. – David Heffernan Nov 22 '18 at 08:57
  • I added the missing pieces to the C# part in the question. You hit spot on that i pin a byte[] object here. But what I want is not a pinned byte[] object(which works in this code) but instead an object which lifecycle is longer than a function call(on the c++ side) – Captain GouLash Nov 22 '18 at 09:16
  • Thanks for pointing this out. But is it really a good practive to have "just" a static variable inside the dll which is manipulated by functions from dllexport? – Captain GouLash Nov 22 '18 at 09:23
  • Thanks for this, I will try to figure out the best solution for my case with this information. – Captain GouLash Nov 22 '18 at 09:33
  • Once you have pinned the object, it lives until you unpin it. You can make as many calls to the DLL as you like between pinning and then unpinning. – David Heffernan Nov 22 '18 at 10:01

0 Answers0