// This function will display the current power state of the // device specified by DeviceName (BKL1: for example) private void GetDevicePowerState(string DeviceName) { // get the power state CE_DEVICE_POWER state = new CE_DEVICE_POWER(); int result = GetDevicePower(DeviceName, 1, ref state); if (result == ERROR_SUCCESS) { // display the results switch (state) { case CE_DEVICE_POWER.PwrDeviceUnspecified: txtDeviceInfo.Text = "Power State: Unspecified"; break; case CE_DEVICE_POWER.PwrDeviceMaximum: txtDeviceInfo.Text = "Power State: Maximum"; break; default: txtDeviceInfo.Text = "Power State: " + state.ToString(); break; } } else { // display error code in text box txtDeviceInfo.Text = "Error occurred, code: "; if (result == 2) { txtDeviceInfo.Text += "Device not found"; } } }
// This function takes a registry key and recursively enumerates its values // and subkeys while adding each to the parent TreeNode for displaying // in the TreeView control private void PopulateListFromReg(string rootKey, string keyName, ref ListBox parent) { // make sure rootKey and keyName aren't empty if (!rootKey.Equals("")) { // list the values first string Values = Registry.EnumValues(Registry.RootKey.LocalMachine, rootKey, false); // make sure Values is not null if (Values != null) { // split the coma-delimited return string up into the individual values string[] ValList = Values.Split(','); // iterate through each value and add to the tree // if the device is recognized by GetDevicePower foreach (string curVal in ValList) { if (curVal.StartsWith("Name")) { string DeviceName = curVal.Substring(5, curVal.Length - 6); // check to see if it's recognized by GetDevicePower // if it's recognized CE_DEVICE_POWER state = new CE_DEVICE_POWER(); int result = GetDevicePower(DeviceName, 1, ref state); if (result != 2) { parent.Items.Add(DeviceName); } } } } // now list the keys string Keys = Registry.EnumValues(Registry.RootKey.LocalMachine, rootKey, true); if (Keys != null) { string[] KeyList = Keys.Split(','); // iterate through each subkey and add to the tree foreach (string curKey in KeyList) { if (!curKey.Equals("")) { string subKey = rootKey + "\\" + curKey; PopulateListFromReg(subKey, curKey, ref parent); } } } } }
// This function will attempt to set the power state of the specified // device using both DevicePowerNotify and SetDevicePower. Sometimes // DevicePowerNotify by itself will not work. private void SetDevicePowerState(string DeviceName, CE_DEVICE_POWER state) { // set the power state int result = DevicePowerNotify(DeviceName, state, POWER_NAME); if (result != 0) { // Show error code txtDeviceInfo.Text = "Error in DPN, code: " + result.ToString(); } result = SetDevicePower(DeviceName, POWER_NAME, state); if (result != 0) { txtDeviceInfo.Text += "\r\nError in SDP, code: " + result.ToString(); } }
public void btnSetDevicePower_Click(object sender, EventArgs e) { // read the device name string DeviceName = txtDeviceName.Text; if (DeviceName.Equals("")) { return; } else { CE_DEVICE_POWER state = new CE_DEVICE_POWER(); // determine desired state if (radD0.Checked) { state = CE_DEVICE_POWER.D0; } else if (radD1.Checked) { state = CE_DEVICE_POWER.D1; } else if (radD2.Checked) { state = CE_DEVICE_POWER.D2; } else if (radD3.Checked) { state = CE_DEVICE_POWER.D3; } else { state = CE_DEVICE_POWER.D4; } SetDevicePowerState(DeviceName, state); // call GetDevicePowerState and auto-update the text box // so the user knows the (new) current state GetDevicePowerState(DeviceName); } }
private static extern int DevicePowerNotify( string device, CE_DEVICE_POWER state, int flags);
private static extern int GetDevicePower( [MarshalAs(UnmanagedType.LPWStr)] string pvDevice, int dwDeviceFlags, ref CE_DEVICE_POWER DeviceState);
private static extern int SetDevicePower( string pvDevice, int dwDeviceFlags, CE_DEVICE_POWER DeviceState);
public void btnSetDevicePower_Click(object sender, EventArgs e) { // read the device name string DeviceName = txtDeviceName.Text; if (DeviceName.Equals("")) { return; } else { CE_DEVICE_POWER state = new CE_DEVICE_POWER(); // determine desired state if (radD0.Checked) state = CE_DEVICE_POWER.D0; else if (radD1.Checked) state = CE_DEVICE_POWER.D1; else if (radD2.Checked) state = CE_DEVICE_POWER.D2; else if (radD3.Checked) state = CE_DEVICE_POWER.D3; else state = CE_DEVICE_POWER.D4; SetDevicePowerState(DeviceName, state); // call GetDevicePowerState and auto-update the text box // so the user knows the (new) current state GetDevicePowerState(DeviceName); } }
// This function will display the current power state of the // device specified by DeviceName (BKL1: for example) private void GetDevicePowerState(string DeviceName) { // get the power state CE_DEVICE_POWER state = new CE_DEVICE_POWER(); int result = GetDevicePower(DeviceName, 1, ref state); if (result == ERROR_SUCCESS) { // display the results switch (state) { case CE_DEVICE_POWER.PwrDeviceUnspecified: txtDeviceInfo.Text = "Power State: Unspecified"; break; case CE_DEVICE_POWER.PwrDeviceMaximum: txtDeviceInfo.Text = "Power State: Maximum"; break; default: txtDeviceInfo.Text = "Power State: " + state.ToString(); break; } } else { // display error code in text box txtDeviceInfo.Text = "Error occurred, code: "; if (result == 2) txtDeviceInfo.Text += "Device not found"; } }
private static extern int GetDevicePower( [MarshalAs(UnmanagedType.LPWStr)]string pvDevice, int dwDeviceFlags, ref CE_DEVICE_POWER DeviceState);