简体   繁体   中英

How can I read from unmanaged memory in C#?

I'd like to create Foo objects in C# from an unmanaged array of Foo structs created in C++. This 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 C# side: (I ommitted some fancy code making it possible to call the C++ functions from within C# for better readability);

List<Foo> detectFooObjects()
{
    IntPtr fooDetector = createFooDetector();

    IntPtr detectedFoos = IntPtr.Zero;
    detectFoo(fooDetector, ref detectedFoos);

    // How do I get Foo objects from my IntPtr pointing to an unmanaged array of Foo structs?

    releaseFooObjects(detectedFoos);

    releaseFooDetector(fooDetector);
}

But I don't know how to retrieve the objects from the IntPtr detectedFoos . It should be possible somehow... Any hints?

UPDATE

Let's assume, Foo is a simple detection rectangle.

C++:

struct Foo
{
    int x;
    int y;
    int w;
    int h;
};

C#:

[StructLayout(LayoutKind.Sequential)]
public struct Foo
{
    public int x;
    public int y;
    public int width;
    public int height;
}

Is it possible to read from unmanaged memory and create new managed objects from it before releasing the unmanaged memory?

I don't know how may Foo objects will be detected, so I don't know, how much memory to allocate in C# before calling detectFoo() . That's why I alloc/free memory in C++ and just pass a pointer to it. But somehow I can't retrieve the detectedFoo s pointer address under C#. How do I do that?

You must re-declare Foo in your C# project. Assuming you know the count of Foo s and the value of sizeof(Foo) you should be able to use System.Runtime.Interopservices.Marshal.PtrToStructure() to retrieve your Foo structures one at a time.

You have to define your struct again in C#, and it depends on your stuct. Your struct have to be blitable (the memory layout of the C# struct ave to be the same as it is for the C struct)

Have a look at "Marshalling structs"

Or post your real "Foo" struct and i can show you the C# version

UPDATE:

Because your struct seems to be blitable, you can simple cast the pointer to unmanaged memory to a pointer to the struct defined in c#:

If it is ok for your application to use unsafe code you can write:

unsafe List<Foo> detectFooObjects()
{
 List<Foo> res = new List<Foo>()
IntPtr fooDetector = createFooDetector();

IntPtr detectedFoos = IntPtr.Zero;
int nNumFoos = detectFoo(fooDetector, ref detectedFoos );
for(int i=0;i<nNumFoos;i++)
{
   Foo** ppDetectedFoos = detectedFoos.ToPointer();

   Foo* pFoo = *ppDetectedFoos
   res.Add(*pFoo); //copies the struct because is a struct
   ppDetectedFoos++:
}

releaseFooObjects(detectedFoos);

releaseFooDetector(fooDetector);
return res;
}

我最终通过使用C ++ / CLI包装器类解决了我的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM