Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Retrieving and using an IntPtr* through reflection

I'm currently working on some code which reflects over structures that are marshaled back from calls into a native dll. Some of the structs contain IntPtr* fields that point to null-terminated arrays of pointers. These fields require special processing. When reflecting over the structs, I can recognize these fields because they are marked by a custom attribute.

The following illustrates what I'm trying to do:

public void ProcessStruct(object theStruct)
{
    foreach (FieldInfo fi in theStruct.GetType().GetFields(BindingFlags.Public |  BindingFlags.Instance))
    {
        if (fi.FieldType.IsPointer && IsNullTermArray(fi))
        {
            //Has the custom attribute, commence processing of 
            //IntPtr* pointing to null-terminated array
            ProcessIntPtr(fi.GetValue(theStruct));
        }
        else{/*..Other Processing..*/  }
    }
}
public void unsafe ProcessIntPtr(IntPtr* ptr)
{
    //Iterate over the array and process the elements
    //There are pointer operations here.
}

The problem is that

  fi.GetValue(theStruct)

returns an object, which I obviously can't pass directly to ProcessIntPtr(). I cannot change the signature of ProcessIntPtr() to accept an object, as then I wouldn't be able to perform the pointer operations that I require. Obviously, I also can't cast from object to IntPtr*.

What techniques are available to handle this issue?

like image 215
Odrade Avatar asked Nov 27 '25 17:11

Odrade


1 Answers

While you may not be able to cast from Object to IntPtr*, you can cast to IntPtr. Remember, IntPtr* is just a pointer pointer. So you can get to the first pointer and then cast it back.

var ptr1 = (IntPtr)(fi.GetValue(theStruct));
var ptr2 = (IntPtr*)(ptr1);
like image 164
JaredPar Avatar answered Nov 29 '25 06:11

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!