473,408 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Get IntPtr for a byte array

I have a third party Dll with a function which requires an IntPtr but the
data is in a byte array. I need to get the IntPtr for this byte array so I
can pass it into the function.

Thanks in advance,
~Logan
Nov 15 '05 #1
2 29427
Logan,
I found the answer,
use GCHandle, code sample follows
----------------------------------------------------------------------------
-------------
//b is the byte array
//ToSend is an IntPtr that is set to the address of b
GCHandle gch = GCHandle.Alloc(b,GCHandleType.Pinned);
ToSend = gch.AddrOfPinnedObject();
----------------------------------------------------------------------------
-------------


Just don't forget to Free the GCHandle when you're done with it.

Another possible solution is to change the DLL function declaration to
take a byte array parameter instead of an IntPtr.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
try the fixed statement

fixed (byte *p=ByteArray) {
IntPtr MyIntPtr= (IntPtr)p;
SomeDllMethod(MyIntPtr);
}

don't forget to use /unsafe when compiling

Oscar

"Logan McKinley" <lo***@globalweb.net> wrote in message
news:uc*************@tk2msftngp13.phx.gbl...
I have a third party Dll with a function which requires an IntPtr but the
data is in a byte array. I need to get the IntPtr for this byte array so I can pass it into the function.

Thanks in advance,
~Logan

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
1
by: Jon Finch | last post by:
Hi All this is a follow up to a post I did a few days back regarding reading a font from an assembly into a memory font object. The only thing I am stuck on is getting the actual memory size of a...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
2
by: bill tie | last post by:
How do I convert ADODB.Stream into an array of bytes? Thank you.
3
by: Beorne | last post by:
I have to pass a byte array as an input parameter to a function in a propertary dll. The c++ signature of the original function is the following: ---------- C++ ---------- int...
1
by: swts | last post by:
this is in reference to one of the posts i found in the .NET forum i need to send structure across thru the socket.... static byte StructureToByteArray(object obj) > > { > > int len =...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.