[Windows Mobile Development and Hacking General] ft1860:
oh,thanks

It almost work.

It works fine on my Universal,but it doesn't work on my x50v with WM5.

I should not debug it on my x50v,there is so many bugs in Dell's WM5 update.

Another question is how to get the max brightness value.On my Universal it is 10,but on my x50v it is 8;

below is my new code :)

#include <WinReg.h>

#include <pm.h>

TCHAR tszAppName[] = TEXT("PMSAMPLE");

TCHAR tszTitle[] = TEXT("Power Manager Sample");

HINSTANCE g_hInst = NULL; // Local copy of hInstance

HANDLE g_hEventShutDown = NULL;

HANDLE g_hPowerNotificationThread = NULL;

HWND g_hSystemState = NULL;

//***************************************************************************

// Function Name: SetBacklightRequirement

//

// Purpose: Sets or releases the device power requirement to keep the

// backlight at D0

//

// Arguments:

// IN BOOL fBacklightOn - TRUE to leave the backlight on

//

void SetBacklightRequirement(BOOL fBacklightOn)

{

// the name of the backlight device

TCHAR tszBacklightName[] = TEXT("BKL1:");

static HANDLE s_hBacklightReq = NULL;

if (fBacklightOn)

{

if (NULL == s_hBacklightReq)

{

// Set the requirement that the backlight device must remain

// in device state D0 (full power)

s_hBacklightReq = SetPowerRequirement(tszBacklightName, D4,

POWER_NAME, NULL, 0);

if (!s_hBacklightReq)

{

RETAILMSG(1, (L"SetPowerRequirement failed: %X\n",

GetLastError()));

}

}

}

else

{

if (s_hBacklightReq)

{

if (ERROR_SUCCESS != ReleasePowerRequirement(s_hBacklightReq))

{

RETAILMSG(1, (L"ReleasePowerRequirement failed: %X\n",

GetLastError()));

}

s_hBacklightReq = NULL;

}

}

}

#define REG_BACKLIGHT L"ControlPanel\\Backlight"

//#define REG_VAL_BATT_TO L"BatteryTimeout"

//#define REG_VAL_AC_TO L"ACTimeout"

#define REG_VAL_BN L"BrightNess"

#define REG_VAL_ACBN L"ACBrightness"

unsigned int OldBattBN=0;

unsigned int OldACBN=0;

void RegOptionBL( DWORD dw1,DWORD dw2)

{

HKEY hKey = 0;

DWORD dwSize;

HANDLE hBL;

static bool bOpened=false;

if ( ERROR_SUCCESS == RegOpenKeyEx( HKEY_CURRENT_USER,REG_BACKLIGHT, 0, 0, &hKey ) )

{

if( !bOpened )

{

bOpened=true;

dwSize = 4;

RegQueryValueEx( hKey, REG_VAL_BN,NULL,NULL,(unsigned char*)&OldBattBN,&dwSize );

dwSize = 4;

RegQueryValueEx( hKey, REG_VAL_ACBN,NULL,NULL,(unsigned char*) &OldACBN,&dwSize );

// dwSize = 4;

// dwValue = 0xefff ;

// RegSetValueEx( hKey,REG_VAL_BATT_TO,NULL,REG_DWORD,(unsigned char *)&dwValue,dwSize );

// dwSize = 4;

// dwValue = 0xefff ;

// RegSetValueEx( hKey,REG_VAL_AC_TO,NULL,REG_DWORD,(unsigned char *)&dwValue,dwSize );

dwSize = 4;

RegSetValueEx( hKey,REG_VAL_BN,NULL,REG_DWORD,(unsigned char *)&dw1,dwSize );

dwSize = 4;

RegSetValueEx( hKey,REG_VAL_ACBN,NULL,REG_DWORD,(unsigned char *)&dw2,dwSize );

}

else

{

if (OldBattBN)

{

dwSize = 4;

RegSetValueEx( hKey,REG_VAL_BN,NULL,REG_DWORD,(unsigned char *)&OldBattBN,dwSize );

}

if (OldACBN)

{

OldACBN = 4;

RegSetValueEx( hKey,REG_VAL_ACBN,NULL,REG_DWORD,(unsigned char *)&OldACBN,dwSize );

}

}

RegCloseKey( hKey );

hBL = CreateEvent( NULL, FALSE, FALSE,L"BackLightChangeEvent" );

if( hBL )

{

SetEvent(hBL);

CloseHandle( hBL );

}

}

}

int WINAPI WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR lpCmdLine,

int nCmdShow)

{

int nResult = 0;

// Create mutex to track whether or not an application is already running

HANDLE hMutex = CreateMutex(0, 0, _T("_PMSAMPLE_EXE_MUTEX_"));

// check the result code

if (NULL != hMutex)

{

if (ERROR_ALREADY_EXISTS != GetLastError())

{

SetBacklightRequirement(TRUE);

RegOptionBL(8,8);

Sleep(5000);

RegOptionBL(-1,-1);

SetBacklightRequirement(FALSE);

}

else

{

}

CloseHandle(hMutex);

}

return ( nResult >= 0 );

}