C# GetObject and CreateObject
When interoperating with COM applicaitons, sometimes you need to get the running instance of the application object of the app. In VB you would be doing this by using the GetObject method.
Basically the GetObject method returns a reference to an object provided by a COM component.
For example you want to get a reference on the currently running excel program.
Dim excelObj As Object
excelObj = GetObject(, "Excel.Application")
If the above statement causes an error, then no instance of the excel application is running.
This routine is very basic for VB programmers.
But what about C#? How could you do this in C#?
There is no direct equivalent of the GetObject method in C#. I have this problem lately, and I have produced a simple routine that "could be" the equivalent of GetObject method (I've used this to get a reference on a running Autodesk AutoCAD application:
acadObj = (AcadApplication) Marshal.GetActiveObject("AutoCAD.Application.16");
Note: you'll have to add the namespace System.Runtime.InteropServices to be able to use the Marshal object.
In VB you can also create a reference to a COM applicaiton by using the CreateObject method if the GetObject method fails:
excelObj = CreateObject("Excel.Application")
In C# its quite very simple. You just need to allocate the application object of the COM component:
AcadApplication acadObj = new AcadApplication();
This sample routine would then launch a new AutoCAD application.