Turn display off with VB.Net

  • Thread starter Thread starter Benjamin Lukner
  • Start date Start date
B

Benjamin Lukner

Hi!

I've found some eVC++ Code for turning the PocketPC display off and on.
Works fine, no problem.
URL:
http://www.microsoft.com/mobile/developer/technicalarticles/displayoff.asp

Now I try to do the same with VB.Net...
The code executes without any error, but the display stays on :-(

Normally it's either a problem with BYREF/BYVAL, the declared data type
or a misused parameter. (Sure, what else...)

Hope there's somebody who is familiar with C++ and VB and may have a
look at the code...

Thanks in advance! :-)


Kind regards,

Benjamin Lukner
trinomix GmbH



Private Declare Function ExtEscape Lib "coredll.dll" ( _
ByVal hdc As Int32, _
ByVal nEscape As Int32, _
ByVal cbinput As Int32, _
ByVal lpszindata As Object, _
ByVal cbOutput As Int32, _
ByVal lpszOutData As Object _
) As Int32

Private Declare Function GetDC Lib "coredll.dll" ( _
ByVal hWnd As Int32 _
) As Int32

Private Declare Function ReleaseDC Lib "coredll.dll" ( _
ByVal hWnd As Int32, _
ByVal hDC As Int32 _
) As Int32

Private Declare Sub Sleep Lib "coredll.dll" ( _
ByVal dwMilliseconds As Int32 _
)

' GDI Escapes for ExtEscape()
Private Const QUERYESCSUPPORT As Int32 = 8

' The following are unique to CE
Private Const GETVFRAMEPHYSICAL As Int32 = 6144
Private Const GETVFRAMELEN As Int32 = 6145
Private Const DBGDRIVERSTAT As Int32 = 6146
Private Const SETPOWERMANAGEMENT As Int32 = 6147
Private Const GETPOWERMANAGEMENT As Int32 = 6148

Private Enum VIDEO_POWER_STATE As Long
VideoPowerOn = 1
VideoPowerStandBy = 2
VideoPowerSuspend = 3
VideoPowerOff = 4
End Enum

Private Structure VIDEO_POWER_MANAGEMENT
Dim Length As Int64
Dim DPMSVersion As Int64
Dim PowerState As Int64
End Structure

Public Sub TurnOffDisplay()

Dim gdc As Int32
Dim iESC As Int32 = SETPOWERMANAGEMENT

gdc = GetDC(Nothing)

If ExtEscape(gdc, QUERYESCSUPPORT, 4, iESC, 0, Nothing) = 0 Then
MsgBox("Sorry, your Pocket PC does not support DisplayOff", _
MsgBoxStyle.OKOnly, "Pocket PC Display Off Feature")
Else
Dim vpm As VIDEO_POWER_MANAGEMENT
vpm.Length = 24
vpm.DPMSVersion = 1
vpm.PowerState = VIDEO_POWER_STATE.VideoPowerOff
' Power off the display
ExtEscape(gdc, SETPOWERMANAGEMENT, 24, vpm, 0, Nothing)
Sleep(5000)
vpm.PowerState = VIDEO_POWER_STATE.VideoPowerOn
' Power on the display
ExtEscape(gdc, SETPOWERMANAGEMENT, 24, vpm, 0, Nothing)
ReleaseDC(Nothing, gdc)
End If

End Sub
 
Peter said:
I posted the C# code to do this here:-
http://www.opennetcf.org/forums/topic.asp?TOPIC_ID=774

You could either build it in a C# dll and call it from your VB code or
convert the code. You could try one of the automatic converters here
http://www.opennetcf.org/forums/topic.asp?TOPIC_ID=162 however they are
rarely flawless...

Peter

I already tried a byte array, but 1 byte too short and without
bitconverter. That's life... ;-)

Thank you very much!

Kind regards,

Benjamin Lukner
trinomix GmbH


Ok folks, here we go:


Private Declare Function ExtEscape Lib "coredll.dll" ( _
ByVal hdc As Int32, _
ByVal nEscape As Int32, _
ByVal cbinput As Int32, _
ByVal lpszindata() As Byte, _
ByVal cbOutput As Int32, _
ByVal lpszOutData() As Byte _
) As Int32

Private Declare Function GetDC Lib "coredll.dll" ( _
ByVal hWnd As Int32 _
) As Int32

Private Declare Function ReleaseDC Lib "coredll.dll" ( _
ByVal hWnd As Int32, _
ByVal hDC As Int32 _
) As Int32

Private Declare Sub Sleep Lib "coredll.dll" ( _
ByVal dwMilliseconds As Int32 _
)

' GDI Escapes for ExtEscape()
Private Const QUERYESCSUPPORT As Int32 = 8

' The following are unique to CE
Private Const GETVFRAMEPHYSICAL As Int32 = 6144
Private Const GETVFRAMELEN As Int32 = 6145
Private Const DBGDRIVERSTAT As Int32 = 6146
Private Const SETPOWERMANAGEMENT As Int32 = 6147
Private Const GETPOWERMANAGEMENT As Int32 = 6148

Private Enum VIDEO_POWER_STATE As Integer
VideoPowerOn = 1
VideoPowerStandBy = 2
VideoPowerSuspend = 3
VideoPowerOff = 4
End Enum

Public Sub TurnOffDisplay()

Dim gdc As Int32
Dim yEsc(4) As Byte

' Get display handle
gdc = GetDC(Nothing)

' Check if power off is possible
BitConverter.GetBytes(SETPOWERMANAGEMENT).CopyTo(yEsc, 0)
If ExtEscape(gdc, QUERYESCSUPPORT, yEsc.Length, yEsc, 0, Nothing _
) = 0 Then
MsgBox("Sorry, your Pocket PC does not support DisplayOff", _
MsgBoxStyle.OKOnly, "Pocket PC Display Off Feature")
Else

Dim iSize As Integer = 12
Dim vpm(iSize) As Byte

' structure size
BitConverter.GetBytes(iSize).CopyTo(vpm, 0)
' dpms version
BitConverter.GetBytes(CInt(1)).CopyTo(vpm, 4)
' power state
BitConverter.GetBytes(VIDEO_POWER_STATE.VideoPowerOff _
).CopyTo(vpm, 8)

' Power off the display
ExtEscape(gdc, SETPOWERMANAGEMENT, vpm.Length, vpm, 0, Nothing)

Sleep(5000)

' power state
BitConverter.GetBytes(VIDEO_POWER_STATE.VideoPowerOn _
).CopyTo(vpm, 8)

' Power on the display
ExtEscape(gdc, SETPOWERMANAGEMENT, vpm.Length, vpm, 0, Nothing)

ReleaseDC(Nothing, gdc)

End If

End Sub
 
Here there is the same code in VB.NET:

Public Class TurnDisplay
Private Declare Sub ReleaseDC Lib "coredll.dll"
(ByVal hWnd As IntPtr, ByVal hDC As IntPtr)

Private Declare Function ExtEscape
Lib "coredll.dll" (ByVal hDC As IntPtr, ByVal Esc As
Integer, ByVal cbInData As Integer, ByVal InData() As
Integer, ByVal cbOutData As Integer, ByVal OutData As
IntPtr) As IntPtr

Private Declare Function ExtEscape
Lib "coredll.dll" (ByVal hDC As IntPtr, ByVal Esc As
Integer, ByVal cbInData As Integer, ByRef InData As
Integer, ByVal cbOutData As Integer, ByVal OutData As
IntPtr) As IntPtr

Private Declare Function GetDC Lib "coredll.dll"
(ByVal hWnd As IntPtr) As IntPtr

Private Const SETPOWERMANAGEMENT As Integer = 6147
Private Const QUERYESCSUPPORT As Integer = 8

Enum VIDEO_POWER_STATE
VideoPowerOn = 1
VideoPowerStandBy
VideoPowerSuspend
VideoPowerOff
End Enum 'VIDEO_POWER_STATE

Private hDC As IntPtr = GetDC(IntPtr.Zero)
Private Esc As Integer = QUERYESCSUPPORT

Public Shared Function TurnOff(ByVal seconds As
Integer) As Boolean
Dim hDC As IntPtr = GetDC(IntPtr.Zero)
Dim Esc As Integer = QUERYESCSUPPORT
Dim hHash As IntPtr = IntPtr.Zero

Try
If Not hHash.Equals(ExtEscape(hDC,
QUERYESCSUPPORT, Marshal.SizeOf(Esc.GetType()), Esc, 0,
IntPtr.Zero)) Then
MessageBox.Show("This device does not
support display power on/off")
Else
Dim vpm(3) As Integer
vpm(0) = 3 * Marshal.SizeOf(GetType
(Integer))
vpm(1) = 1
vpm(2) = CInt
(VIDEO_POWER_STATE.VideoPowerOff)
ExtEscape(hDC, SETPOWERMANAGEMENT, vpm
(0), vpm, 0, IntPtr.Zero)
System.Threading.Thread.Sleep(seconds
* 1000)
vpm(2) = CInt
(VIDEO_POWER_STATE.VideoPowerOn)
ExtEscape(hDC, SETPOWERMANAGEMENT, vpm
(0), vpm, 0, IntPtr.Zero)
End If
ReleaseDC(IntPtr.Zero, hDC)
Return True

Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
End Class

Antares
 
Antares said:
Here there is the same code in VB.NET:

[...]

Thanks!

But I already appended the code in my posting from
2003-12-17 15:02 GMT+1 (after "Ok folks, here we go:") ;-)


Benjamin Lukner
 
Turn On / Off Phone In Vb.net

please, I need kwnow as turn off and turn on the phone in vb.net for a windows mobile, Can some body help me, please.
 
Back
Top