473,814 Members | 3,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object to binary[] from COM in .NET using C#

Greetings:
It turned out that using COM objects from .NET C# isn't straight forward at
all.
Now, I have successfully used reflection to instantiate the object, and
invoked the methods and properties through Type.InvokeMemb er, but...

One property whose value I retrieve through :

TImagingObject. InvokeMember("J PGData", BindingFlags.Ge tProperty, null,
certificateImag e, null);

returns an Object.

Now how can I obtain a byte[] out of this returned Object to pass it to
Response.Write? !
I can't cast into an array, of course, and I can't find the right methods to
access the binary data and instantiate an array with it.

Sincerely,
A. Gharbeia

Nov 16 '05 #1
15 4405
Now how can I obtain a byte[] out of this returned Object to pass it to
Response.Write ?!
I can't cast into an array, of course,


Why not? What's the actual type of the returned object
(obj.GetType(). ToString())?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Thank you for your response, Mattias.

Using .GetType().ToSt ring() directly on the reference returned by System.Type.Inv okeMember() (i.e. without first assigning it to an object), returns System.Byte[*].

However, passing this reference also directly to "Response.Binar yWrite()" (i.e. by placing the whole InvokeMember call within the parentheses), yields:

Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.Htt pResponse.Binar yWrite(byte[])' has some invalid arguments

I also tried assigning the reference to an Object object and using System.Convert. ChangeType(obje ct, invocation.GetT ype()) to convert object to the type returned by the invocation and do InvokeMember() once more, this time assigning the returned reference to object. And I got the same error!!
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Now how can I obtain a byte[] out of this returned Object to pass it to
Response.Write ?!
I can't cast into an array, of course,


Why not? What's the actual type of the returned object
(obj.GetType(). ToString())?



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3
One more thing:
Doing the whole thing in JScript.NET is much easier and straighforward,
since calling the COM methods doesn't neccessate using Type.InvokeMemb er.
However, attempting to output the result to the HTTP stream using
Response.Binary Write yields a similar error:

System.InvalidC astException: Specified cast is not valid.

A. Gharbeia

"A. Gharbeia" <ah***@heia.org > wrote in message
news:uC******** ******@tk2msftn gp13.phx.gbl...
Thank you for your response, Mattias.

Using .GetType().ToSt ring() directly on the reference returned by
System.Type.Inv okeMember() (i.e. without first assigning it to an object),
returns System.Byte[*].

However, passing this reference also directly to "Response.Binar yWrite()"
(i.e. by placing the whole InvokeMember call within the parentheses),
yields:

Compiler Error Message: CS1502: The best overloaded method match for
'System.Web.Htt pResponse.Binar yWrite(byte[])' has some invalid arguments

I also tried assigning the reference to an Object object and using
System.Convert. ChangeType(obje ct, invocation.GetT ype()) to convert object to
the type returned by the invocation and do InvokeMember() once more, this
time assigning the returned reference to object. And I got the same error!!
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Now how can I obtain a byte[] out of this returned Object to pass it to
Response.Write ?!
I can't cast into an array, of course,


Why not? What's the actual type of the returned object
(obj.GetType(). ToString())?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #4
Using .GetType().ToSt ring() directly on the reference returned by System.Type.Inv okeMember() (i.e. without first assigning it to an object), returns System.Byte[*].


In that case you should be able to cast it to System.Array and access
the array items with Array.GetValue( ).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5
Yes.
But this will mean I will have to access the Array elements one by one!
Then what?

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:Oc******** ******@TK2MSFTN GP10.phx.gbl...
Using .GetType().ToSt ring() directly on the reference returned by
System.Type.Inv okeMember() (i.e. without first assigning it to an object),
returns System.Byte[*].
In that case you should be able to cast it to System.Array and access
the array items with Array.GetValue( ).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #6
I tried:

Object o = ImagingComponen t.InvokeMember( "JPGData",
BindingFlags.Ge tProperty, null, certificateImag e, null);
Array b = Array.CreateIns tance(typeof(by te), ((Array)o).Long Length);
b.CopyTo(b, 0);
Response.Binary Write(b);

and got the same compiler error:

CS1502: The best overloaded method match for
'System.Web.Htt pResponse.Binar yWrite(byte[])' has some invalid arguments
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:Oc******** ******@TK2MSFTN GP10.phx.gbl...
Using .GetType().ToSt ring() directly on the reference returned by
System.Type.Inv okeMember() (i.e. without first assigning it to an object),
returns System.Byte[*].
In that case you should be able to cast it to System.Array and access
the array items with Array.GetValue( ).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #7
Yes.
But this will mean I will have to access the Array elements one by one!
Then what?


Not necessarily, you should be able to copy the contents over to a
regular byte[] using Buffer.BlockCop y().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #8
Although I don't know how to create a byte[] with a dynamic size (that will
only be known at runtime), I tried:

Object r = ImaginCOM.Invok eMember("JPGDat a", BindingFlags.Ge tProperty,
null, certificateImag e, null);
byte[] b;
Buffer.BlockCop y( ((Array)r), 0, b, 0, ((Array)r).Leng th );

and got:

CS0154: The property or indexer 'System.Web.UI. Page.Buffer' cannot be used
in this context because it lacks the get accessor

I'm trying this and other tricks with JScript.NET in addition to C#. I with
I could find a solution in JScript.NET, as the code there is much cleaner. I
tried to play on the JScript Array/.NET System.Array duality but reached no
where either.

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:eM******** ******@TK2MSFTN GP09.phx.gbl...
Yes.
But this will mean I will have to access the Array elements one by one!
Then what?


Not necessarily, you should be able to copy the contents over to a
regular byte[] using Buffer.BlockCop y().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #9
Although I don't know how to create a byte[] with a dynamic size (that will
only be known at runtime), I tried:

Object r = ImaginCOM.Invok eMember("JPGDat a", BindingFlags.Ge tProperty,
null, certificateImag e, null);
byte[] b;
Buffer.BlockCop y( ((Array)r), 0, b, 0, ((Array)r).Leng th );

and got:

CS0154: The property or indexer 'System.Web.UI. Page.Buffer' cannot be used
in this context because it lacks the get accessor

I'm trying this and other tricks with JScript.NET in addition to C#. I with
I could find a solution in JScript.NET, as the code there is much cleaner. I
tried to play on the JScript Array/.NET System.Array duality but reached no
where either.

What do you think?

A. Gharbeia

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:eM******** ******@TK2MSFTN GP09.phx.gbl...
Yes.
But this will mean I will have to access the Array elements one by one!
Then what?


Not necessarily, you should be able to copy the contents over to a
regular byte[] using Buffer.BlockCop y().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #10

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

Similar topics

5
10146
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got this while experimenting, trying to figure out the file objects modes,
1
3978
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of size n. There are n!/(s!(n-s)!) ways to do this. Donald E. Knuth gives several methods (algorithms) to generate all the s-combinations in . In such procedure-oriented way, each s-combination is processed while it's being generated. In some
5
7591
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any assistance gratefully received. Best regards,
5
5682
by: Samuel R. Neff | last post by:
Are there any tools available to view a file containing a binary serialized object in a friendly format? Something to list classes and data and such? Thanks, Sam B-Line is now hiring one Washington D.C. area VB.NET developer for WinForms + WebServices position.
7
9561
by: schoenfeld1 | last post by:
I've implemented IPC between two applications using named pipes and binary serialization, but have noticed that the binary formatter is rather slow. It seems that the binary formatter reflects the entire type everytime it is invoked to serialize/deserialize an object of that type. Is there a way to prepare the binary formatter with a pre-defined type, such that it only reflects once but can be re-used to serialize/deserialize objects...
7
11964
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return "DirectoryEntry" objects within the path that I specify (either using the DirectoryEnrtry.Children or using the DirectorySearcher class) and all started well and dandy! Now the problem; some of the properties of the DirectoryEntry objects being...
3
17209
by: nigel.thomson | last post by:
Hello All Is there an easy way to do this? I have a database that contains records witha image as one of the fields, what I want to do is export the images to a seperate folder, in whatever foremat is suitable (gif, jpeg etc) But i can't work out how to do this This is all the information i have
8
8934
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using GetChunk and AppendChunk to read and write binary data from the OLE Object field. I am using VBA and DAO for this experiment. The OLE Object field is being used to store Long Binary data.
2
1684
by: mpreisdorf | last post by:
I want to save the raw data of a class (without the .NET object overhead) to a binary file. For example: ref class Test { public: String^ name; Int32 number; ..... }
0
4109
by: phoenix7 | last post by:
Dear all, I want to store some data in form of a zip file into an access database. I created a table with with a column of type OLE Object, then I designed a form to insert data to the table. I inserted some files to it, but when I tried to read them using my java client it retrieves an OLE object but I just need the file content (what the program gets form the db was the original file added with some extra binary information at the...
0
9734
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10408
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10426
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10141
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7686
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.