473,776 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

covernting SByte [] to SByte *

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.LoadFro m("filename ....);

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

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

// Get the infos
pInfos = myType.GetMetho ds();

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

// Create a class instance
Object myClass = myType.InvokeMe mber(null, BindingFlags.Cr eateInstance ...

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

// Invoke method
myType.InvokeMe mber(myInfo.Nam e,
BindingFlags.In vokeMember ...
null,
myClass,
myData);

However this signature does work nor does

Object [] parms = new Object [1];
parms[0] = myData;
myType.InvokeMe mber(.....,parm s);

Doing:

ParameterInfo[] parmInfo = myInfo.GetParam eters [];

shows that parmInfo[0].ParameterType is SByte*

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

Nov 17 '05 #1
5 8515
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]
- mv*@spam.guard. caspershouse.co m

"Pat Ireland" <ir*******@eart hlink.net> wrote in message
news:A7******** *************** ***********@mic rosoft.com...
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.LoadFro m("filename ....);

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

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

// Get the infos
pInfos = myType.GetMetho ds();

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

// Create a class instance
Object myClass = myType.InvokeMe mber(null, BindingFlags.Cr eateInstance ...

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

// Invoke method
myType.InvokeMe mber(myInfo.Nam e,
BindingFlags.In vokeMember ...
null,
myClass,
myData);

However this signature does work nor does

Object [] parms = new Object [1];
parms[0] = myData;
myType.InvokeMe mber(.....,parm s);

Doing:

ParameterInfo[] parmInfo = myInfo.GetParam eters [];

shows that parmInfo[0].ParameterType is SByte*

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

Nov 17 '05 #2
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]
- mv*@spam.guard. caspershouse.co m

"Pat Ireland" <ir*******@eart hlink.net> wrote in message
news:A7******** *************** ***********@mic rosoft.com...
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.LoadFro m("filename ....);

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

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

// Get the infos
pInfos = myType.GetMetho ds();

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

// Create a class instance
Object myClass = myType.InvokeMe mber(null, BindingFlags.Cr eateInstance ...

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

// Invoke method
myType.InvokeMe mber(myInfo.Nam e,
BindingFlags.In vokeMember ...
null,
myClass,
myData);

However this signature does work nor does

Object [] parms = new Object [1];
parms[0] = myData;
myType.InvokeMe mber(.....,parm s);

Doing:

ParameterInfo[] parmInfo = myInfo.GetParam eters [];

shows that parmInfo[0].ParameterType is SByte*

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


Nov 17 '05 #3
Hi Pat,

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

Hope that helps.

"Pat Ireland" <ir*******@eart hlink.net> schrieb im Newsbeitrag
news:47******** *************** ***********@mic rosoft.com...
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]
- mv*@spam.guard. caspershouse.co m

"Pat Ireland" <ir*******@eart hlink.net> wrote in message
news:A7******** *************** ***********@mic rosoft.com...
>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.LoadFro m("filename ....);
>
> // Get the types
> cTypes = c.GetTypes();
>
> // Get the appropriate type entry
> myType=cTypes[1];
>
> // Get the infos
> pInfos = myType.GetMetho ds();
>
> // Get the appropriate info entry
> myInfo=pInfos[0];
>
> // Create a class instance
> Object myClass = myType.InvokeMe mber(null, BindingFlags.Cr eateInstance
> ...
>
> // Data to pass
> SByte [] myData = new SByte[3] {1,2,3};
>
> // Invoke method
> myType.InvokeMe mber(myInfo.Nam e,
> BindingFlags.In vokeMember ...
> null,
> myClass,
> myData);
>
> However this signature does work nor does
>
> Object [] parms = new Object [1];
> parms[0] = myData;
> myType.InvokeMe mber(.....,parm s);
>
> Doing:
>
> ParameterInfo[] parmInfo = myInfo.GetParam eters [];
>
> shows that parmInfo[0].ParameterType is SByte*
>
> Now how do I get from my SByte [] is SByte *
>
>
>


Nov 17 '05 #4
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.Reflecti on.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.
Nov 17 '05 #5
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.WriteLi ne("myData type = {0}",myData.Get Type());
object [] myArgs = new object[1];
myArgs[0] = (IntPtr) ptrParm;
Console.WriteLi ne("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" <ir*******@eart hlink.net> schrieb im Newsbeitrag
news:47******** *************** ***********@mic rosoft.com...
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]
- mv*@spam.guard. caspershouse.co m

"Pat Ireland" <ir*******@eart hlink.net> wrote in message
news:A7******** *************** ***********@mic rosoft.com...
>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.LoadFro m("filename ....);
>
> // Get the types
> cTypes = c.GetTypes();
>
> // Get the appropriate type entry
> myType=cTypes[1];
>
> // Get the infos
> pInfos = myType.GetMetho ds();
>
> // Get the appropriate info entry
> myInfo=pInfos[0];
>
> // Create a class instance
> Object myClass = myType.InvokeMe mber(null, BindingFlags.Cr eateInstance
> ...
>
> // Data to pass
> SByte [] myData = new SByte[3] {1,2,3};
>
> // Invoke method
> myType.InvokeMe mber(myInfo.Nam e,
> BindingFlags.In vokeMember ...
> null,
> myClass,
> myData);
>
> However this signature does work nor does
>
> Object [] parms = new Object [1];
> parms[0] = myData;
> myType.InvokeMe mber(.....,parm s);
>
> Doing:
>
> ParameterInfo[] parmInfo = myInfo.GetParam eters [];
>
> shows that parmInfo[0].ParameterType is SByte*
>
> Now how do I get from my SByte [] is SByte *
>
>
>


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3715
by: Nick | last post by:
Hi, Problem statement: Calling a java method from Dot Net code that returns a java byte array. Overview: I have a java server from which I need to establish connection. I hva ebeen able to send across data to the server in a serialised stream. However, when I need to geta response back to the client (.Net), I get a binary stream. What I wanna do is to convert this stream into an SByte array (The java byte is actually a signed byte,...
3
1312
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example below... --
3
2201
by: Steve - DND | last post by:
After downloading and using two different memory profiling applications today, it seems that sbyte, int16, and int32 are all stored using 12b of memory. Someone please tell me I'm reading these memory profilers wrong! If I'm really reading it correctly, then what's the point of using anything other than a 32 bit integer? If they're all going to be stored the same way, then I can't achieve any memory savings(which I was really hoping for) by...
2
24890
by: Ray Ackley | last post by:
I need to convert a byte to an sbyte - and it has to preserve the original binary meaning of the 8 bits. For example - a 1111 1111, which is interpreted as byte to be 255, should show up in the sbyte as -1. The BitConverter class only takes 2 byte arrays or larger, and the Convert.ToSByte simply throws an overflow error. Any ideas? Thanks, Ray Ackley
1
12546
by: Darrel | last post by:
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster method for converting an array of sbytes to an array of bytes? Thanks, Darrel
6
1848
by: garyusenet | last post by:
Hello i'm just starting out with csharp. and am trying to figure out why an sbyte can take any value from '-128' to 128. i've drawn a BIT box with eight bytes, and I'm assuming the eighth bit is used to SIGN the number, which leaves seven bits. when all 7 bits are on you get : 127 so i was assuming the most you can get negative would be 127, and the
3
6407
by: reju | last post by:
I have to convert an sByte array to byte array. This sbyte array contains some signed values due to this .net converting functions will throw an exception.any work around for this? is there any way to pass sbyte array to stream classes eg: memory stream?
0
1395
by: Ben Voigt [C++ MVP] | last post by:
Chip Gore wrote: Actually, because you are using the C++/CLI compiler, microsoft.public.dotnet.languages.vc is the most appropriate place to discuss this. To be useful from C# and other .NET languages, the member functions of your ref class should accept parameters of type System::String^. The C++/CLI compiler provides some pretty simple methods for getting the data as native C++ characters if you need to, for example the...
2
1379
by: K Viltersten | last post by:
I try the following. sbyte HalvOfFour () { sbyte b = 4; return b / 2; } Apparently, since 2 is regarded as int, the operation upgrades my b to and int and after it has been performed, i stand there with an int, which is wrong
0
10120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8952
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5367
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.