 |
|
|
|
Marshaling a Byte Array to COM object
|
|
|
 |
|
Location: Blogs Andy'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
|
|
|
|
 |
|
 |
|
|