Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I go about instantiating a COM Object in C# by CLSID?

Forgive me if my terminology is off, as this is somewhat uncharted territory for me.

I have a program which needs to create a FolderShortcut. Microsoft has documentation on how to create it in C++, and I'm trying to translate the directions to C#. The instructions state that the CoCreateInstance function needs to be called with CLSID_FolderShortcut as a parameter, which I infer to mean that it's instantiating a COM object. The CLSID for this object is {0AFACED1-E828-11D1-9187-B532F1E9575D}.

I've tried adding a reference to Shell32.dll from the COM tab, but the FolderShortcut object does not show up in Intellisense (maybe it's not in the typelib?). I also thought about doing a DLLImport, but, of course, that only gives me access to functions, not objects.

What do I need to do to get access to this object in .Net?

like image 965
bshacklett Avatar asked Mar 14 '13 19:03

bshacklett


People also ask

How do you instantiate an object?

Instantiating a Class The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The new operator returns a reference to the object it created.

How do I instantiate an instance?

Another way to instantiate a class is by calling a static factory method. A class can provide a public static factory method that is nothing but a static method that returns an instance of the class. Always remember that it is not the same as the factory method pattern. We can use it instead of the constructor.

What is instantiation of an object?

What Does Instantiate Mean? Instantiate (a verb) and instantiation (the noun) in computer science refer to the creation of an object (or an “instance” of a given class) in an object-oriented programming (OOP) language. Referencing a class declaration, an instantiated object is named and created, in memory or on disk.


1 Answers

If you do not want to import the classes during compile time, as Simon Mourier describes it is also possible to do some late binding to the COM objects, using Activator.

If you've got the ProgID of your object, get the type using

Type comType = Type.GetTypeFromProgID("MyProg.ProgId");

otherwise you can get the type by it's CLSID:

Type comType = 
    Type.GetTypeFromCLSID(new Guid("0AFACED1-E828-11D1-9187-B532F1E9575D"));

Using this type, you are now able to create an instance of the coclass, using Activator.CreateInstance:

var instance = Activator.CreateInstance(comType);

Basicly you can now invoke methods using Type.InvokeMember. This only works if the object implements the IDispatch interface.

However for your specific example you should be able to cast the instance to System.Runtime.InteropServices.ComTypes.IPersistFile, which results in an call to QueryInterface for COM objects. Using this interface you can easily access the members of IPersistFile.

You may continue here with further reading.

like image 121
Carsten Avatar answered Oct 24 '22 00:10

Carsten