Use C # to do WinForm programs, long time will inevitably encounter and COM components to deal with the place, how to create COM objects also become a problem we must face. As far as I know, there are several ways to create COM objects:
1 used. NET wrapper COM component
The easiest thing to do is to import the DLL that contains the COM components and have the IDE generate a. NET Il wrapper into the project so that all COM types and their associated types that are implemented in COM can be used directly in the. NET program, and the Idispatch,dual For example, in the 2003 era, to write their own browser-based browser, you have to manually join the IWebBrowser2 interface-related DLL, this way is the most commonly used, but also the most fool, so there is nothing to explain.
But this way has a very bad disadvantage---not all COM objects can be exported in this way. As mentioned earlier, only interfaces that implement the Idispatch,dual type will support being exported, and different versions of COM may produce dissimilar export DLLs. For example, a machine to write code on the import of a Jet2.6 version of the packaging DLL, code compiled to get to the B machine to run, but the B machine version of the jet is 2.8, there may be a run-time error.
2 Dynamic Creation with reflection
This includes using the Type.gettypefromclsid and Type.getfromprogid two methods to get the type of the COM object and create it. This is a good way to understand that you have to know the GUID or the ProgID of a COM object before you use both methods. Fortunately, this is not difficult, generally we want to make a COM object, more or less understand some of this COM object GUID or progid information. You can use this to get a type object. NET to create objects in the common reflection method to do.
Here is a code instance of the COM object that created the JetEngine:
1 public Object Getactivexobject (Guid clsid)
2 {
3 Type t = type.gettypefromclsid (CLSID);
4 if (t = = null) return null;
5
6 return activator.createinstance (t);
7}
8
9 GUID g = new GUID ("De88c160-ff2c-11d1-bb6f-00c04fae22da"); JetEngine
Object jet = Getactivexobject (g);
Does it feel like the last place to call Getactivexobject (g) and the JavaScript inside IE that uses new activexojbect to create COM objects?
3 Declare CoCreateInstance external function, use this function to create the corresponding COM instance
M$ in 2005 inside the wrapped WebBrowser control inside is to use this function to create, use this way to create COM, just like in C + + is no different. One thing to note is that when we introduce external methods into our code, The parameters of the method and the type of the return value are not necessarily the only ones that can be used logically as they are converted to each other.
For example, the following statements are correct:
1 [Return:marshalas (Unmanagedtype.interface)]
2 [DllImport ("Ole32.dll", Exactspelling=true, preservesig= False)]
3 public static extern object CoCreateInstance ([in] ref GUID CLSID,
4 [MarshalAs (Unmanagedtype.interfa CE)] object punkouter, int context, [in] ref Guid (IID);
5
6 [DllImport ("Ole32.dll", Exactspelling=true, Preservesig=false)]
7 public static extern IntPtr Cocreatein Stance ([in] ref GUID CLSID,
8 IntPtr punkouter, int context, [in] ref GUID IID);
9
Ten [DllImport ("Ole32.dll ", Exactspelling=true)]
one public static extern int CoCreateInstance ([in] ref Guid CLSID,
IntPtr Punkouter, I NT context, [in] ref Guid IID, [out] out IntPtr pvoid);
[DllImport (Ole32.dll, exactspelling=true)]
public static extern int CoCreateInstance ([in] ref GUID CLSID,
[MarshalAs (Unmanagedtype.interface)] object punkouter, int context,
[in] ref Guid IID, [Marshal As (Unmanagedtype.interface), out] out object pVoid);
Even when you have the corresponding declaration of the interface type, you can completely change the above object or IntPtr to the corresponding interface type, provided that the declaration of your interface type must be correct. Readers in C + + has done COM must remember this way, But there is no longer a need for CoInitialize and couninitialize. NET inside yourself to help you fix it. Incidentally, the object in the example above is connected to the IntPtr declaration, We can use Marshal.getobjectforiunknown and marshal.getiunknownforobject to turn between object and IntPtr, The first question is of course that both of these methods point to COM objects. This method provides the most incoming parameters and the most flexible creation objects.