0

I'm calling a set of C# functions from a Python 3.10 script via the clr module from the PythonNet library. One of those functions returns a pointer of type System.Reflection.Pointer, that points to an array of float values.

I'm quite a bit confused as to how I'm exactly supposed to acquire or access the actual float array that the System.Reflection.Pointer variable is supposedly pointing to.

In my Visual Studio 2022 IDE, I can see that the pointer variable has a few class functions, such as ToString() or Unbox(), however none of those give me the desired float array.

What is the proper way of accessing the data pointed to by a System.Reflection.Pointer variable in Python?

Thanks for reading my post, any guidance is appreciated.

Runsva
  • 365
  • 1
  • 7

1 Answers1

0

You can't do that with only Python.NET APIs. Create a C# converter(as a class library and call it in Python, like the following example.

In C#, do like this.

using System.Reflection;

namespace Example;
public class Converter
{
    public static unsafe IntPtr PointerToIntPtr(Pointer p)
    {
        return (IntPtr)Pointer.Unbox(p);
    }
}

In Python, do like this.

import clr

clr.AddReference('path to the .NET dll')
import Example
...
# The ptr is assumed to be an instance of System.Reflection.Pointer
addr = Example.Converter.PointerToIntPtr(ptr).ToInt64()
# The addr will be an int.
relent95
  • 3,703
  • 1
  • 14
  • 17
  • Unfortunately, I'm unable to access or edit anything on the C# side here. I'm interfacing with some software, that was pre-packaged. I saw some other users talk about a `Marshal` class in C#, and it's class function `Copy`, however I have no idea how to use it. Would that be closer to what I need? – Runsva Jan 27 '23 at 09:25
  • You don't need to modify existing C# modules. You need to create a new [class library](https://learn.microsoft.com/en-us/dotnet/standard/class-libraries). It's very easy but you have to learn for a few hours if you don't have experience with C# and .NET. – relent95 Jan 27 '23 at 09:52