covernting SByte [] to SByte *

G

Guest

I am attempting to pass an SByte [] to another .NET class that expects the
data to be passed as SByte *. I searched around and found the boxing can be
used to produce a pointer but I'm not sure if this is appropriate. I am using
the following sequence:

// Load class
Assembly c = Assembl.LoadFrom("filename ....);

// Get the types
cTypes = c.GetTypes();

// Get the appropriate type entry
myType=cTypes[1];

// Get the infos
pInfos = myType.GetMethods();

// Get the appropriate info entry
myInfo=pInfos[0];

// Create a class instance
Object myClass = myType.InvokeMember(null, BindingFlags.CreateInstance ...

// Data to pass
SByte [] myData = new SByte[3] {1,2,3};

// Invoke method
myType.InvokeMember(myInfo.Name,
BindingFlags.InvokeMember ...
null,
myClass,
myData);

However this signature does work nor does

Object [] parms = new Object [1];
parms[0] = myData;
myType.InvokeMember(.....,parms);

Doing:

ParameterInfo[] parmInfo = myInfo.GetParameters [];

shows that parmInfo[0].ParameterType is SByte*

Now how do I get from my SByte [] is SByte *
 
N

Nicholas Paldino [.NET/C# MVP]

Pat,

The only way to do this is through unsafe code. The SByte* parameter
can only be used in unsafe code. In order to do this, you would want to do
this:

// Need unsafe code. Assume your array is stored in a variable named array.
unsafe
{
// Get the pointer. This is not boxing, it's unsafe code. The pointer
has to be
// fixed so it doesn't jump around.
fixed (SByte* arrayPointer = array)
{
// Make the call, passing arrayPointer for the SByte* parameter
here.
}
}

Hope this helps.
 
G

Guest

Hi Nicholas,

It has been a long time since we last conversed.

I was able to use the unsafe & fixed as you specified, however,
I have a problem with the last argument on the InvokeMember for the class.

This arguments represents a list of object arguments to be passed in the
call to the class method.

I am unable to expicitly use the ptr as the last agrument in tha call
because that gives an argument mismatch on the InvokeMember.

I tried to cast the ptr as an object but get the message:
Cannot convert type 'sbyte*' to 'object'.

I also tried to create an object array, per the InvokeMember examples, but I
am unable to assign the ptr to an element of the object array, specifically:

object [] myData = new object [1];
myData[0] = ptr;

failes with the same Cannot convert message.

Suggestions?

Pat, MCSD/VB.NET/C#



Nicholas Paldino said:
Pat,

The only way to do this is through unsafe code. The SByte* parameter
can only be used in unsafe code. In order to do this, you would want to do
this:

// Need unsafe code. Assume your array is stored in a variable named array.
unsafe
{
// Get the pointer. This is not boxing, it's unsafe code. The pointer
has to be
// fixed so it doesn't jump around.
fixed (SByte* arrayPointer = array)
{
// Make the call, passing arrayPointer for the SByte* parameter
here.
}
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Pat Ireland said:
I am attempting to pass an SByte [] to another .NET class that expects the
data to be passed as SByte *. I searched around and found the boxing can
be
used to produce a pointer but I'm not sure if this is appropriate. I am
using
the following sequence:

// Load class
Assembly c = Assembl.LoadFrom("filename ....);

// Get the types
cTypes = c.GetTypes();

// Get the appropriate type entry
myType=cTypes[1];

// Get the infos
pInfos = myType.GetMethods();

// Get the appropriate info entry
myInfo=pInfos[0];

// Create a class instance
Object myClass = myType.InvokeMember(null, BindingFlags.CreateInstance ...

// Data to pass
SByte [] myData = new SByte[3] {1,2,3};

// Invoke method
myType.InvokeMember(myInfo.Name,
BindingFlags.InvokeMember ...
null,
myClass,
myData);

However this signature does work nor does

Object [] parms = new Object [1];
parms[0] = myData;
myType.InvokeMember(.....,parms);

Doing:

ParameterInfo[] parmInfo = myInfo.GetParameters [];

shows that parmInfo[0].ParameterType is SByte*

Now how do I get from my SByte [] is SByte *
 
C

Christof Nordiek

Hi Pat,

i didn't test it, but should work.
myData[0] = (IntPtr)ptr;

Hope that helps.

Pat Ireland said:
Hi Nicholas,

It has been a long time since we last conversed.

I was able to use the unsafe & fixed as you specified, however,
I have a problem with the last argument on the InvokeMember for the class.

This arguments represents a list of object arguments to be passed in the
call to the class method.

I am unable to expicitly use the ptr as the last agrument in tha call
because that gives an argument mismatch on the InvokeMember.

I tried to cast the ptr as an object but get the message:
Cannot convert type 'sbyte*' to 'object'.

I also tried to create an object array, per the InvokeMember examples, but
I
am unable to assign the ptr to an element of the object array,
specifically:

object [] myData = new object [1];
myData[0] = ptr;

failes with the same Cannot convert message.

Suggestions?

Pat, MCSD/VB.NET/C#



Nicholas Paldino said:
Pat,

The only way to do this is through unsafe code. The SByte* parameter
can only be used in unsafe code. In order to do this, you would want to
do
this:

// Need unsafe code. Assume your array is stored in a variable named
array.
unsafe
{
// Get the pointer. This is not boxing, it's unsafe code. The
pointer
has to be
// fixed so it doesn't jump around.
fixed (SByte* arrayPointer = array)
{
// Make the call, passing arrayPointer for the SByte* parameter
here.
}
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Pat Ireland said:
I am attempting to pass an SByte [] to another .NET class that expects
the
data to be passed as SByte *. I searched around and found the boxing
can
be
used to produce a pointer but I'm not sure if this is appropriate. I am
using
the following sequence:

// Load class
Assembly c = Assembl.LoadFrom("filename ....);

// Get the types
cTypes = c.GetTypes();

// Get the appropriate type entry
myType=cTypes[1];

// Get the infos
pInfos = myType.GetMethods();

// Get the appropriate info entry
myInfo=pInfos[0];

// Create a class instance
Object myClass = myType.InvokeMember(null, BindingFlags.CreateInstance
...

// Data to pass
SByte [] myData = new SByte[3] {1,2,3};

// Invoke method
myType.InvokeMember(myInfo.Name,
BindingFlags.InvokeMember ...
null,
myClass,
myData);

However this signature does work nor does

Object [] parms = new Object [1];
parms[0] = myData;
myType.InvokeMember(.....,parms);

Doing:

ParameterInfo[] parmInfo = myInfo.GetParameters [];

shows that parmInfo[0].ParameterType is SByte*

Now how do I get from my SByte [] is SByte *
 
M

Mattias Sjögren

Pat,

I also tried to create an object array, per the InvokeMember examples, but I
am unable to assign the ptr to an element of the object array, specifically:

object [] myData = new object [1];
myData[0] = ptr;

failes with the same Cannot convert message.

Suggestions?


Use System.Reflection.Pointer.Box()



Mattias
 
G

Guest

Christof,

Yes the assignment works, but it still does not accomplish what is required
8-(.

The parameter to the method I am calling requires a SByte *
The InvokeMethod argument that is forwarded to the class is an object []
Based on the documentation of the InvokeMethod this means the the 0th
entry in the object[] must be of type SByte *.

You can not case a SByte * to an object expicitly.

How can i get the SByte * type in to the object array?

Nicolas gave me a method for constructing a SByte * pointer but I can not
load that into the object[].

The Cannot convert message occurs.

AfterI cast the SByte * to an IntPtr as you suggest, I then load this into
the object[0] and then get its type, it is magically (well not really) is
known as a IntPrt type.

using System;
namespace TestUnsafePtr
{
class TestUnsafePtr
{
[STAThread]
static void Main(string[] args)
{
SByte [] myData = new SByte[2] {(sbyte) 'A', (sbyte) 'B'};
unsafe
{
fixed(SByte * ptrParm = myData)
{
Console.WriteLine("myData type = {0}",myData.GetType());
object [] myArgs = new object[1];
myArgs[0] = (IntPtr) ptrParm;
Console.WriteLine("myArgs[0] type = {0}",myArgs[0].GetType());
} // fixed(SByte * PtrmyData = myData)
} // unsafe
} // static void Main ()
} // class TestUnsafePtr
} // namespace TestUnsafePtr


Christof Nordiek said:
Hi Pat,

i didn't test it, but should work.
myData[0] = (IntPtr)ptr;

Hope that helps.

Pat Ireland said:
Hi Nicholas,

It has been a long time since we last conversed.

I was able to use the unsafe & fixed as you specified, however,
I have a problem with the last argument on the InvokeMember for the class.

This arguments represents a list of object arguments to be passed in the
call to the class method.

I am unable to expicitly use the ptr as the last agrument in tha call
because that gives an argument mismatch on the InvokeMember.

I tried to cast the ptr as an object but get the message:
Cannot convert type 'sbyte*' to 'object'.

I also tried to create an object array, per the InvokeMember examples, but
I
am unable to assign the ptr to an element of the object array,
specifically:

object [] myData = new object [1];
myData[0] = ptr;

failes with the same Cannot convert message.

Suggestions?

Pat, MCSD/VB.NET/C#



Nicholas Paldino said:
Pat,

The only way to do this is through unsafe code. The SByte* parameter
can only be used in unsafe code. In order to do this, you would want to
do
this:

// Need unsafe code. Assume your array is stored in a variable named
array.
unsafe
{
// Get the pointer. This is not boxing, it's unsafe code. The
pointer
has to be
// fixed so it doesn't jump around.
fixed (SByte* arrayPointer = array)
{
// Make the call, passing arrayPointer for the SByte* parameter
here.
}
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am attempting to pass an SByte [] to another .NET class that expects
the
data to be passed as SByte *. I searched around and found the boxing
can
be
used to produce a pointer but I'm not sure if this is appropriate. I am
using
the following sequence:

// Load class
Assembly c = Assembl.LoadFrom("filename ....);

// Get the types
cTypes = c.GetTypes();

// Get the appropriate type entry
myType=cTypes[1];

// Get the infos
pInfos = myType.GetMethods();

// Get the appropriate info entry
myInfo=pInfos[0];

// Create a class instance
Object myClass = myType.InvokeMember(null, BindingFlags.CreateInstance
...

// Data to pass
SByte [] myData = new SByte[3] {1,2,3};

// Invoke method
myType.InvokeMember(myInfo.Name,
BindingFlags.InvokeMember ...
null,
myClass,
myData);

However this signature does work nor does

Object [] parms = new Object [1];
parms[0] = myData;
myType.InvokeMember(.....,parms);

Doing:

ParameterInfo[] parmInfo = myInfo.GetParameters [];

shows that parmInfo[0].ParameterType is SByte*

Now how do I get from my SByte [] is SByte *
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top