Solved

IntPtr to byte[] array

Posted on 2004-04-20
6
12,836 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Suggested Solutions

Title # Comments Views Activity
Needing an event fired when a row is added on a datagridview 4 45
Question! 4 36
Cant save 3D 4 19
Expression Evaluater 3 24
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now