PC Review


Reply
Thread Tools Rate Thread

covernting SByte [] to SByte *

 
 
=?Utf-8?B?UGF0IElyZWxhbmQ=?=
Guest
Posts: n/a
 
      19th Sep 2005
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 *



 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      19th Sep 2005
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 Removed)

"Pat Ireland" <(E-Mail Removed)> wrote in message
news:A760D0EB-621E-4501-8A89-(E-Mail 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 *
>
>
>



 
Reply With Quote
 
=?Utf-8?B?UGF0IElyZWxhbmQ=?=
Guest
Posts: n/a
 
      19th Sep 2005
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 [.NET/C# MVP]" wrote:

> 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 Removed)
>
> "Pat Ireland" <(E-Mail Removed)> wrote in message
> news:A760D0EB-621E-4501-8A89-(E-Mail 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 *
> >
> >
> >

>
>
>

 
Reply With Quote
 
Christof Nordiek
Guest
Posts: n/a
 
      19th Sep 2005
Hi Pat,

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

Hope that helps.

"Pat Ireland" <(E-Mail Removed)> schrieb im Newsbeitrag
news:47AC4C49-9353-4FCD-A1E0-(E-Mail Removed)...
> 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 [.NET/C# MVP]" wrote:
>
>> 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 Removed)
>>
>> "Pat Ireland" <(E-Mail Removed)> wrote in message
>> news:A760D0EB-621E-4501-8A89-(E-Mail 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 *
>> >
>> >
>> >

>>
>>
>>



 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      19th Sep 2005
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
=?Utf-8?B?UGF0IElyZWxhbmQ=?=
Guest
Posts: n/a
 
      19th Sep 2005
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" wrote:

> Hi Pat,
>
> i didn't test it, but should work.
> myData[0] = (IntPtr)ptr;
>
> Hope that helps.
>
> "Pat Ireland" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:47AC4C49-9353-4FCD-A1E0-(E-Mail Removed)...
> > 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 [.NET/C# MVP]" wrote:
> >
> >> 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 Removed)
> >>
> >> "Pat Ireland" <(E-Mail Removed)> wrote in message
> >> news:A760D0EB-621E-4501-8A89-(E-Mail 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 *
> >> >
> >> >
> >> >
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Algorithm for sbyte? Rich Microsoft C# .NET 1 19th Jun 2008 08:18 AM
sbyte understanding needed garyusenet@myway.com Microsoft C# .NET 6 25th Jan 2006 03:20 PM
byte[] <=> sbyte[] =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= Microsoft Dot NET Framework 9 12th Oct 2005 06:41 PM
Conversion from Stream to SByte =?Utf-8?B?Tmljaw==?= Microsoft Dot NET 2 5th Jul 2005 08:00 AM
Convert sbyte to byte Microsoft Dot NET Framework 1 6th Apr 2004 12:56 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:45 AM.