Hi All,
Is there a way to call and get "Battery Information" using "DeviceIOControl" API.
Appreciated, if answer with some utility or sample code.
Thanks,
Sanjay
4 6525
Unfortunately Bytes.com isn't a code writing service, we're a forum of like-minded people offering help to those who are willing to help themselves.
Show us the code you've tried along with any issues or error messages you're getting and we'll do our best to help :)
Sure... Below is the code: - #region Variables Declaration
-
-
private const int DIGCF_DEFAULT = 0x00000001; // only valid with DIGCF_DEVICEINTERFACE
-
private const int DIGCF_PRESENT = 0x00000002;
-
private const int DIGCF_ALLCLASSES = 0x00000004;
-
private const int DIGCF_PROFILE = 0x00000008;
-
private const int DIGCF_DEVICEINTERFACE = 0x00000010;
-
-
const int GENERIC_READ = 80000000;
-
const uint GENERIC_WRITE = 0x40000000;
-
internal static uint OPEN_EXISTING = 3;
-
internal static uint FILE_ATTRIBUTE_NORMAL = 0x80;
-
internal static uint FILE_SHARE_READ = 1;
-
internal static uint FILE_SHARE_WRITE = 2;
-
-
public const uint METHOD_BUFFERED = 0;
-
public const uint FILE_ANY_ACCESS = 0x0001;
-
public const uint FILE_DEVICE_BATTERY = 0x294044; //0x294040; //0x00000029;
-
-
public static readonly Guid GUID_DEVCLASS_BATTERY = new Guid(0x72631e54, 0x78a4, 0x11d0, 0xbc, 0xf7, 0x00, 0xaa, 0x00, 0xb7, 0xb3, 0x2a);
-
#endregion
-
-
#region Windows APIs
-
-
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
-
static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, [MarshalAs(UnmanagedType.LPTStr)] string Enumerator,
-
IntPtr hwndParent, uint Flags);
-
[DllImport("setupapi.dll")]
-
internal static extern Int32 SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, Int32 MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
-
-
[DllImport("setupapi.dll", SetLastError = true)]
-
protected static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr lpDeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA oInterfaceData, IntPtr lpDeviceInterfaceDetailData, uint nDeviceInterfaceDetailDataSize, out uint nRequiredSize, IntPtr lpDeviceInfoData);
-
-
[DllImport("setupapi.dll", SetLastError = true)]
-
protected static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr lpDeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA oInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA oDetailData, uint nDeviceInterfaceDetailDataSize, out uint nRequiredSize, IntPtr lpDeviceInfoData);
-
-
[DllImport("kernel32.dll", SetLastError = true)]
-
public static extern IntPtr CreateFile([MarshalAs(UnmanagedType.LPStr)] string lpFileName, uint dwDesiredAccess,
-
uint dwShareMode, IntPtr SecurityAttributes,
-
uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
-
-
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
-
public static extern SafeFileHandle CreateFile(string fileName, [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
-
[MarshalAs(UnmanagedType.U4)] FileShare fileShare, IntPtr securityAttributes,
-
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, [MarshalAs(UnmanagedType.U4)] EFileAttributes flags,
-
IntPtr template);
-
-
[DllImport("wimgapi.dll", EntryPoint = "WIMCloseHandle", SetLastError = true)]
-
public static extern int WIMCloseHandle(IntPtr hObject);
-
-
[DllImport(@"hid.dll", CharSet = CharSet.Auto, SetLastError = true)]
-
public static extern void HidD_GetHidGuid(out Guid gHid);
-
-
[DllImport("setupapi.dll")]
-
internal static extern Int32 SetupDiClassNameFromGuid(ref Guid ClassGuid, StringBuilder className, Int32 ClassNameSize, ref Int32 RequiredSize);
-
-
[DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
-
public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInvo,
-
ref Guid interfaceClassGuid, Int32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
-
-
[DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
-
public static extern UInt16 SetupDiDestroyDeviceInfoList(IntPtr hDevInfo);
-
-
//[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
-
//public static extern bool DeviceIoControl(SafeHandle hDevice, uint dwIoControlCode,
-
// IntPtr lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, out int lpBytesReturned,
-
// IntPtr lpOverlapped);
-
-
//[DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
-
//public static extern bool DeviceIoControl(SafeFileHandle hDevice, uint IoControlCode,
-
// [MarshalAs(UnmanagedType.AsAny)][In] BATTERY_QUERY_INFORMATION InBuffer, uint nInBufferSize,
-
//[MarshalAs(UnmanagedType.AsAny)][Out] BATTERY_INFORMATION OutBuffer, out uint nOutBufferSize, ref uint pBytesReturned,
-
//[In] IntPtr lpOverlapped);
-
-
[DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
-
public static extern bool DeviceIoControl(SafeFileHandle hDevice, uint IoControlCode,
-
ref BATTERY_QUERY_INFORMATION InBuffer, uint nInBufferSize,
-
ref BATTERY_INFORMATION OutBuffer, uint nOutBufferSize, ref uint pBytesReturned,
-
IntPtr lpOverlapped);
-
-
[DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
-
public static extern bool DeviceIoControl(SafeFileHandle hDevice, uint IoControlCode, ref ulong InBuffer,
-
uint nInBufferSize, ref ulong OutBuffer, uint nOutBufferSize, ref ulong pBytesReturned, object lpOverlapped);
-
-
[DllImport("kernel32.dll")]
-
public static extern int DeviceIoControl(IntPtr hDevice, int dwIoControlCode, ref short lpInBuffer, int nInBufferSize,
-
IntPtr lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr lpOverlapped);
-
-
#endregion
-
-
#region Structures and Enums
-
-
[StructLayout(LayoutKind.Sequential, Pack = 1)]
-
public struct SP_DEVICE_INTERFACE_DATA
-
{
-
public UInt32 cbSize;
-
public Guid InterfaceClassGuid;
-
public UInt32 Flags;
-
public UIntPtr Reserved;
-
}
-
[StructLayout(LayoutKind.Sequential, Pack = 1)]
-
public struct SP_DEVICE_INTERFACE_DETAIL_DATA
-
{
-
public UInt32 cbSize;
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
-
public string DevicePath;
-
}
-
-
[StructLayout(LayoutKind.Sequential, Pack = 1)]
-
public struct BATTERY_QUERY_INFORMATION
-
{
-
public ulong BatteryTag;
-
public BATTERY_QUERY_INFORMATION_LEVEL InformationLevel;
-
public long AtRate;
-
}
-
-
[StructLayout(LayoutKind.Sequential, Pack = 1)]
-
public struct BATTERY_QUERY_INFORMATION_LEVEL
-
{
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
-
public string BatteryDeviceName;
-
public ulong BatteryEstimatedTime;
-
public BATTERY_REPORTING_SCALE BatteryGranularityInformation;
-
public BATTERY_INFORMATION BatteryInformation;
-
public BATTERY_MANUFACTURE_DATE BatteryManufactureDate;
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
-
public string BatteryManufactureName;
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
-
public string BatterySerialNumber;
-
public ulong BatteryTemperature;
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
-
public string BatteryUniqueID;
-
}
-
-
[StructLayout(LayoutKind.Sequential, Pack = 1)]
-
public struct BATTERY_REPORTING_SCALE
-
{
-
public ulong Granularity;
-
public ulong Capacity;
-
}
-
-
[StructLayout(LayoutKind.Sequential, Pack = 1)]
-
public struct BATTERY_MANUFACTURE_DATE
-
{
-
public char Day;
-
public char Month;
-
public ushort Year;
-
}
-
-
[StructLayout(LayoutKind.Sequential, Pack = 1)]
-
public struct BATTERY_INFORMATION
-
{
-
public ulong Capabilities;
-
public char Technology;
-
public char[] Reserved;
-
public char[] Chemistry;
-
public ulong DesignCapacity;
-
public ulong FullChargeCapacity;
-
public ulong DefaultAlert1;
-
public ulong DefaultAlert2;
-
public ulong CriticalBias;
-
public ulong CycleCount;
-
}
-
-
[Flags]
-
public enum EFileAttributes : uint
-
{
-
Readonly = 0x00000001,
-
Hidden = 0x00000002,
-
System = 0x00000004,
-
Directory = 0x00000010,
-
Archive = 0x00000020,
-
Device = 0x00000040,
-
Normal = 0x00000080,
-
Temporary = 0x00000100,
-
SparseFile = 0x00000200,
-
ReparsePoint = 0x00000400,
-
Compressed = 0x00000800,
-
Offline = 0x00001000,
-
NotContentIndexed = 0x00002000,
-
Encrypted = 0x00004000,
-
Write_Through = 0x80000000,
-
Overlapped = 0x40000000,
-
NoBuffering = 0x20000000,
-
RandomAccess = 0x10000000,
-
SequentialScan = 0x08000000,
-
DeleteOnClose = 0x04000000,
-
BackupSemantics = 0x02000000,
-
PosixSemantics = 0x01000000,
-
OpenReparsePoint = 0x00200000,
-
OpenNoRecall = 0x00100000,
-
FirstPipeInstance = 0x00080000
-
}
-
-
#endregion
-
-
#region Methods
-
public void ConnectWithDevice()
-
{
-
int index = 0;
-
Guid guid = GUID_DEVCLASS_BATTERY;
-
-
// get the GUID of the HID class
-
//HidD_GetHidGuid(out guid);
-
-
// get a handle to all devices that are part of the HID class
-
// Fun fact: DIGCF_PRESENT worked on my machine just fine. I reinstalled Vista, and now it no longer finds the Wiimote with that parameter enabled...
-
IntPtr hDevInfo = SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE);// | HIDImports.DIGCF_PRESENT);
-
-
// create a new interface data struct and initialize its size
-
SP_DEVICE_INTERFACE_DATA diData = new SP_DEVICE_INTERFACE_DATA();
-
diData.cbSize = (uint)Marshal.SizeOf(diData);
-
-
// get a device interface to a single device (enumerate all devices)
-
while (SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
-
{
-
UInt32 size;
-
-
// Get Device Name
-
string deviceName = GetClassNameFromGuid(diData.InterfaceClassGuid);
-
-
// get the buffer size for this device detail instance (returned in the size parameter)
-
SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);
-
-
// create a detail struct and set its size
-
SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new SP_DEVICE_INTERFACE_DETAIL_DATA();
-
diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5);
-
-
// actually get the detail struct
-
if (SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
-
{
-
MessageBox.Show("Battery " + (index + 1) + " Device Path: " + diDetail.DevicePath);
-
-
// open a read/write handle to our device using the DevicePath returned
-
SafeFileHandle mHandle = CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, EFileAttributes.Overlapped, IntPtr.Zero);
-
-
if (!mHandle.IsInvalid)
-
{
-
bool result = false;
-
-
try
-
{
-
ulong time = 0, tag = 0, bytes = 0;
-
result = DeviceIoControl(mHandle, CTL_CODE(FILE_DEVICE_BATTERY, 0x10, METHOD_BUFFERED, FILE_ANY_ACCESS),
-
ref time, (uint)Marshal.SizeOf(time), ref tag, (uint)Marshal.SizeOf(tag), ref bytes, null);
-
if (result)
-
{
-
BATTERY_QUERY_INFORMATION bqi = new BATTERY_QUERY_INFORMATION();
-
bqi.AtRate = 0;
-
bqi.BatteryTag = tag;
-
//bqi.InformationLevel =
-
-
MessageBox.Show("Tag: " + tag.ToString() + " and bytes: " + bytes);
-
-
uint len = 0;
-
uint nInBufferSize = (uint)Marshal.SizeOf(bqi);
-
BATTERY_INFORMATION bi = new BATTERY_INFORMATION();
-
uint nOutBufferSize = (uint)Marshal.SizeOf(bqi);
-
result = false;
-
-
result = DeviceIoControl(mHandle, CTL_CODE(FILE_DEVICE_BATTERY, 0x11, METHOD_BUFFERED, FILE_ANY_ACCESS),
-
ref bqi, nInBufferSize, ref bi, nOutBufferSize, ref len, IntPtr.Zero);
-
-
if (result)
-
{
-
MessageBox.Show("Bytes Return: " + len.ToString());
-
MessageBox.Show("Design Capacity: " + bi.DesignCapacity);
-
MessageBox.Show("Full Charge Capacity: " + bi.FullChargeCapacity);
-
}
-
else
-
{
-
MessageBox.Show("No Result");
-
}
-
}
-
else
-
{
-
MessageBox.Show("Battery Query Information Not processed");
-
}
-
}
-
finally
-
{
-
//Marshal.FreeHGlobal(pInBuffer);
-
//Marshal.FreeHGlobal(pOutBuffer);
-
}
-
-
}
-
else
-
{
-
MessageBox.Show("In-valid File Handle");
-
}
-
-
mHandle.Close();
-
-
int errCode = Marshal.GetLastWin32Error();
-
MessageBox.Show("Win32 Error: " + errCode.ToString());
-
}
-
else
-
{
-
// failed to get the detail struct
-
throw new Exception("SetupDiGetDeviceInterfaceDetail failed on index " + index);
-
}
-
-
// move to the next device
-
index++;
-
}
-
-
// clean up our list
-
SetupDiDestroyDeviceInfoList(hDevInfo);
-
-
// if we didn't find a Wiimote, throw an exception
-
//if (!found)
-
// throw new Exception("Wiimote not found in HID device list.");
-
}
-
-
private static uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access)
-
{
-
return (uint)(((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method));
-
}
-
-
#endregion
-
Thanks,
Sanjay @PsychoCoder
Moved to C# forum, also, what problems/errors are you having with this code?
Hi,
I have issue related to the Windows API "DeviceIOControl" calling and its declaration. Actully, this API not returning value of "Battery_Information" object. I need value of properties "DesignCapacity" and "FullChargeCapacity" from this object.
I have called this API "DevieIOControl" in method "ConnectWithDevice".
You can also have suggest with different approach to get the value of properties "DesignCapacity" and "FullChargeCapacity".
Thanks,
Sanjay
Sign in to post your reply or Sign up for a free account.
Similar topics
by: ewoo |
last post by:
I'm trying to write a wrapper in csharp to wrap DeviceIoControl() win32
method for IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS control code--without much
luck.
I've seen lots of examples out there for...
|
by: Maria |
last post by:
Hi all ! :)
I am trying to find a way to return the battery level of a notebook or
laptop or any mobile device (but to make it simple a laptop would be
fine).
I am trying to do this because I'm...
|
by: Ken Allen |
last post by:
I am looking for information on using the Win32 API
procedure, or its equivalent in C#. I have looked but can find no
satisfactory references.
I am working on an existing product that has a few...
|
by: Michael Allen |
last post by:
I would like to perform something similar to the below function in C# .NET.
The C++ below code is from a Microsoft DDK sample driver application.
Specifically, I would like to perform Device I/O...
|
by: Jacky |
last post by:
Hi,
I am trying to make network card interface with VB.NET 2002.
I use DeviceIOControl-function. I have tried to define inbuffer and
outbuffer using byte array and it's pointer. The second I...
|
by: Pixie |
last post by:
I am trying to query the change journal using the deviceIOControl API. The API doesn't return an error, but all of the values in the output buffer are zero, and they shouldn't be. My code is below. I...
|
by: Pixie |
last post by:
We are successfully getting a handle to a drive using createfile then using that handle to query the change journal using DeviceIOControl with the paramter FSCTL_QUERY_USN_DATA. However when we try...
|
by: Juan Pedro Gonzalez |
last post by:
Helo,
I'm having problems here with the input buffer.... Ive defined the API call
as:
<System.Runtime.InteropServices.DllImport("kernel32", SetLastError:=True)> _
Private Shared Function...
|
by: =?Utf-8?B?TWFyaW5h?= |
last post by:
Does any know any sample of how to do a basic DeviceIoControl with something
like IOCTL_BATTERY_GETSYSTEMPOWERSTATUSEX2 in C#
I have been stuck all week :(
and google doesnt yield anything of...
|
by: kid joe |
last post by:
Hi all,
Sorry this is a bit off-topic... but I do write a good amount of C code on
my laptop so there is a link...
Basically, it's a question about laptop batteries. About ten days ago, I...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |