Advanced C# Question
-
Hi All, I want to be able to create an instance of a COM object dynamically, given it's GUID. Having created an instance of the COM object I would like to use reflection to show in a list control the functions that this object exports. I don't want to import the COM object at design time and create a class from it - the COM object is changing regularly, it's a test harness that's being developed concurrently by a set of users. Is this possible? If so, how do I create the COM object from the GUID only? Thanks in advance, Dave Kerr
-
Hi All, I want to be able to create an instance of a COM object dynamically, given it's GUID. Having created an instance of the COM object I would like to use reflection to show in a list control the functions that this object exports. I don't want to import the COM object at design time and create a class from it - the COM object is changing regularly, it's a test harness that's being developed concurrently by a set of users. Is this possible? If so, how do I create the COM object from the GUID only? Thanks in advance, Dave Kerr
Creating the COM object is no problem, but such objects do not support reflection. The only way to get the information you need is through the object's type library. There are a number of COM articles here on CodeProject that include suggestions as to how to proceed with this.
txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
Hi All, I want to be able to create an instance of a COM object dynamically, given it's GUID. Having created an instance of the COM object I would like to use reflection to show in a list control the functions that this object exports. I don't want to import the COM object at design time and create a class from it - the COM object is changing regularly, it's a test harness that's being developed concurrently by a set of users. Is this possible? If so, how do I create the COM object from the GUID only? Thanks in advance, Dave Kerr
You can use GetTypeFromCLSID to get a COM Type give its GUID. Then you can use
Activator.CreateInstance
to create an object instance from that Type. -
You can use GetTypeFromCLSID to get a COM Type give its GUID. Then you can use
Activator.CreateInstance
to create an object instance from that Type.Mirko1980 wrote:
Then you can use Activator.CreateInstance to create an object instance from that Type.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Mirko1980 wrote:
Then you can use Activator.CreateInstance to create an object instance from that Type.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Refer also to Richard's answer: I fear you can't get full Reflection information from a COM object.
Thanks for the info, so as I understand it, I can use Activator.CreateInstance to get the COM object, but I cannot use reflection. However, can I programmatically load the type library and get the function names from that? Again, I don't want to create a class from the type library, but dynamically retrieve the function names and parameters they take from a type library at runtime...
-
Refer also to Richard's answer: I fear you can't get full Reflection information from a COM object.
-
Thanks for the info, so as I understand it, I can use Activator.CreateInstance to get the COM object, but I cannot use reflection. However, can I programmatically load the type library and get the function names from that? Again, I don't want to create a class from the type library, but dynamically retrieve the function names and parameters they take from a type library at runtime...
DaveKerr wrote:
so as I understand it, I can use Activator.CreateInstance to get the COM object, but I cannot use reflection
You can, as long as the component supports
IDispatch
you can use the CustomMarshaller assembly (you'll commonly find it in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727). The following code sample displays the members ofDirControl.DirList.8.0
using reflection:using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.CustomMarshalers;
using System.Reflection;namespace ComMarshaller
{
[
ComImport,
Guid("00020400-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
public interface IDispatch
{
void Reserved();
[PreserveSig]
int GetTypeInfo(uint nInfo, int lcid,
[MarshalAs(
UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TypeToTypeInfoMarshaler))]
out Type typeInfo);
}class Program
{
static void Main(string[] args)
{
Type type = Type.GetTypeFromProgID("DirControl.DirList.8.0");
object o = Activator.CreateInstance(type);
IDispatch disp = o as IDispatch;
if (disp != null)
{
Type tp = null;
disp.GetTypeInfo(0, 0, out tp);
MemberInfo[] mi = tp.GetMembers();foreach (MemberInfo minfo in mi) { Console.WriteLine(minfo.Name); } } Console.WriteLine("Done"); Console.ReadKey(); }
}
}Remember, this needs CustomMarshaller.dll added as a reference in your application.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.