Solved

IntPtr to byte[] array

Posted on 2004-04-20
6
12,701 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

15 Experts available now in Live!

Get 1:1 Help Now