SpriteHand
Module Border
  Marshaling a Byte Array to COM object
Module Border
Location: BlogsAndy's Blog    
Posted by: host 8/25/2006 7:05 AM

I have been helping someone port a Delphi app to WindowsForms and we ran into something interesting. The Delphi app uses a 3rd-party ActiveX control to display spreadsheets. Unforunately, the ActiveX control has not been ported to .NET, and the Delphi app stores the spreadsheet control data as a proprietary format BLOB in the database.

The ActiveX control had documentation for the method we needed to call:

Syntax
ReadFromBlob hBlob, nReservedBytes

Part           Type       Description
hBlob          OLE_HANDLE Reference to a BLOB variable in memory.
nReservedBytes Integer    Size of the BLOB variable. Not implemented in this version and must be 0.

So, after adding the ActiveX control to WindowsForms, how to feed it these bytes? We need to create an unmanaged buffer to hold the data and get that to the method. We can use AllocCoTaskMem of the Marshal class to allocate the unmanaged buffer and then marshal it to the ActiveX control...

// assume we have a byte array inside byteData read from DB.
int
size = byteData.Length;
IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(size) * size);
Marshal.Copy(byteData, 0, buffer, size);
int ptr = buffer.ToInt32();

// now we can feed the control 
myControl.ReadFromBlob(ptr, 0);

Marshal.FreeCoTaskMem(buffer);

Permalink |  Trackback

Comments (3)   Add Comment
Re: Marshaling a Byte Array to COM object    By Anonymous on 12/13/2006 2:49 AM
I am attempting to do the same thing. I used the example snippet you provided and received a "Invalid file." error. I know the byteData byte array contains the correct information. I've written the same byte array out to a file and read it in from the ActiveX control. If possible I like to read directly from a blob so I don't have to create files.

your help is appreciated.

Re: Marshaling a Byte Array to COM object    By Anonymous on 12/15/2006 11:43 AM
After some investigation and Andy's help. I found the reason my blobs did not load is it did not contain 4 specific bytes to the beginning. Below is a snippet of what I did to make it work. Assuming SSBytes contains a blob that is read in.

byte [] beginBytes = {76, 18, 0, 0};
byte [] byteData= new byte[SSBytes.Length + beginBytes .Length];
Array.Copy(beginBytes , 0, byteData, 0, (long)beginBytes .Length);
Array.Copy(SSBytes, 0, byteData, 4, (long)SSBytes.Length);




Re: Marshaling a Byte Array to COM object    By Anonymous on 9/1/2008 7:21 AM
Nice explanation... hats off..

asp net web development


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border
RSS   Twitter
Module Border Module Border
Module Border
  Diversions
Module Border

TALKING RAGDOLL
This Windows Phone 7 App was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine. It gets interesting when you import your friends photos and have your way with them!

MORE INFO



DROPPYPOP
This Windows Phone 7 game was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine.
DEMO

MORE INFO



BOSS LAUNCH
This physics game won first place in the Server Quest Contest. Created using Silverlight 2, the Physics Helper Library,  and the Farseer Physics Engine.
PLAY IT

MORE INFO



DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2013 andy.beaulieu.com - Login