コード例 #1
0
ファイル: RawCarClientApp.cs プロジェクト: summy00/COM
        static void Main(string[] args)
        {
            // Make a ComCar.
            Console.WriteLine("****Making ComCar and using object reference. ****");
            ComCarClass cCar = new ComCarClass();

            cCar.CrankTunes();

            for (int i = 0; i < 10; i++)
            {
                cCar.SpeedUp(10);
                Console.WriteLine("cCar speed is: {0}", cCar.CurrentSpeed());
            }

            // Now make a ComCar and use interfaces.
            Console.WriteLine("\n**** Making ComCar and using interface references. ****");
            ComCarClass cCar2    = new ComCarClass();
            IRadio      itfRadio = (IRadio)cCar2;

            itfRadio.CrankTunes();

            ICar itfCar = (ICar)cCar2;

            for (int i = 0; i < 10; i++)
            {
                itfCar.SpeedUp(10);
                Console.WriteLine("cCar2 speed is: {0}", itfCar.CurrentSpeed());
            }

            // Now make a ComCar using ComCar.
            Console.WriteLine("\n**** Making ComCar and using ComCar. ****");
            ComCar cCar3 = new ComCar();

            for (int i = 0; i < 10; i++)
            {
                cCar3.SpeedUp(10);
                Console.WriteLine("cCar2 speed is: {0}", cCar3.CurrentSpeed());
            }

            // Trigger inherited System.Object members.
            Console.WriteLine("\n****Using inherited System.Object members. ****");
            ComCarClass theCar   = new ComCarClass();
            ComCarClass otherCar = new ComCarClass();

            Console.WriteLine("ToString: {0}", theCar.ToString());
            Console.WriteLine("Hash: {0}", theCar.GetHashCode().ToString());
            Console.WriteLine("theCar = otherCar? : {0} ", theCar.Equals(otherCar).ToString());
            Type t = theCar.GetType();

            Console.WriteLine(t.Assembly.FullName);

            // Make derived type.
            Console.WriteLine("\n****Using .NET type derived from COM wrapper. ****");
            DotNetCar dnc = new DotNetCar();

            dnc.CrankTunes();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // Make a raw COM car.
            ComCarClass c = new ComCarClass();

            // See if this guy is a COM object...
            Console.WriteLine("Am I a COM object? {0}",
                              Marshal.IsComObject(c).ToString());

            // Get IUnknown of object.
            IntPtr itfUnknownPtr = Marshal.GetIUnknownForObject(c);

            // Manually AddRef() and Release()
            // using the IUnknown pointer.
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("AddReffing! Count: {0}",
                                  Marshal.AddRef(itfUnknownPtr));
                Console.WriteLine("Releasing! Count: {0}",
                                  Marshal.Release(itfUnknownPtr));
            }

            // Manually call QueryInterface().
            IntPtr itfRadioPtr;
            Guid   IIDRadio = Marshal.GenerateGuidForType(typeof(IRadio));

            Marshal.QueryInterface(itfUnknownPtr,
                                   ref IIDRadio,
                                   out itfRadioPtr);

            // Convert raw IntPtr to IRadio and
            // manually crank tunes.
            IRadio itfRadio = (IRadio)Marshal.GetObjectForIUnknown(itfRadioPtr);

            itfRadio.CrankTunes();

            // This releases ALL interfaces
            // held by the RCW.
            ComCarClass c2 = new ComCarClass();

            Console.WriteLine("Ref Count after calling Marshal.ReleaseComObject(c2): {0}",
                              Marshal.ReleaseComObject(c2));

            // This will throw an error.
            try
            {
                c2.CrankTunes();
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #3
0
ファイル: RawCarClientApp.cs プロジェクト: rojac07/COM
        static void Main(string[] args)
        {
            // Make a ComCar.
            Console.WriteLine("****Making ComCar and using object reference. ****");
            ComCarClass cCar = new ComCarClass();
            cCar.CrankTunes();

            for(int i = 0; i < 10; i++)
            {
                cCar.SpeedUp(10);
                Console.WriteLine("cCar speed is: {0}", cCar.CurrentSpeed());
            }

            // Now make a ComCar and use interfaces.
            Console.WriteLine("\n**** Making ComCar and using interface references. ****");
            ComCarClass cCar2 = new ComCarClass();
            IRadio itfRadio = (IRadio)cCar2;
            itfRadio.CrankTunes();

            ICar itfCar = (ICar)cCar2;
            for(int i = 0; i < 10; i++)
            {
                itfCar.SpeedUp(10);
                Console.WriteLine("cCar2 speed is: {0}", itfCar.CurrentSpeed());
            }

            // Now make a ComCar using ComCar.
            Console.WriteLine("\n**** Making ComCar and using ComCar. ****");
            ComCar cCar3 = new ComCar();

            for(int i = 0; i < 10; i++)
            {
                cCar3.SpeedUp(10);
                Console.WriteLine("cCar2 speed is: {0}", cCar3.CurrentSpeed());
            }

            // Trigger inherited System.Object members.
            Console.WriteLine("\n****Using inherited System.Object members. ****");
            ComCarClass theCar = new ComCarClass();
            ComCarClass otherCar = new ComCarClass();
            Console.WriteLine("ToString: {0}", theCar.ToString());
            Console.WriteLine("Hash: {0}", theCar.GetHashCode().ToString());
            Console.WriteLine("theCar = otherCar? : {0} ", theCar.Equals(otherCar).ToString());
            Type t = theCar.GetType();
            Console.WriteLine(t.Assembly.FullName);

            // Make derived type.
            Console.WriteLine("\n****Using .NET type derived from COM wrapper. ****");
            DotNetCar dnc = new DotNetCar();
            dnc.CrankTunes();
        }
コード例 #4
0
ファイル: DownAndDirtyClient.cs プロジェクト: rojac07/COM
        static void Main(string[] args)
        {
            // Make a raw COM car.
            ComCarClass c = new ComCarClass();

            // See if this guy is a COM object...
            Console.WriteLine("Am I a COM object? {0}",
                Marshal.IsComObject(c).ToString());

            // Get IUnknown of object.
            IntPtr itfUnknownPtr = Marshal.GetIUnknownForObject(c);

            // Manually AddRef() and Release()
            // using the IUnknown pointer.
            for(int i = 0; i < 5; i++)
            {
                Console.WriteLine("AddReffing! Count: {0}",
                    Marshal.AddRef(itfUnknownPtr));
                Console.WriteLine("Releasing! Count: {0}",
                    Marshal.Release(itfUnknownPtr));
            }

            // Manually call QueryInterface().
            IntPtr itfRadioPtr;
            Guid IIDRadio = Marshal.GenerateGuidForType(typeof(IRadio));
            Marshal.QueryInterface(itfUnknownPtr,
                ref IIDRadio,
                out itfRadioPtr);

            // Convert raw IntPtr to IRadio and
            // manually crank tunes.
            IRadio itfRadio = (IRadio)Marshal.GetObjectForIUnknown(itfRadioPtr);
            itfRadio.CrankTunes();

            // This releases ALL interfaces
            // held by the RCW.
            ComCarClass c2 = new ComCarClass();
            Console.WriteLine("Ref Count after calling Marshal.ReleaseComObject(c2): {0}",
                Marshal.ReleaseComObject(c2));

            // This will throw an error.
            try
            {
                c2.CrankTunes();
            }
            catch(NullReferenceException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }