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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A. Gharbeia

    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



  • Mattias Sjögren

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

    [color=blue]
    >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,[/color]

    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.

    Comment

    • A. Gharbeia

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

      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" <mattias.dont.w ant.spam@mvps.o rg> wrote in message news:%23KCvYBIL EHA.1120@TK2MSF TNGP11.phx.gbl. ..[color=blue]
      > [color=green]
      > >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,[/color]
      >
      > 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.[/color]

      Comment

      • A. Gharbeia

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

        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" <ahmad@heia.org > wrote in message
        news:uCAnEiILEH A.2716@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" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
        news:%23KCvYBIL EHA.1120@TK2MSF TNGP11.phx.gbl. ..[color=blue]
        >[color=green]
        > >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,[/color]
        >
        > 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.[/color]


        Comment

        • Mattias Sjögren

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

          [color=blue]
          >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[*].[/color]

          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.

          Comment

          • A. Gharbeia

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

            Yes.
            But this will mean I will have to access the Array elements one by one!
            Then what?

            "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
            news:Oc$RizILEH A.3324@TK2MSFTN GP10.phx.gbl...[color=blue]
            >[color=green]
            > >Using .GetType().ToSt ring() directly on the reference returned by[/color][/color]
            System.Type.Inv okeMember() (i.e. without first assigning it to an object),
            returns System.Byte[*].[color=blue]
            >
            > 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.[/color]


            Comment

            • A. Gharbeia

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

              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" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
              news:Oc$RizILEH A.3324@TK2MSFTN GP10.phx.gbl...[color=blue]
              >[color=green]
              > >Using .GetType().ToSt ring() directly on the reference returned by[/color][/color]
              System.Type.Inv okeMember() (i.e. without first assigning it to an object),
              returns System.Byte[*].[color=blue]
              >
              > 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.[/color]


              Comment

              • Mattias Sjögren

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

                [color=blue]
                >Yes.
                >But this will mean I will have to access the Array elements one by one!
                >Then what?[/color]

                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.

                Comment

                • A. Gharbeia

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

                  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" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                  news:eMmxhVJLEH A.1644@TK2MSFTN GP09.phx.gbl...[color=blue]
                  >[color=green]
                  > >Yes.
                  > >But this will mean I will have to access the Array elements one by one!
                  > >Then what?[/color]
                  >
                  > 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.[/color]


                  Comment

                  • A. Gharbeia

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

                    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" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                    news:eMmxhVJLEH A.1644@TK2MSFTN GP09.phx.gbl...[color=blue]
                    >[color=green]
                    > >Yes.
                    > >But this will mean I will have to access the Array elements one by one!
                    > >Then what?[/color]
                    >
                    > 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.[/color]


                    Comment

                    • Mattias Sjögren

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

                      [color=blue]
                      >CS0154: The property or indexer 'System.Web.UI. Page.Buffer' cannot be used
                      >in this context because it lacks the get accessor[/color]

                      The code looks correct, you just got the wrong Buffer class. Make it

                      System.Buffer.B lockCopy( ...



                      Mattias

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

                      Comment

                      • A. Gharbeia

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

                        that same code from the past post with System.Buffer results in:

                        CS0165: Use of unassigned local variable 'b'

                        From what I understand, arrays in C# aren't dynamic and have to be
                        dimensioned and initialised before use.


                        "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                        news:ecfcYQKLEH A.2388@TK2MSFTN GP09.phx.gbl...[color=blue]
                        >[color=green]
                        > >CS0154: The property or indexer 'System.Web.UI. Page.Buffer' cannot be[/color][/color]
                        used[color=blue][color=green]
                        > >in this context because it lacks the get accessor[/color]
                        >
                        > The code looks correct, you just got the wrong Buffer class. Make it
                        >
                        > System.Buffer.B lockCopy( ...
                        >
                        >
                        >
                        > Mattias
                        >
                        > --
                        > Mattias Sjögren [MVP] mattias @ mvps.org
                        > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                        > Please reply only to the newsgroup.[/color]



                        Comment

                        • Mattias Sjögren

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

                          [color=blue]
                          >that same code from the past post with System.Buffer results in:
                          >
                          >CS0165: Use of unassigned local variable 'b'[/color]

                          Well you have to instantiate the byte array first.

                          byte[] b = new byte[((Array)r).Leng th];



                          Mattias

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

                          Comment

                          • A. Gharbeia

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

                            Thank you very much Mattias.
                            That did it.
                            I thought I tried that before, but it seems I did something wrong.

                            I hope you don't mind me asking another question: Is the reflection part
                            required in ASP.NET C#, or is there a way to add a reference to the COM
                            object so I can do away with the extra code.


                            Ahmad Gharbeia


                            Comment

                            • Carter

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

                              Question for A. Gharbeia: do you always know what sort of object you are getting back from your InvokeMember call
                              For instance, why not cast your original example to an Image instance

                              Image tImage = (Image) TImagingObject. InvokeMember("J PGData", BindingFlags.Ge tProperty, null, certificateImag e, null)

                              2 of the overloaded Image.Save() methods allow you to perist the image as a Stream

                              Stream tImageByteStrea m = new Stream()
                              tImage.Save(tIm ageByteStream, ImagingFormat.J peg)

                              and so on and so forth..

                              Comment

                              Working...