Click here to Skip to main content
15,896,439 members
Articles / Programming Languages / C# 4.0

Accessing Hardware in Silverlight using COM

Rate me:
Please Sign up or sign in to vote.
4.91/5 (17 votes)
26 May 2010CPOL6 min read 42.6K   856   29  
An example of a COM object written in C# that allows access to hardware from Silverlight
using System;
using System.Runtime.InteropServices.Automation;
using System.Windows;
using System.Windows.Controls;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        dynamic m_ComDrawerContoller;
        AutomationEvent m_DrawerChangeEventHandler;

        public MainPage()
        {
            InitializeComponent();

            // the COM object will only be successfully created if the application is currently running
            // out of browser and in elevated trust.
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
            {
                RegisterComObject();
            }
        }

        private void RegisterComObject()
        {
            try
            {
                // Create the object.  If ProgId attribute is not added to the COM class, the 
                // string to use is assembly.class
                m_ComDrawerContoller = AutomationFactory.CreateObject("ComExample.Application");
                
                // Creates an object to manage the event from the com object.
                m_DrawerChangeEventHandler = AutomationFactory.GetEvent(m_ComDrawerContoller, "DrawerStateChange");

                // Adds a handler for the event actually being raised.
                m_DrawerChangeEventHandler.EventRaised += new EventHandler<AutomationEventArgs>(HandleDrawerStateChange);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Handles the click from any of the "Open Drawer x" buttons.
        /// The drawer number is in the tag property of the button.
        /// </summary>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int drawerNumber = Convert.ToInt32(((Button)sender).Tag);
            m_ComDrawerContoller.OpenDrawer(drawerNumber);
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            m_ComDrawerContoller.CloseAllOpenDrawers();
        }

        /// <summary>
        /// Handles the event raised by the com object.  
        /// </summary>
        private void HandleDrawerStateChange(object sender, AutomationEventArgs e)
        {
            switch ((int)e.Arguments[0])
            {
                case 1:
                    Drawer1Status.Text = (bool)e.Arguments[1] ? "Open" : "Closed";
                    break;
                
                case 2:
                    Drawer2Status.Text = (bool)e.Arguments[1] ? "Open" : "Closed";
                    break;
                case 3:
                    Drawer3Status.Text = (bool)e.Arguments[1] ? "Open" : "Closed";
                    break;
            }
        }

        private void check_click(object sender, System.Windows.RoutedEventArgs e)
        {
        	int drawerNumberToCheck;
			int.TryParse(txtDrawerToCheck.Text, out drawerNumberToCheck);

            if (drawerNumberToCheck > 0)
            {
                bool isOpen = m_ComDrawerContoller.IsDrawerOpen(drawerNumberToCheck);

                if (isOpen) lblCheckStatus.Text = "Open"; else lblCheckStatus.Text = "Closed";
            }
			
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions