Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. Quering a COM object in .net

Quering a COM object in .net

Scheduled Pinned Locked Moved COM
csharpdatabasehelptutorialquestion
5 Posts 3 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cable beach
    wrote on last edited by
    #1

    I support an application that is a combination of vb6 and .net components. I am trying to replace a vb6 dll with an equivalent in c# but I've got stuck on how to query the properties of a com object that is passed as Object. The vb6 form instantiates a .net object through COM and then passes a reference to itself but as an object, not a specific type:

    public bool Init(ref ADOConn.STConnector db, object MREForm, string user)

    In VB i could query any public variables on the form for example: dim loc as string = MREForm.Location If I use reflection, all I get is the properties of the object type, not the underlying COM type. How can I do the same thing in C#? Thanks for any help on this.

    _ 1 Reply Last reply
    0
    • C cable beach

      I support an application that is a combination of vb6 and .net components. I am trying to replace a vb6 dll with an equivalent in c# but I've got stuck on how to query the properties of a com object that is passed as Object. The vb6 form instantiates a .net object through COM and then passes a reference to itself but as an object, not a specific type:

      public bool Init(ref ADOConn.STConnector db, object MREForm, string user)

      In VB i could query any public variables on the form for example: dim loc as string = MREForm.Location If I use reflection, all I get is the properties of the object type, not the underlying COM type. How can I do the same thing in C#? Thanks for any help on this.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      In C#, you just use a type cast.

      IMyInterface myInt = (IMyInterface)MREForm;

      «_Superman_»
      I love work. It gives me something to do between weekends.

      Microsoft MVP (Visual C++)

      Polymorphism in C

      C 1 Reply Last reply
      0
      • _ _Superman_

        In C#, you just use a type cast.

        IMyInterface myInt = (IMyInterface)MREForm;

        «_Superman_»
        I love work. It gives me something to do between weekends.

        Microsoft MVP (Visual C++)

        Polymorphism in C

        C Offline
        C Offline
        cable beach
        wrote on last edited by
        #3

        Thanks for the answer, but in order to cast, you need to have a type to cast to. In this case, the variable being passed (as an object), is actually a vb6 form, and to get the type means that I need a reference to the vb6 dll, and the form needs to be an exported type. If I was using VB.net (and I have tried this), I can just reference properties of the object without casting, or creating a reference, presumably using late binding to query the object at runtime "under the bonnet" - this is all taken care of for the VB developer. My question is: how can I do the same thing in c#? To clarify the sample code: VB6 code: dim obj as MREPlugin32.MREPlugin ... set obj = new MREPlugin32.MREPlugin obj.Init(Me) VB.net code (this works): public Sub Init(obj as Object) dim location as string = obj.Location End Sub So how can I do this? C# Code: public Void Init(object obj) { string location = obj.Location; // Somehow, without any external references. }

        L 1 Reply Last reply
        0
        • C cable beach

          Thanks for the answer, but in order to cast, you need to have a type to cast to. In this case, the variable being passed (as an object), is actually a vb6 form, and to get the type means that I need a reference to the vb6 dll, and the form needs to be an exported type. If I was using VB.net (and I have tried this), I can just reference properties of the object without casting, or creating a reference, presumably using late binding to query the object at runtime "under the bonnet" - this is all taken care of for the VB developer. My question is: how can I do the same thing in c#? To clarify the sample code: VB6 code: dim obj as MREPlugin32.MREPlugin ... set obj = new MREPlugin32.MREPlugin obj.Init(Me) VB.net code (this works): public Sub Init(obj as Object) dim location as string = obj.Location End Sub So how can I do this? C# Code: public Void Init(object obj) { string location = obj.Location; // Somehow, without any external references. }

          L Offline
          L Offline
          Lim Bio Liong
          wrote on last edited by
          #4

          Hello cable beach, 1. >> So how can I do this? >> C# Code: >> public Void Init(object obj) >> { >> string location = obj.Location; // Somehow, without any external references. >> } 1.1 Unfortunately, this syntax style (indicating a dynamic call to the Location property of a late-bound object) is not possible in C#. 1.2 To make a late bound call to an imported COM object's method or property, you must use the static Type.InvokeMember() function. 1.3 Here are links to 2 useful short articles explaining this : Calling a COM Component From C# (Late Binding) http://www.c-sharpcorner.com/UploadFile/psingh/CallingCOMComponentFromCSharp12022005231615PM/CallingCOMComponentFromCSharp.aspx[^] Late Binding with Reflection http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx[^] 2. >> VB.net code (this works): >> public Sub Init(obj as Object) >> dim location as string = obj.Location >> End Sub 2.1 Although I do not know enough about VB.NET, I am not surprised that the above syntactic expression is possible. It is likely designed to be so since this is definitely possible in VB6.0. 2.2 I believe that the restriction in C# is probably due to strict type checking which is enforced in languages like C++. - Bio.

          C 1 Reply Last reply
          0
          • L Lim Bio Liong

            Hello cable beach, 1. >> So how can I do this? >> C# Code: >> public Void Init(object obj) >> { >> string location = obj.Location; // Somehow, without any external references. >> } 1.1 Unfortunately, this syntax style (indicating a dynamic call to the Location property of a late-bound object) is not possible in C#. 1.2 To make a late bound call to an imported COM object's method or property, you must use the static Type.InvokeMember() function. 1.3 Here are links to 2 useful short articles explaining this : Calling a COM Component From C# (Late Binding) http://www.c-sharpcorner.com/UploadFile/psingh/CallingCOMComponentFromCSharp12022005231615PM/CallingCOMComponentFromCSharp.aspx[^] Late Binding with Reflection http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx[^] 2. >> VB.net code (this works): >> public Sub Init(obj as Object) >> dim location as string = obj.Location >> End Sub 2.1 Although I do not know enough about VB.NET, I am not surprised that the above syntactic expression is possible. It is likely designed to be so since this is definitely possible in VB6.0. 2.2 I believe that the restriction in C# is probably due to strict type checking which is enforced in languages like C++. - Bio.

            C Offline
            C Offline
            cable beach
            wrote on last edited by
            #5

            Yeah, I tried using reflection, but this still requires you have a reference to the underlying type. In the end I just switched the project to VB.net, and it works. I still find it surprising that there is something vb.net can do that c# can't. Anyway, thanks for your input.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups