I would like to create objects Fooin C # from an unmanaged array of structures Foocreated in C ++. Here is how I think it should work:
On the C ++ side:
extern "C" __declspec(dllexport) void* createFooDetector()
{
return new FooDetector();
}
extern "C" __declspec(dllexport) void releaseFooDetector(void* fooDetector)
{
FooDetector *fd = (FooDetector*)fooDetector;
delete fd;
}
extern "C" __declspec(dllexport) int detectFoo(void* fooDetector, Foo **detectedFoos)
{
FooDetector *fd = (FooDetector*)fooDetector;
vector<Foo> foos;
fd->detect(foos);
int numDetectedFoos = foos.size();
Foo *fooArr = new Foo[numDetectedFoos];
for (int i=0; i<numDetectedFoos; ++i)
{
fooArr[i] = foos[i];
}
detectedFoos = &fooArr;
return numDetectedFoos;
}
extern "C" __declspec(dllexport) void releaseFooObjects(Foo* fooObjects)
{
delete [] fooObjects;
}
On the side of C #: (I missed some fancy code that allows you to call C ++ functions from within C # for better readability);
List<Foo> detectFooObjects()
{
IntPtr fooDetector = createFooDetector();
IntPtr detectedFoos = IntPtr.Zero;
detectFoo(fooDetector, ref detectedFoos);
releaseFooObjects(detectedFoos);
releaseFooDetector(fooDetector);
}
But I do not know how to get objects from IntPtr detectedFoos. It should be possible somehow ... Any clues?
UPDATE
Suppose Foo- this is a simple detection rectangle.
C ++:
struct Foo
{
int x;
int y;
int w;
int h;
};
WITH#:
[StructLayout(LayoutKind.Sequential)]
public struct Foo
{
public int x;
public int y;
public int width;
public int height;
}
Can I read from unmanaged memory and create new managed objects from it before releasing unmanaged memory?
, Foo, , # detectFoo(). / ++ . - detectedFoo #. ?