Solved

IntPtr to byte[] array

Posted on 2004-04-20
6
12,924 Views
Last Modified: 2008-02-26
How can I create a IntPtr to a byte[] array?
0
Comment
Question by:rossryan
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10875176
I guess you want to pass managed byte array to unmanaged function (if I am wrong, correct me).
Create memory block of desired length using Marshal.AllocHGlobal Method:

IntPtr pUnmanaged = Marshal.AllocHGlobal(nArraySize);

Copy byte[] array to it using Marshal.Copy Method:

public static void Copy(
   byte[] source,
   int startIndex,
   IntPtr destination,
   int length
);

Marshal.Copy(byteArray, 0, pUnmanaged, nArraySize);

Now you can pass IntPtr pUnmanaged to unmanaged function. Don't forget to free pUnmanaged after this:

Marshal.FreeHGlobal(pUnmanaged);
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10876037
Something like this will convert a bytes a C# structure...

byte[] sourceData;
MyStruct destStruct;
int arrayIndex = 0;

GCHandle handle = GCHandle.Alloc(sourceData, GCHandleType.Pinned);
try
{
IntPtr buffer = handle.AddrOfPinnedObject();
buffer = (IntPtr)(buffer.ToInt32() +
(arrayIndex*Marshal.SizeOf(typeof(MyStruct))));
destStruct = (MyStruct)Marshal.PtrToStructure(buffer, typeof(MyStruct));
}
finally
{
handle.Free();
}
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 10876050
If you need it for platform interop this is done by the compiler you can use the MarshalAsAttribute to specify the type of the marshalling. What do you want to use it for?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:rossryan
ID: 10884913
I need to toss an IntPtr into a OpenGL sub, so that it can scale an image.

More precisely, I shove an IntPtr (from AlexFM) into the procedure, and does some scaling, and out pops another IntPtr.

Given that the original IntPtr pointed to a byte[] array, I think I need something comparable to receive the data.

Declaration of sub:

[C#]

[SuppressUnmanagedCodeSecurity]
public static int gluScaleImage(
   int format,
   int widthIn,
   int heightIn,
   int typeIn,
   IntPtr dataIn,
   int widthOut,
   int heightOut,
   int typeOut,
   IntPtr dataOut
);
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10885433
Well, you already know what to do with dataIn parameter.
Now you need to do something with dataOut. Allocate memory block of required length (size of scaled image):

dataOut = Marshal.AllocHGlobal(widthOut*heightOut);

and call gluScaleImage. After this read information filled by gluScaleImage to managed byte[] array using function:

public static void Copy(
   IntPtr source,
   byte[] destination,
   int startIndex,
   int length
);

byte[] scaledImage = new byte[widthOut*heightOut);
Marshal.Copy(dataOut, scaledImage , 0, widthOut*heightOut);
Marshal.FreeHGlobal(scaledImage);

For future reference: when you have question about API call, post C++ API declaration and short description. For example, for gluScaleImage this should be:
IntPtr dataIn;    [in] input image
IntPtr dataOut;  [out] output image filled by function
This gives enough infotmation to write PInvoke call.
0
 

Author Comment

by:rossryan
ID: 10885475
Description is lacking, but this is from the Tao C# libraries, so there are no C++ declaration used... :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question