Quering a COM object in .net
-
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. -
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.In C#, you just use a type cast.
IMyInterface myInt = (IMyInterface)MREForm;
«_Superman_»
I love work. It gives me something to do between weekends. -
In C#, you just use a type cast.
IMyInterface myInt = (IMyInterface)MREForm;
«_Superman_»
I love work. It gives me something to do between weekends.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. }
-
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. }
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.
-
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.