|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HardwareHelperLib;
/*
* HW_Lib_Test
* ===================================================
* Simple Win Form to Demonstrate HH Lib
* Windows XP SP2, VS2005 C#.NET, DotNet 2.0
* ====================================================
* LOG: Who? When? What?
* (v)1.0.0 WJF 11/26/07 Original Implementation
*/
namespace HW_Lib_Test
{
public partial class Form1 : Form
{
//Global library declared
HH_Lib hwh = new HH_Lib();
//Name: Form1()
//Inputs: none
//Outputs: none
//Remarks: Default constructor
public Form1()
{
InitializeComponent();
}
//Name: Form1_Load()
//Inputs: object, eventArgs
//Outputs: none
//Remarks: In the form load we take an initial hardware inventroy,
// then hook the notifications so we can respond if any
// device is added or removed.
private void Form1_Load(object sender, EventArgs e)
{
string[] HardwareList = hwh.GetAll();
foreach (string s in HardwareList)
{
listBox1.Items.Add(s);
}
hwh.HookHardwareNotifications(this.Handle, true);
label1.Text = listBox1.Items.Count.ToString() + " Devices Attached";
}
//Name: Form1_FormClosing
//Inputs: object, eventArgs
//Outputs: none
//Remarks: Whenever the form closes we need to unregister the
// hardware notifier. Failure to do so could cause
// the system not to release some resources. Calling
// this method if you are not currently hooking the
// hardware events has no ill effects so better to be
// safe than sorry.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
hwh.CutLooseHardwareNotifications(this.Handle);
hwh = null;
}
//Name: WndProc
//Inputs: Message
//Outputs: none
//Remarks: This is the override for the window message handler. Here
// is where we can respond to our DEVICECHANGE message we are
// hooking. If we received a hardware change notification
// the method reloads our hardware list. Otherwise, it must
// call the default handler so the message can be processed.
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case HardwareHelperLib.Native.WM_DEVICECHANGE:
{
if (m.WParam.ToInt32() == HardwareHelperLib.Native.DBT_DEVNODES_CHANGED)
{
listBox1.Items.Clear();
string[] HardwareList = hwh.GetAll();
foreach (string s in HardwareList)
{
listBox1.Items.Add(s);
}
label1.Text = listBox1.Items.Count.ToString() + " Devices Attached";
}
break;
}
}
base.WndProc(ref m);
}
//Name: button1_Clck
//Inputs: object, eventArgs
//Outputs: none
//Remarks: We are using this button to demonstrate enabling a
// hardware device. There are several things worth
// noting. First, just to be safe we are disabling
// hardware notifcations until we are done. The UI
// thread in .NET won't let the WndProc method run
// to my knowledge while you are in here but if you
// were invoking these methods on different callers it
// would be worthwhile to disable the notifications
// during. The call to SetDeviceState is designed
// to allow you to pass in multiple devices in an
// array to disable, even though we are currently just
// doing the selected one. Also the search is a
// substring search so be careful not to use something
// so generic that it will affect more devices than
// the one(s) you intended. See the notes for the
// SetDeviceState method in the library for some
// important info.
private void button1_Click(object sender, EventArgs e)
{
//Enable
string[] devices = new string[1];
hwh.CutLooseHardwareNotifications(this.Handle);
devices[0]= listBox1.SelectedItem.ToString();
hwh.SetDeviceState(devices, true);
hwh.HookHardwareNotifications(this.Handle, true);
}
//Name: button2_Clck
//Inputs: object, eventArgs
//Outputs: none
//Remarks: We are using this button to disable a device.
// See remarks above.
private void button2_Click(object sender, EventArgs e)
{
//Disable
string[] devices = new string[1];
hwh.CutLooseHardwareNotifications(this.Handle);
devices[0] = listBox1.SelectedItem.ToString();
hwh.SetDeviceState(devices, false);
hwh.HookHardwareNotifications(this.Handle, true);
}
}
}
|
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.
I began coding at the ripe old age of eleven (yep, I've always been a nerd). Back then every 8-bit computer on the market had its own flavor of BASIC burned into the EPROM and I was adept at most of them. Somewhere in my bedroom there was an actual bed, but you'd be hard pressed to find it surrounded as it was. My collection included a C64, VIC20, TRS80, APPLE II+, TI-99/4A, and even one of those silver Timex Sinclair "computers" with the chicklet keys.
Eventually I taught myself 6502 assembler, and later Pascal and C. While I spent the majority of my professional career doing a mixture of C++, C#, and dabbling in ARM Assembler, for the last year I've been focusing on JAVA and the Android Platform. While I am a Windows guy at heart lately I'm finding some love for UBUNTU as well.
When I am not at the computer I am hanging out with my 12 year old son. He just finished coding a javascript implementation of Conway's Game of Life. Oh yeah, I guess that means we were in front of the computer. Go figure!