How to get address of array parameter?

  • Thread starter Thread starter Richard L Rosenheim
  • Start date Start date
R

Richard L Rosenheim

I'm trying to use a callback procedure from a COM library. The declaration
is Callback(ByRef pData as System.Array, ...).

pData contains the memory representation of a bitmap. I'm trying to create
a bitmap object by passing pData to the bitmap's constructor, which accepts
the data as IntPtr. How do I go about getting a pointer to pData?

I'm using VS 2005 Beta 2.

TIA,

Richard
 
You should allocate an hGlobal, copy the bitmap into that and then pass the
IntPtr to the COM object for the global. See the methods in the Interop
namespace for more information.
 
Thanks for replying, but offhand, that seems to be a rather inefficient
approach. Especially since it also means having to copy the bitmap image
back to the original variable.

Are you sure there isn't a better way?

Richard Rosenheim
 
In doing some more research, I think the approach is to use
System.Runtime.Interopservices.GCHandle.Alloc. But, I haven't been able to
get it to work.

Here's what I have so far:

Private Sub Callback(ByRef pData as System.Array, ByVal lWidth As
Integer, ByVal lHeight As Integer, _
ByVal lBitCount As Integer, ByVal
lSize As Integer, ByVal bTopDown As Integer)

Dim gch As System.Runtime.InteropServicesGCHandle
Dim b As Bitmap

Try
gch = GCHandle.Alloc(pData(0))
b = New Bitmap(lWidth, lHeight, (lWidth * lBitCount) / 8, f,
GCHandle.op_Explicit(gch))
b.Save("c:\test.bmp", ImageFormat.Bmp)
gch.Free()
Catch ex As Exception
...

The b.Save is generating the exception "Run-time exception thrown :
System.Runtime.InteropServices.ExternalException - A generic error occurred
in GDI+.

I also tried calling gch.Free() before doing the save, and received the same
results. Anyone have any suggestions?

Richard Rosenheim
 
Back
Top