Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

IntPtr to byte[] array

Posted on 2004-04-20
6
Medium Priority
?
13,438 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

609 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