473,621 Members | 2,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrapping DeviceIoControl () for IOCTL_VIDEO_QUE RY_DISPLAY_BRIG HTNES

I'm trying to write a wrapper in csharp to wrap DeviceIoControl () win32
method for IOCTL_VIDEO_QUE RY_DISPLAY_BRIG HTNESS 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.Diagnost ics;
using System.Runtime. InteropServices ;

namespace PowerManagement Test
{
[StructLayout(La youtKind.Sequen tial)]
internal struct DISPLAY_BRIGHTN ESS
{
public byte DisplayPolicy; // 0x00000001 Value can be DISPLAYPOLICY_A C or
DISPLAYPOLICY_D C or DISPLAYPOLICY_B OTH
public byte ACBrightness; // 0x00000002
public byte DCBrightness; // 0x00000001 | 0x00000002
}

/// <summary>
/// Constants lifted from winioctl.h from Platform SDK.
/// </summary>
internal class WinIoCtlConstan ts
{
const uint FILE_DEVICE_FIL E_SYSTEM = 0x00000009;
const uint FILE_DEVICE_VID EO = 0x00000023;

const uint FILE_ANY_ACCESS = 0;
const uint FILE_SPECIAL_AC CESS = FILE_ANY_ACCESS ;

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

public static uint FSCTL_GET_VOLUM E_BITMAP =
CTL_CODE(FILE_D EVICE_FILE_SYST EM, 27, METHOD_NEITHER, FILE_ANY_ACCESS );
public static uint FSCTL_GET_RETRI EVAL_POINTERS =
CTL_CODE(FILE_D EVICE_FILE_SYST EM, 28, METHOD_NEITHER, FILE_ANY_ACCESS );
public static uint FSCTL_MOVE_FILE = CTL_CODE(FILE_D EVICE_FILE_SYST EM, 29,
METHOD_BUFFERED , FILE_SPECIAL_AC CESS);

public static uint IOCTL_VIDEO_QUE RY_SUPPORTED_BR IGHTNESS =
CTL_CODE(FILE_D EVICE_VIDEO, 293, METHOD_BUFFERED , FILE_ANY_ACCESS );
public static uint IOCTL_VIDEO_QUE RY_DISPLAY_BRIG HTNESS =
CTL_CODE(FILE_D EVICE_VIDEO, 294, METHOD_BUFFERED , FILE_ANY_ACCESS );
public static uint IOCTL_VIDEO_SET _DISPLAY_BRIGHT NESS =
CTL_CODE(FILE_D EVICE_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_WRIT E = 0x00000002;
const uint FILE_SHARE_DELE TE = 0x00000004;
const uint OPEN_EXISTING = 3;

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

const uint FILE_FLAG_NO_BU FFERING = 0x20000000;
const uint FILE_READ_ATTRI BUTES = (0x0080);
const uint FILE_WRITE_ATTR IBUTES = 0x0100;
const uint ERROR_INSUFFICI ENT_BUFFER = 122;

[DllImport("kern el32.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("kern el32.dll", SetLastError = true)]
static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess ,
uint dwShareMode,
IntPtr lpSecurityAttri butes,
uint dwCreationDispo sition,
uint dwFlagsAndAttri butes,
IntPtr hTemplateFile);

[DllImport("kern el32.dll", SetLastError = true)]
static extern int CloseHandle(Int Ptr hObject);

static private IntPtr OpenVolume(stri ng DeviceName)
{
IntPtr hDevice;
hDevice = CreateFile(
@"\\.\" + DeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRIT E,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hDevice == -1)
{
throw new Exception(Marsh al.GetLastWin32 Error().ToStrin g());
}
return hDevice;
}
static private IntPtr OpenFile(string path)
{
IntPtr hFile;
hFile = CreateFile(
path,
FILE_READ_ATTRI BUTES | FILE_WRITE_ATTR IBUTES,
FILE_SHARE_READ | FILE_SHARE_WRIT E,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hFile == -1)
{
throw new Exception(Marsh al.GetLastWin32 Error().ToStrin g());
}
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.GetLast Win32Error().To String());
throw new Exception(Marsh al.GetLastWin32 Error().ToStrin g());
}
return hDevice;
}

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

// Allocate memory for the Display Brightness structure.
Type structType = typeof(DISPLAY_ BRIGHTNESS);
int structSize = Marshal.SizeOf( structType);
IntPtr pDisplayBrightn ess = Marshal.AllocHG lobal(structSiz e);

DISPLAY_BRIGHTN ESS brightness;
brightness =
(DISPLAY_BRIGHT NESS)Marshal.Pt rToStructure((I ntPtr)pDisplayB rightness,
structType);
// brightness.Disp layPolicy = Convert.ToByte( 1);

try
{
hDevice = OpenLcdDevice() ;

Int64 i64 = 0;

uint outputBufferSiz e = Convert.ToUInt3 2(structSize);
uint bytesReturned = 0;

bool fResult = DeviceIoControl (
hDevice,
WinIoCtlConstan ts.IOCTL_VIDEO_ QUERY_DISPLAY_B RIGHTNESS,
IntPtr.Zero, // Set to NULL.
(uint)Marshal.S izeOf(i64), // Set to zero.
pDisplayBrightn ess, // Pointer to a buffer that receives the
DISPLAY_BRIGHTN ESS structure.
outputBufferSiz e,
ref bytesReturned,
IntPtr.Zero);

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

if (!fResult)
{
Debug.WriteLine ("WIN32 Error: " +
Marshal.GetLast Win32Error().To String(), "QueryDisplayBr ightness");
throw new Exception(Marsh al.GetLastWin32 Error().ToStrin g());
}

int resultAddress = (int)pDisplayBr ightness;
brightness =
(DISPLAY_BRIGHT NESS)Marshal.Pt rToStructure((I ntPtr)resultAdd ress, structType);

Debug.WriteLine ("AC Brightness: " + brightness.ACBr ightness.ToStri ng());
Debug.WriteLine ("DC Brightness: " + brightness.DCBr ightness.ToStri ng());

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

Marshal.FreeHGl obal(pDisplayBr ightness);
pDisplayBrightn ess = IntPtr.Zero;
}
}

static public void QuerySupportedB rightness()
{
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.AllocHG lobal((int)512) ;
IntPtr pDest = pAlloc;

uint outputBufferSiz e = 512;
uint bytesReturned = 0;

bool fResult = DeviceIoControl (
hDevice,
WinIoCtlConstan ts.IOCTL_VIDEO_ QUERY_SUPPORTED _BRIGHTNESS,
IntPtr.Zero, // Set to NULL.
(uint)Marshal.S izeOf(i64), // Set to zero.
pDest, // Pointer to a buffer that receives an array of available
power levels.
outputBufferSiz e,
ref bytesReturned,
IntPtr.Zero);

Debug.WriteLine ("Bytes returned: " + bytesReturned.T oString());
Debug.WriteLine ("WIN32 Error: " +
Marshal.GetLast Win32Error().To String(), "QuerySupported Brightness");

if (!fResult)
{
Debug.WriteLine ("WIN32 Error: " +
Marshal.GetLast Win32Error().To String(), "QuerySupported Brightness");
throw new Exception(Marsh al.GetLastWin32 Error().ToStrin g());
}

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

Marshal.FreeHGl obal(pAlloc);
pAlloc = IntPtr.Zero;
}
}

[DllImport("kern el32.dll")]
public static extern IntPtr CreateFile(
string lpFileName, int dwDesiredAccess , int dwShareMode,
IntPtr lpSecurityAttri butes, int dwCreationDispo sition,
int dwFlagsAndAttri butes, IntPtr hTemplateFile );

private const int INVALID_HANDLE_ VALUE = -1;

// The DeviceIoControl Win32 function.
[DllImport("kern el32.dll", ExactSpelling=t rue) ]
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 2197

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

Similar topics

0
840
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 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...
2
5928
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 it. public static extern int DeviceIoControl( IntPtr hDevice,
2
15532
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 Control from a .NET application. Is there any way to do this in C#. I have tried Googling to no avail. Thanks, Michael Allen
3
8228
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 tried AllocHGlobal and Copy. They works nice though the old app set these parameters to heap and VB.NET to programs reserved memory area.
1
2522
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 have also tried specifying different pack values in my USN_JOURNAL_DATA structure definition but this didn't change anything. Any help would be greatly appreciated Private Const FILE_SHARE_READ = &H1 Private Const FILE_SHARE_WRITE = &H2 Private...
0
2313
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 to enumerate the change journal data using the same handle and using DeviceIOControl with FSCTL_ENUM_DATA it gives error 1 Incorrect function. The help says this means that the drive does not support change journals but this is not the case as we can...
1
4551
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 DeviceIoControl(ByVal hDevice As IntPtr, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBuffer As Integer, ByRef lpOutBuffer As IntPtr, ByVal nOutBufferSize As Integer, ByRef lbBytesResturned As Integer, ByVal lpOverlapped As...
5
3801
by: Lou | last post by:
is there a VB .NET way to use the API "DeviceIoControl"? -Lou
0
1846
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 through openvpn in Ether (Tap mode). code is as follows f = win32file.CreateFile("C:\\WINDOWS\\System32\\drivers\\tunmp.sys", GENERIC_READ, 0, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
4
3885
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 use. Please help me, thanks Marina
0
8213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8156
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8597
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8306
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5554
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.