473,394 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Wrapping DeviceIoControl() for IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNES

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 low-level file access but can't
seem to any for the display. Can you provide some samples of how I might do
this?

Below is my code thus far...

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace PowerManagementTest
{
[StructLayout(LayoutKind.Sequential)]
internal struct DISPLAY_BRIGHTNESS
{
public byte DisplayPolicy; // 0x00000001 Value can be DISPLAYPOLICY_AC or
DISPLAYPOLICY_DC or DISPLAYPOLICY_BOTH
public byte ACBrightness; // 0x00000002
public byte DCBrightness; // 0x00000001 | 0x00000002
}

/// <summary>
/// Constants lifted from winioctl.h from Platform SDK.
/// </summary>
internal class WinIoCtlConstants
{
const uint FILE_DEVICE_FILE_SYSTEM = 0x00000009;
const uint FILE_DEVICE_VIDEO = 0x00000023;

const uint FILE_ANY_ACCESS = 0;
const uint FILE_SPECIAL_ACCESS = FILE_ANY_ACCESS;

const uint METHOD_BUFFERED = 0;
const uint METHOD_NEITHER = 3;

public static uint FSCTL_GET_VOLUME_BITMAP =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 27, METHOD_NEITHER, FILE_ANY_ACCESS);
public static uint FSCTL_GET_RETRIEVAL_POINTERS =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 28, METHOD_NEITHER, FILE_ANY_ACCESS);
public static uint FSCTL_MOVE_FILE = CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 29,
METHOD_BUFFERED, FILE_SPECIAL_ACCESS);

public static uint IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS =
CTL_CODE(FILE_DEVICE_VIDEO, 293, METHOD_BUFFERED, FILE_ANY_ACCESS);
public static uint IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS =
CTL_CODE(FILE_DEVICE_VIDEO, 294, METHOD_BUFFERED, FILE_ANY_ACCESS);
public static uint IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS =
CTL_CODE(FILE_DEVICE_VIDEO, 295, METHOD_BUFFERED, FILE_ANY_ACCESS);

static uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint
Access)
{
return ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) |
(Method);
}
}

class WinIoCtl
{
// Constants.
const uint FILE_SHARE_READ = 0x00000001;
const uint FILE_SHARE_WRITE = 0x00000002;
const uint FILE_SHARE_DELETE = 0x00000004;
const uint OPEN_EXISTING = 3;

const uint GENERIC_READ = (0x80000000);
const uint GENERIC_WRITE = (0x40000000);

const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
const uint FILE_READ_ATTRIBUTES = (0x0080);
const uint FILE_WRITE_ATTRIBUTES = 0x0100;
const uint ERROR_INSUFFICIENT_BUFFER = 122;

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
[Out] IntPtr lpOutBuffer,
uint nOutBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);

[DllImport("kernel32.dll", SetLastError = true)]
static extern int CloseHandle(IntPtr hObject);

static private IntPtr OpenVolume(string DeviceName)
{
IntPtr hDevice;
hDevice = CreateFile(
@"\\.\" + DeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hDevice == -1)
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hDevice;
}
static private IntPtr OpenFile(string path)
{
IntPtr hFile;
hFile = CreateFile(
path,
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hFile == -1)
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hFile;
}

static private IntPtr OpenLcdDevice()
{
IntPtr hDevice;
hDevice = CreateFile(
@"\\.\LCD",
GENERIC_READ,
FILE_SHARE_READ,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hDevice == -1)
{
Debug.WriteLine("OpenLCD Win32 Error: " +
Marshal.GetLastWin32Error().ToString());
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hDevice;
}

static public void QueryDisplayBrightness()
{
IntPtr hDevice = IntPtr.Zero;

// Allocate memory for the Display Brightness structure.
Type structType = typeof(DISPLAY_BRIGHTNESS);
int structSize = Marshal.SizeOf(structType);
IntPtr pDisplayBrightness = Marshal.AllocHGlobal(structSize);

DISPLAY_BRIGHTNESS brightness;
brightness =
(DISPLAY_BRIGHTNESS)Marshal.PtrToStructure((IntPtr )pDisplayBrightness,
structType);
// brightness.DisplayPolicy = Convert.ToByte(1);

try
{
hDevice = OpenLcdDevice();

Int64 i64 = 0;

uint outputBufferSize = Convert.ToUInt32(structSize);
uint bytesReturned = 0;

bool fResult = DeviceIoControl(
hDevice,
WinIoCtlConstants.IOCTL_VIDEO_QUERY_DISPLAY_BRIGHT NESS,
IntPtr.Zero, // Set to NULL.
(uint)Marshal.SizeOf(i64), // Set to zero.
pDisplayBrightness, // Pointer to a buffer that receives the
DISPLAY_BRIGHTNESS structure.
outputBufferSize,
ref bytesReturned,
IntPtr.Zero);

Debug.WriteLine("Bytes returned: " + bytesReturned.ToString());

if (!fResult)
{
Debug.WriteLine("WIN32 Error: " +
Marshal.GetLastWin32Error().ToString(), "QueryDisplayBrightness");
throw new Exception(Marshal.GetLastWin32Error().ToString());
}

int resultAddress = (int)pDisplayBrightness;
brightness =
(DISPLAY_BRIGHTNESS)Marshal.PtrToStructure((IntPtr )resultAddress, structType);

Debug.WriteLine("AC Brightness: " + brightness.ACBrightness.ToString());
Debug.WriteLine("DC Brightness: " + brightness.DCBrightness.ToString());

}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
CloseHandle(hDevice);
hDevice = IntPtr.Zero;

Marshal.FreeHGlobal(pDisplayBrightness);
pDisplayBrightness = IntPtr.Zero;
}
}

static public void QuerySupportedBrightness()
{
IntPtr pAlloc = IntPtr.Zero;
IntPtr hDevice = IntPtr.Zero;

try
{
// Get a handle to the device.
hDevice = OpenLcdDevice();

Int64 i64 = 0;

// Create storage area in memory and get a handle to it.
pAlloc = Marshal.AllocHGlobal((int)512);
IntPtr pDest = pAlloc;

uint outputBufferSize = 512;
uint bytesReturned = 0;

bool fResult = DeviceIoControl(
hDevice,
WinIoCtlConstants.IOCTL_VIDEO_QUERY_SUPPORTED_BRIG HTNESS,
IntPtr.Zero, // Set to NULL.
(uint)Marshal.SizeOf(i64), // Set to zero.
pDest, // Pointer to a buffer that receives an array of available
power levels.
outputBufferSize,
ref bytesReturned,
IntPtr.Zero);

Debug.WriteLine("Bytes returned: " + bytesReturned.ToString());
Debug.WriteLine("WIN32 Error: " +
Marshal.GetLastWin32Error().ToString(), "QuerySupportedBrightness");

if (!fResult)
{
Debug.WriteLine("WIN32 Error: " +
Marshal.GetLastWin32Error().ToString(), "QuerySupportedBrightness");
throw new Exception(Marshal.GetLastWin32Error().ToString());
}

Int64 StartingLcn = (Int64)Marshal.PtrToStructure(pDest, typeof(Int64));
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
finally
{
CloseHandle(hDevice);
hDevice = IntPtr.Zero;

Marshal.FreeHGlobal(pAlloc);
pAlloc = IntPtr.Zero;
}
}

[DllImport("kernel32.dll")]
public static extern IntPtr CreateFile(
string lpFileName, int dwDesiredAccess, int dwShareMode,
IntPtr lpSecurityAttributes, int dwCreationDisposition,
int dwFlagsAndAttributes, IntPtr hTemplateFile );

private const int INVALID_HANDLE_VALUE = -1;

// The DeviceIoControl Win32 function.
[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern bool DeviceIoControl(
IntPtr hDevice, int dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize,
IntPtr lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr
lpOverlapped );
}
}
Nov 22 '05 #1
0 2179

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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...
2
by: Jules Crown | last post by:
Everyone, greetings, newbee here. What I'm trying to do is to compress files on NTFS from C#. I've been fishing around for info and gathered the hereafter. I'd like to know what's wrong with...
2
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...
3
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...
1
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...
0
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...
1
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...
5
by: Lou | last post by:
is there a VB .NET way to use the API "DeviceIoControl"? -Lou
0
by: Andrew | last post by:
Hello I am trying to port some code and I am running into some issues I may or may not be able to solve on my own and would appreciate your help Basically I am trying to open the Tun Driver...
4
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.