Solved

IntPtr to byte[] array

Posted on 2004-04-20
6
13,121 Views
Last Modified: 2008-02-26
How can I create a IntPtr to a byte[] array?
0
Comment
Question by:rossryan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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