473,574 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mashalling IntPtr into a byte array? I can see how to do it the other way around!


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 the memory across.

Using Marshal.PtrStru cture doesn't work - it says my byte() array is not
blittable! (byte is a blittable type however). Cannot use Marshal.Copy,
because that works the other way around (for mashalling to COM, not from
it).

Any ideas?

Dim theLocked As IntPtr
Try

' Find out how big the HGLOBAL allocation returned is

Dim theAudioSize As Integer = GlobalSize(hAud io)

' an array to store the WAV

Dim theSound As System.Byte()

' Resize the array

ReDim theSound(theAud ioSize)

' Now, lock its buffer so we can load it in to our variable.

theLocked = GlobalLock(hAud io)

' Marshal "theLocked" into the array

??????????????? ???????

Catch Ex As Exception

' Whoops

Finally
Marshal.FreeHGl obal(theLocked)
End Try
Nov 20 '05 #1
5 9795
Hi,

Did you try Marshal.PtrToSt ructure?

Ken
------------------------
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:bn******** ***********@new s.demon.co.uk.. .

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 the memory across.

Using Marshal.PtrStru cture doesn't work - it says my byte() array is not
blittable! (byte is a blittable type however). Cannot use Marshal.Copy,
because that works the other way around (for mashalling to COM, not from
it).

Any ideas?

Dim theLocked As IntPtr
Try

' Find out how big the HGLOBAL allocation returned is

Dim theAudioSize As Integer = GlobalSize(hAud io)

' an array to store the WAV

Dim theSound As System.Byte()

' Resize the array

ReDim theSound(theAud ioSize)

' Now, lock its buffer so we can load it in to our variable.

theLocked = GlobalLock(hAud io)

' Marshal "theLocked" into the array

??????????????? ???????

Catch Ex As Exception

' Whoops

Finally
Marshal.FreeHGl obal(theLocked)
End Try

Nov 20 '05 #2
Yes, that was my first attempt, I wrote:

Marshal.PtrToSt ructure( myAudioPtr, theSoundByteArr ay)

Exception was: "The specified structure must be blittable or have layout
information."

I was under the impression that byte was blittable, as was the complex type
byte()?
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eU******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

Did you try Marshal.PtrToSt ructure?

Ken
------------------------
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:bn******** ***********@new s.demon.co.uk.. .

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 the memory across.

Using Marshal.PtrStru cture doesn't work - it says my byte() array is not
blittable! (byte is a blittable type however). Cannot use Marshal.Copy,
because that works the other way around (for mashalling to COM, not from
it).

Any ideas?

Dim theLocked As IntPtr
Try

' Find out how big the HGLOBAL allocation returned is

Dim theAudioSize As Integer = GlobalSize(hAud io)

' an array to store the WAV

Dim theSound As System.Byte()

' Resize the array

ReDim theSound(theAud ioSize)

' Now, lock its buffer so we can load it in to our variable.

theLocked = GlobalLock(hAud io)

' Marshal "theLocked" into the array

??????????????? ???????

Catch Ex As Exception

' Whoops

Finally
Marshal.FreeHGl obal(theLocked)
End Try


Nov 20 '05 #3
I got it:

theLocked is my locked HGLOBAL

Sound is byte(theAudioSi ze)

then,

Marshal.Copy(th eLocked, Sound, 0, theAudioSize)

"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:bn******** ***********@new s.demon.co.uk.. .
Yes, that was my first attempt, I wrote:

Marshal.PtrToSt ructure( myAudioPtr, theSoundByteArr ay)

Exception was: "The specified structure must be blittable or have layout
information."

I was under the impression that byte was blittable, as was the complex type byte()?
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eU******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

Did you try Marshal.PtrToSt ructure?

Ken
------------------------
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:bn******** ***********@new s.demon.co.uk.. .

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 the memory across.

Using Marshal.PtrStru cture doesn't work - it says my byte() array is not blittable! (byte is a blittable type however). Cannot use Marshal.Copy, because that works the other way around (for mashalling to COM, not from it).

Any ideas?

Dim theLocked As IntPtr
Try

' Find out how big the HGLOBAL allocation returned is

Dim theAudioSize As Integer = GlobalSize(hAud io)

' an array to store the WAV

Dim theSound As System.Byte()

' Resize the array

ReDim theSound(theAud ioSize)

' Now, lock its buffer so we can load it in to our variable.

theLocked = GlobalLock(hAud io)

' Marshal "theLocked" into the array

??????????????? ???????

Catch Ex As Exception

' Whoops

Finally
Marshal.FreeHGl obal(theLocked)
End Try



Nov 20 '05 #4
Hi,

I guess I should have read your message better. When I use the
netserverenum api it returns a pointer to an array. I have to read each
element of the array individually. Here is an example. I hope this helps.

Imports System.Runtime. InteropServices

Module Module1

Structure Computer_info_1 01

Public Platform_ID As Integer

<MarshalAsAttri bute(UnmanagedT ype.LPWStr)> Public Name As String

Public Version_Major As Integer

Public Version_Minor As Integer

Public Type As Integer

<MarshalAsAttri bute(UnmanagedT ype.LPWStr)> Public Comment As String

End Structure

Declare Unicode Function NetServerEnum Lib "Netapi32.d ll" _

(ByVal Servername As Integer, ByVal level As Integer, _

ByRef buffer As Integer, ByVal PrefMaxLen As Integer, _

ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, _

ByVal ServerType As Integer, ByVal DomainName As String, _

ByRef ResumeHandle As Integer) As Integer

Declare Function NetApiBufferFre e Lib "Netapi32.d ll" _

(ByVal lpBuffer As Integer) As Integer

Private Const SV_TYPE_SERVER As Integer = &H2 ' All Servers

Sub Main()

Dim ComputerInfo As Computer_info_1 01

Dim i, MaxLenPref, level, ret, EntriesRead, TotalEntries, ResumeHandle As
Integer

Dim BufPtr As Integer

Dim iPtr As IntPtr

MaxLenPref = -1

level = 101

ret = NetServerEnum(0 , level, BufPtr, MaxLenPref, EntriesRead, TotalEntries,
_

SV_TYPE_SERVER, "MSHOME", ResumeHandle) ' Replace MSHOME with your workgroup
name

If ret <> 0 Then

Console.WriteLi ne("An Error has occured")

Return

End If

' loop thru the entries

For i = 0 To EntriesRead - 1

' copy the stuff into our structure

Dim ptr As IntPtr = New IntPtr(BufPtr)

computerInfo = CType(Marshal.P trToStructure(p tr,
GetType(Compute r_info_101)), _

Computer_info_1 01)

BufPtr = BufPtr + Len(ComputerInf o)

Console.WriteLi ne(computerInfo .Name)

Next

NetApiBufferFre e(BufPtr)

Console.Write(" Press Enter to End")

Dim s As String = Console.ReadLin e()

End Sub

End Module

Ken
--------------------------------
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:bn******** ***********@new s.demon.co.uk.. .
Yes, that was my first attempt, I wrote:

Marshal.PtrToSt ructure( myAudioPtr, theSoundByteArr ay)

Exception was: "The specified structure must be blittable or have layout
information."

I was under the impression that byte was blittable, as was the complex type byte()?
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eU******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

Did you try Marshal.PtrToSt ructure?

Ken
------------------------
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:bn******** ***********@new s.demon.co.uk.. .

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 the memory across.

Using Marshal.PtrStru cture doesn't work - it says my byte() array is not blittable! (byte is a blittable type however). Cannot use Marshal.Copy, because that works the other way around (for mashalling to COM, not from it).

Any ideas?

Dim theLocked As IntPtr
Try

' Find out how big the HGLOBAL allocation returned is

Dim theAudioSize As Integer = GlobalSize(hAud io)

' an array to store the WAV

Dim theSound As System.Byte()

' Resize the array

ReDim theSound(theAud ioSize)

' Now, lock its buffer so we can load it in to our variable.

theLocked = GlobalLock(hAud io)

' Marshal "theLocked" into the array

??????????????? ???????

Catch Ex As Exception

' Whoops

Finally
Marshal.FreeHGl obal(theLocked)
End Try



Nov 20 '05 #5
Robin,
In addition to Ken's other comments.

My understanding is that Marshal.PtrToSt ructure & Mashal.Structur eToPtr are
used for COMPLEX types. COMPLEX Types as in Structures & Classes. Remember
that Byte is blittable, as its a primitive type. Primitive type as in its
native to the machine, the managed & unmanaged representation are the same
(a 32bit unmanaged int has the same physical layout as a 32bit managed int).
My understanding is you need to use Marshal.Copy to copy arrays of primitive
types.

There are overloaded Marshal.Copy functions that copy from a byte array to
an IntPtr and that copy from an IntPtr to a Byte array. Watch your source &
destination arguments and you should be fine.

Dim theLocked As IntPtr
Dim theSound As System.Byte()
Dim theSoundSize as Integer
' Copy from HGLOBAL to the byte array
Marshal.Copy(th eLocked, theSound, 0, theSoundSize)

' Copy from the byte array to HGLOBAL
Marshal.Copy(th eSound, 0, theLocked, theSoundSize)

With Marshal.Copy the source is the first parameter, followed by the
destination. (Note arrays have 2 actual parameters, the array & the starting
index).

Adam Nathan's book ".NET and COM - The Complete Interoperabilit y Guide" from
SAMS Press, provides you will every thing you ever wanted to know about
Interop and then some.

Hope this helps
Jay

"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:bn******** ***********@new s.demon.co.uk.. .
Yes, that was my first attempt, I wrote:

Marshal.PtrToSt ructure( myAudioPtr, theSoundByteArr ay)

Exception was: "The specified structure must be blittable or have layout
information."

I was under the impression that byte was blittable, as was the complex type byte()?
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eU******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

Did you try Marshal.PtrToSt ructure?

Ken
------------------------
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:bn******** ***********@new s.demon.co.uk.. .

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 the memory across.

Using Marshal.PtrStru cture doesn't work - it says my byte() array is not blittable! (byte is a blittable type however). Cannot use Marshal.Copy, because that works the other way around (for mashalling to COM, not from it).

Any ideas?

Dim theLocked As IntPtr
Try

' Find out how big the HGLOBAL allocation returned is

Dim theAudioSize As Integer = GlobalSize(hAud io)

' an array to store the WAV

Dim theSound As System.Byte()

' Resize the array

ReDim theSound(theAud ioSize)

' Now, lock its buffer so we can load it in to our variable.

theLocked = GlobalLock(hAud io)

' Marshal "theLocked" into the array

??????????????? ???????

Catch Ex As Exception

' Whoops

Finally
Marshal.FreeHGl obal(theLocked)
End Try



Nov 20 '05 #6

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

Similar topics

0
1562
by: Tien Pham via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Tien Pham I am having problems with intptr. Or maybe i am not using it properly. What it is that i am having problem with is the marshaling a structure to an ptr. For instance, i have a structure declared as followed: <StructLayout(LayoutKind.Sequential, Pack:=1)> Public...
2
29540
by: Logan McKinley | last post by:
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
1
6426
by: Chua Wen Ching | last post by:
I had a scenario here. public struct StructA { public byte avalue; public byte bvalue; public IntPtr InData; public StructA(byte a, byte b)
1
25497
by: zishen yan | last post by:
Hello, I have a question confused me a long time. I am working on DirectDraw .NET a while. And I want to copy data to surface, but I found Surface.Lock return System.Array. I tried to cast it to byte,byte, but in vain( It will crash without any exception). Before I saw somebody said he used unsafe code to slove the problem on newsgroup,...
0
1293
by: Kirk Marple | last post by:
an external API that we're using is giving me back video data in a multi-dimensional Array (not byte, just System.Array). so, for an HD quality video, the data is organized as of bytes. i know this rather weird, but it's what i have to work with... i've been reading thru Array.Copy and other related methods, but nothing seems...
2
1950
by: Nik | last post by:
Hi, I am having a problem while trying to use a buffer in a Function call. I have ported the code from the MSDN documentation and it seems that the function expects a different type of input parameter than the one MSDN is configured for. I need some help to convert it. Here is the ported block of code -- Dim buf As Int16() ...
8
5898
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How can I convert this Byte() in this IntPtr ?
4
11968
by: Rainer Queck | last post by:
Hi NG, is there a way to copy a buffer pointed to by a IntPtr directly into a two dimensional byte-array? I tried this, what obviously doesn't work: byte image = new byte; IntPtr p = <hardwareDeviceClass>.GetBufPtr();
1
15669
by: Beorne | last post by:
I have imported a corporate image handling COM object in my C# project. To access in a fast way the memory of the image there is a method that returns a pointer to the memory (in byte) of the underlying image. This method can be used to read as well to rewrite the pixel values. In the interface .idl file the method is declared as: ...
0
7814
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7826
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8107
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5307
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3748
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3756
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.