static void LateBindtoComponent() { ///////////////////////////////////////////////////////////////////// // Create an ATLDllCOMServer.SimpleObject COM object. // // Get type from ProgID Type simpleObjType = Type.GetTypeFromProgID( "ATLDllCOMServer.SimpleObject"); // [-or-] Get type from CLSID //Type simpleObjType = Type.GetTypeFromCLSID( // new Guid("7AD9E1C6-E804-43C2-9383-1C8F35283081")); if (simpleObjType == null) { Console.WriteLine("The ATLDllCOMServerLib.SimpleObject " + "component is not registered"); return; } object simpleObj = Activator.CreateInstance(simpleObjType); ///////////////////////////////////////////////////////////////////// // Consume the properties and the methods of the COM object. // try { // Set the property: FloatProperty { Console.WriteLine("Set FloatProperty = {0,0:f2}", 1.2f); simpleObjType.InvokeMember("FloatProperty", BindingFlags.SetProperty, null, simpleObj, new object[] { 1.2f }); } // Get the property: FloatProperty { float fProp = (float)simpleObjType.InvokeMember( "FloatProperty", BindingFlags.GetProperty, null, simpleObj, null); Console.WriteLine("Get FloatProperty = {0,0:f2}", fProp); } // Call the method: HelloWorld, that returns a string. { string strResult = simpleObjType.InvokeMember("HelloWorld", BindingFlags.InvokeMethod, null, simpleObj, null) as string; Console.WriteLine("Call HelloWorld => {0}", strResult); } // Call the method: GetProcessThreadID, that outputs two integers. { Console.WriteLine("The client process and thread: {0}, {1}", NativeMethod.GetCurrentProcessId(), NativeMethod.GetCurrentThreadId()); // Parameter modifiers indicate that both parameters are // output parameters. ParameterModifier[] paramMods = new ParameterModifier[1]; paramMods[0] = new ParameterModifier(2); paramMods[0][0] = true; // [out] parameter paramMods[0][1] = true; // [out] parameter object[] args = new object[2]; // Must initialize the array element. Otherwise, InvokeMember // throws the "Type mismatch" exception. args[0] = new int(); args[1] = new int(); simpleObjType.InvokeMember("GetProcessThreadID", BindingFlags.InvokeMethod, null, simpleObj, args, paramMods, null, null); Console.WriteLine("Call GetProcessThreadID => {0}, {1}", args[0], args[1]); } Console.WriteLine(); } catch (Exception ex) { Console.WriteLine("The server throws the error: {0}", ex.Message); if (ex.InnerException != null) { Console.WriteLine("Description: {0}", ex.InnerException.Message); } } ///////////////////////////////////////////////////////////////////// // Release the COM object. // It is strongly recommended against using ReleaseComObject to // manually release an RCW object that represents a COM component // unless you absolutely have to. We should generally let CLR release // the COM object in the garbage collector. // Ref: http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx // Marshal.FinalReleaseComObject(simpleObj); }
/// <summary> /// /// </summary> static void EarlyBindtoComponent() { ///////////////////////////////////////////////////////////////////// // Create an ATLDllCOMServer.SimpleObject COM object. // ATLDllCOMServerLib.SimpleObject simpleObj = new ATLDllCOMServerLib.SimpleObject(); ///////////////////////////////////////////////////////////////////// // Register the events of the COM object. // simpleObj.FloatPropertyChanging += new ATLDllCOMServerLib. _ISimpleObjectEvents_FloatPropertyChangingEventHandler( simpleObj_FloatPropertyChanging); ///////////////////////////////////////////////////////////////////// // Consume the properties and the methods of the COM object. // try { // Set the property: FloatProperty, which raises the event // FloatPropertyChanging. { Console.WriteLine("Set FloatProperty = {0,0:f2}", 1.2f); simpleObj.FloatProperty = 1.2f; } // Get the property: FloatProperty. { Console.WriteLine("Get FloatProperty = {0,0:f2}", simpleObj.FloatProperty); } // Call the method: HelloWorld, that returns a string. { string strResult = simpleObj.HelloWorld(); Console.WriteLine("Call HelloWorld => {0}", strResult); } // Call the method: GetProcessThreadID, that outputs two integers. { Console.WriteLine("The client process and thread: {0}, {1}", NativeMethod.GetCurrentProcessId(), NativeMethod.GetCurrentThreadId()); int processId, threadId; simpleObj.GetProcessThreadID(out processId, out threadId); Console.WriteLine("Call GetProcessThreadID => {0}, {1}", processId, threadId); } Console.WriteLine(); } catch (Exception ex) { Console.WriteLine("The server throws the error: {0}", ex.Message); if (ex.InnerException != null) { Console.WriteLine("Description: {0}", ex.InnerException.Message); } } ///////////////////////////////////////////////////////////////////// // Release the COM object. // It is strongly recommended against using ReleaseComObject to // manually release an RCW object that represents a COM component // unless you absolutely have to. We should generally let CLR release // the COM object in the garbage collector. // Ref: http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx // Marshal.FinalReleaseComObject(simpleObj); }