Hi,
I'm using the the RIL_Initialize-Call from the stril.zip.
(The programm works well - thank you for your great work!!! ;o)
Now I'd tried to implement the RIL in an own project with more Classes and now, there's a problem:
I've got a class KKsmsIn. doRIL, resultCallback, notifyCallback (KKsmsIn::doRil a.s.o.) are implemented as public (tried also private). Whenn I trie to compile the programm then line
RIL_Initialize(1, resultCallback, notifyCallback...
generates an error: Cannot vonvert parameter 2 from 'void (unsingned long...' to 'void (__cdecl *)(insigned long...'
I'd implemented the RIL.h and RIL.lib, If I mark the resultCallback the description is 'void __cdecl resultCallback(...'
Pse help, think it's a little sign or command which is missing,
but I work since 12 hours on this line and have no idea, how the solution could be :-(
Greetings from hannovre,
andreas
PS: And now I'm going to donate some money ;o)
-> is there a chance do donate without a creditcard? *hope*
Try following....
it is part of my code :
Code:
CRadio g_radio;
//////////////////////////////////////////////////////////////////////
// Callback functions
//////////////////////////////////////////////////////////////////////
void CALLBACK NotifyCallback(DWORD dwCode, const void *lpData, DWORD cbData, DWORD dwParam)
{
g_radio.OnNotifyCallback(dwCode, lpData, cbData, dwParam);
}
void CALLBACK ResultCallback(DWORD dwCode, HRESULT hrCmdID, const void *lpData, DWORD cbData, DWORD dwParam)
{
g_radio.OnResultCallback(dwCode, hrCmdID, lpData, cbData, dwParam);
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRadio::CRadio()
{
dwParam = 0x55AA55AA;
dwNotificationClasses = 0xFF0000;
}
CRadio::~CRadio()
{
}
bool CRadio::InitializeRIL()
{
HRESULT result;
result = RIL_Initialize(1, ResultCallback, NotifyCallback, dwNotificationClasses, dwParam, &hRil);
return hRil!=NULL;
}
void CRadio::OnNotifyCallback(DWORD dwCode, const void *lpData, DWORD cbData, DWORD dwParam)
{
// ...
}
void CRadio::OnResultCallback(DWORD dwCode, HRESULT hrCmdID, const void *lpData, DWORD cbData, DWORD dwParam)
{
// ...
}
It works! Many thanks
Greetings from hannovre and anymore good speed,
andreas
you can do without the global variable, if you pas 'this' for the dwParam in RIL_Initialize, and then cast the dwParam in notify and result back to CRadio*.
Use at your own risk!
Code:
/*
HTC HD2 Compass interface
MilaCzeque (at) gmail.com
This program is licensed under the terms of the GNU General Public License (GPL).
*/
#include <windows.h>
#include <service.h>
struct compass_data
{
short x;
short y;
short z;
short angle;
short d;
short dummy[10]; //just to make sure nothing get overwritten
};
struct compass
{
compass_data data;
HANDLE h;
HANDLE service;
int init()
{
h = CreateFile( TEXT( "SEN0:" ), 0, 0, 0, 3, 0x80, 0 );
if( h == INVALID_HANDLE_VALUE || h == 0 )
{
h = 0;
service = RegisterService( TEXT( "SEN" ), 0, TEXT( "HTCSensorService.dll" ), 1 );
if( service == 0 )
{
return 0;
}
h = CreateFile( TEXT( "SEN0:" ), 0, 0, 0, 3, 0x80, 0 );
if( h == INVALID_HANDLE_VALUE || h == 0 )
{
h = 0;
return 0;
}
}
if( 0 == DeviceIoControl( h, 0x3000001, 0, 0, 0, 0, 0, 0 ))
{
CloseHandle( h );
h = 0;
return 0;
}
return true;
}
short update()
{
if( !h )
{
return 0;
}
DWORD read = 0;
if( 0 == DeviceIoControl( h, 0x3000003, 0, 0, &data, 0, &read, 0 ))
{
return 0;
}
else
{
return 1;
}
}
int deinit()
{
if( h )
{
if( 0 == DeviceIoControl( h, 0x3000002, 0, 0, 0, 0, 0, 0 ))
{
CloseHandle( h );
h = 0;
return 0;
}
}
return 1;
}
};
Edit:
Plese do mind that this code is licensed under GPL and so is anything based on it (including C# and VB code in this thread). This means that if you use it in your project you MUST release it under GPL (and with source code).
I'm sick of everyone ignoring this. :-(
Multitouch and the compass at a similar time?
The Leo is definately getting better!
Thanks!
Someone should inform the Layar developers about this.
maati:
Don't think that Layar would agree with GPL.
l3v5y:
It will be even better with FM Radio API working.
I had a play with this, the issue is, at current I haven't found exactly what the "angle" is. Do you know whether it's the angle between the device and North? or the bearing of the device?
It's the same number as you can see in original compass application. Angle between device and magnetic north.
MilaCzeque, thanks a lot the leo is indeed getting better and better!
I don't think you can help with this, but do you have any idea about how we might get hardware compass support in Google Maps, iGO, and other software?
I'd like to refer to this thread by the way. Thx!
Maybe someone can write program which will emulate COM port with nmea from internal gps but with heading from compass.
@MilaCzeque
Hi,
See here: http://forum.xda-developers.com/showthread.php?t=571266
Is it possible to see some sample code written in C# as well ?
Thank you very much.
Thanks!
Anyone managed to get into the proximity sensor?
Great!!!
How can this be used in C# or Vb.net ?
I dont develop in C++ for Windows Mobile.
retrieving capacitive sensor data similar. Hack touch.dll and get DeviceIOControl codes....
Can anybody translate that code in C#?
Better yet, let's make a .net library so its accessible to everyone! l3v5y?
shirreer said:
Better yet, let's make a .net library so its accessible to everyone! l3v5y?
Click to expand...
Click to collapse
That would be the best.
I tried this:
Code:
Imports System.Runtime.InteropServices
Public Class clsCompass
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As System.UInt32, ByVal dwShareMode As System.UInt32, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As System.UInt32, ByVal dwFlagsAndAttributes As System.UInt32, ByVal hTemplateFile As IntPtr) As Integer
End Function
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function RegisterService(ByVal lpszType As String, ByVal dwIndex As System.UInt32, ByVal lpszLib As String, ByVal dwInfo As System.UInt32)
End Function
<DllImport("coredll.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> Public Shared Function DeviceIoControl(<[In]()> ByVal hDevice As Integer, <[In]()> ByVal dwIoControlCode As Integer, <[In]()> ByVal lpInBuffer As IntPtr, <[In]()> ByVal nInBufferSize As Integer, <Out()> ByVal lpOutBuffer As IntPtr, <[In]()> ByVal nOutBufferSize As Integer, <Out()> ByRef lpBytesReturned As Integer, <[In]()> ByVal lpOverlapped As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Const DIO_INIT As Int32 = &H3000001
Const DIO_READ As Int32 = &H3000003
Const DIO_QUIT As Int32 = &H3000002
Const INVALID_HANDLE_VALUE As Int64 = -1
Structure compass_data
Public x As Short
Public y As Short
Public z As Short
Public angle As Short
Public d As Short
Public dummy As String
End Structure
Private devHandle As Integer
Private srvHandle As Integer
Public compassData As New compass_data
Public Function initCompass()
devHandle = CreateFile("SEN0:", 0, 0, 0, 3, &H80, 0)
If devHandle = INVALID_HANDLE_VALUE Or devHandle = 0 Then
devHandle = 0
srvHandle = RegisterService("SEN", 0, "HTCSensorService.dll", 1)
If srvHandle = 0 Then Return False
devHandle = CreateFile("SEN0:", 0, 0, 0, 3, &H80, 0)
If devHandle = INVALID_HANDLE_VALUE Or devHandle = 0 Then Return False
End If
If DeviceIoControl(devHandle, DIO_INIT, 0, 0, 0, 0, 0, 0) = 0 Then Return False
Return True
End Function
Public Function updateData()
Dim readBytes As System.UInt32
If devHandle = 0 Then Return False
If DeviceIoControl(devHandle, DIO_READ, 0, 0, compassData, 0, readBytes, 0) = 0 Then
Return False
Else
Return True
End If
End Function
Public Function shutdownCompass()
If devHandle <> 0 Then
If DeviceIoControl(devHandle, DIO_QUIT, 0, 0, 0, 0, 0, 0) = 0 Then
Return False
Else
Return True
End If
End If
Return True
End Function
End Class
However, this does not compile:
Error 2 Value of type 'GPSLib.clsCompass.compass_data' cannot be converted to 'System.IntPtr'. C:\Dokumente und Einstellungen\Marco\Eigene Dateien\Visual Studio 2008\Projects\GPSLib\GPSLib\clsCompass.vb 44 55 GPSLib
I have no idea how to convert this instance of a struct to a int pointer... any ideas??
Solved that one. Now during run w/ this code, I get a NotSupported exception in updateData():
Code:
Imports System.Runtime.InteropServices
Public Class clsCompass
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As System.UInt32, ByVal dwShareMode As System.UInt32, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As System.UInt32, ByVal dwFlagsAndAttributes As System.UInt32, ByVal hTemplateFile As IntPtr) As Integer
End Function
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function RegisterService(ByVal lpszType As String, ByVal dwIndex As System.UInt32, ByVal lpszLib As String, ByVal dwInfo As System.UInt32)
End Function
<DllImport("coredll.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> Public Shared Function DeviceIoControl(<[In]()> ByVal hDevice As Integer, <[In]()> ByVal dwIoControlCode As Integer, <[In]()> ByRef nInputBuffer As Integer, <[In]()> ByVal nInBufferSize As Integer, <Out()> ByVal lpOutBuffer As compass_data, <[In]()> ByVal nOutBufferSize As Integer, <Out()> ByRef lpBytesReturned As Integer, <[In]()> ByVal lpOverlapped As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("coredll.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> Public Shared Function DeviceIoControl(<[In]()> ByVal hDevice As Integer, <[In]()> ByVal dwIoControlCode As Integer, <[In]()> ByRef nInputBuffer As Integer, <[In]()> ByVal nInBufferSize As Integer, <Out()> ByVal lpOutBuffer As Integer, <[In]()> ByVal nOutBufferSize As Integer, <Out()> ByRef lpBytesReturned As Integer, <[In]()> ByVal lpOverlapped As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Const DIO_INIT As Int32 = &H3000001
Const DIO_READ As Int32 = &H3000003
Const DIO_QUIT As Int32 = &H3000002
Const INVALID_HANDLE_VALUE As Int64 = -1
Structure compass_data
Public x As Short
Public y As Short
Public z As Short
Public angle As Short
Public d As Short
Public dummy As String
End Structure
Overrides Function toString() As String
Return "X: " + compassData.x + vbNewLine _
+ "X: " + compassData.x + vbNewLine _
+ "Y: " + compassData.y + vbNewLine _
+ "Z: " + compassData.z + vbNewLine _
+ "a: " + compassData.angle + vbNewLine _
+ "d: " + compassData.d + vbNewLine
End Function
Private devHandle As Integer
Private srvHandle As Integer
Public compassData As New compass_data
Public Function initCompass()
devHandle = CreateFile("SEN0:", 0, 0, 0, 3, &H80, 0)
If devHandle = INVALID_HANDLE_VALUE Or devHandle = 0 Then
devHandle = 0
srvHandle = RegisterService("SEN", 0, "HTCSensorService.dll", 1)
If srvHandle = 0 Then Return False
devHandle = CreateFile("SEN0:", 0, 0, 0, 3, &H80, 0)
If devHandle = INVALID_HANDLE_VALUE Or devHandle = 0 Then Return False
End If
If DeviceIoControl(devHandle, DIO_INIT, 0, 0, 0, 0, 0, 0) = 0 Then Return False
Return True
End Function
Public Function updateData()
Dim readBytes As System.UInt32
If devHandle = 0 Then Return False
If DeviceIoControl(devHandle, DIO_READ, 0, 0, compassData, 0, readBytes, 0) = 0 Then
Return False
Else
Return True
End If
End Function
Public Function shutdownCompass()
If devHandle <> 0 Then
If DeviceIoControl(devHandle, DIO_QUIT, 0, 0, 0, 0, 0, 0) = 0 Then
Return False
Else
Return True
End If
End If
Return True
End Function
End Class
System.NotSupportedException was unhandled
Message="NotSupportedException"
StackTrace:
bei GPSLib.clsCompass.updateData() bei GPSLib.frmMain.Timer1_Tick(Object sender, EventArgs e) bei System.Windows.Forms.Timer._WnProc(WM wm, Int32 wParam, Int32 lParam) bei System.Windows.Forms.ApplicationThreadContext._InternalContextMessages(WM wm, Int32 wParam, Int32 lParam) bei Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) bei System.Windows.Forms.Application.Run(Form fm) bei GPSLib.frmMain.Main()
InnerException:
Finally finished
FINALLY!
I am not sure about the meaning of the data returned however.
Code:
Imports System.Runtime.InteropServices
Public Class clsCompass
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Public Shared Function CreateFile _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As IntPtr) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Shared Function RegisterService _
(ByVal lpszType As String, _
ByVal dwIndex As Integer, _
ByVal lpszLib As String, _
ByVal dwInfo As Integer) As Integer
End Function
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Shared Function LocalAlloc _
(ByVal uFlags As Integer, _
ByVal uBytes As Integer) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Shared Function LocalFree _
(ByVal hMem As IntPtr) As IntPtr
End Function
<DllImport("coredll.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
Public Shared Function DeviceIoControl _
(<[In]()> ByVal hDevice As IntPtr, _
<[In]()> ByVal dwIoControlCode As Integer, _
<[In]()> ByVal lpInputBuffer As IntPtr, _
<[In]()> ByVal nInBufferSize As Integer, _
<Out()> ByVal lpOutBuffer As IntPtr, _
<[In]()> ByVal nOutBufferSize As Integer, _
<Out()> ByRef lpBytesReturned As Integer, _
<[In]()> ByVal lpOverlapped As IntPtr) As Integer
End Function
Const DIO_INIT As Int32 = &H3000001
Const DIO_READ As Int32 = &H3000003
Const DIO_QUIT As Int32 = &H3000002
Const INVALID_HANDLE_VALUE As Int64 = -1
Const LMEM_FIXED = &H0
Const LMEM_ZEROINIT = &H40
Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)
Structure compass_data
Public x As Short
Public y As Short
Public z As Short
Public angle As Short
Public d As Short
Public dummy As String
End Structure
Public rdBuf(8) As Byte
Overrides Function toString() As String
Return _
"X: " + rdBuf(0).ToString + vbNewLine _
+ "Y: " + rdBuf(1).ToString + vbNewLine _
+ "Z: " + rdBuf(2).ToString + vbNewLine _
+ "a: " + rdBuf(3).ToString + vbNewLine _
+ "d: " + rdBuf(4).ToString + vbNewLine
End Function
Private devHandle As IntPtr
Private srvHandle As Integer
Public compassData As New compass_data
Public Function initCompass()
devHandle = CreateFile("SEN0:", 0, 0, 0, 3, &H80, 0)
If devHandle.ToInt32 = INVALID_HANDLE_VALUE Or devHandle.ToInt32 = 0 Then
devHandle = IntPtr.Zero
srvHandle = RegisterService("SEN", 0, "HTCSensorService.dll", 1)
If srvHandle = 0 Then Return False
devHandle = CreateFile("SEN0:", 0, 0, 0, 3, &H80, 0)
If devHandle.ToInt32 = INVALID_HANDLE_VALUE Or devHandle.ToInt32 = 0 Then Return False
End If
If DeviceIoControl(devHandle, DIO_INIT, 0, 0, 0, 0, 0, 0) = 0 Then Return False
Return True
End Function
Public Function updateData()
' Dim readBytes As Integer
Dim buf(8) As Byte
Dim result As Integer
Dim getMemory As New IntPtr()
If devHandle = 0 Or devHandle = IntPtr.Zero Then Return False
getMemory = LocalAlloc(LPTR, buf.Length)
Marshal.Copy(buf, 0, getMemory, buf.Length)
result = DeviceIoControl(devHandle, DIO_READ, IntPtr.Zero, 0, getMemory, buf.Length, 0, IntPtr.Zero)
Marshal.Copy(getMemory, buf, 0, buf.Length)
If getMemory <> IntPtr.Zero Then LocalFree(getMemory)
If result = 0 Then
Return False
Else
rdBuf = buf
Return True
End If
End Function
Public Function shutdownCompass()
If devHandle <> 0 Then
If DeviceIoControl(devHandle, DIO_QUIT, 0, 0, 0, 0, 0, 0) = 0 Then
Return False
Else
Return True
End If
End If
Return True
End Function
End Class
Almost there...
Mapping your buffer to short[5] gives me a fair compass heading in number 4. No idea what 1..3 are, they seem to react to the device's orientation (GSensor?)
Here's the C# code (convert it back to VB with Red-Gate's .NET Reflector if you wish):
using System;
using System.Runtime.InteropServices;
namespace Sensors
{
public interface ICompass
{
/// <summary>
/// Compass heading.
/// </summary>
double Angle { get;}
/// <summary>
/// Angle is valid
/// </summary>
bool IsValid { get;}
/// <summary>
/// Start compass sensor
/// </summary>
void Start();
/// <summary>
/// Stop compass sensor
/// </summary>
void Stop();
/// <summary>
/// Reset compass sensor
/// </summary>
void Reset();
}
public class HTCCompass:ICompass
{
private IntPtr devHandle;
private int srvHandle;
public short[] buffer = new short[5];
#region ICompass
public double Angle
{
get
{
// try to connect if necessary
if(!ready) ready = initCompass();
return ready && updateData() ? buffer[3] : -1.0;
}
}
bool ready = false;
public bool IsValid { get { return ready; } }
public void Start() { ready = initCompass(); }
public void Stop() { shutdownCompass(); ready = false; }
public void Reset() { Stop(); Start(); }
#endregion
private const int DIO_INIT = 0x3000001;
private const int DIO_QUIT = 0x3000002;
private const int DIO_READ = 0x3000003;
private const long INVALID_HANDLE_VALUE = -1L;
//private const int LMEM_FIXED = 0;
//private const int LMEM_ZEROINIT = 0x40;
//private const int LPTR = 0x40;
public bool initCompass()
{
if (this.devHandle != IntPtr.Zero)
return true;
this.devHandle = CreateFile("SEN0:", 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero);
if ((this.devHandle.ToInt32() == INVALID_HANDLE_VALUE) | (this.devHandle.ToInt32() == 0))
{
this.devHandle = IntPtr.Zero;
this.srvHandle = RegisterService("SEN", 0, "HTCSensorService.dll", 1);
if (this.srvHandle == 0)
return false;
this.devHandle = CreateFile("SEN0:", 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero);
if ((this.devHandle.ToInt32() == INVALID_HANDLE_VALUE) | (this.devHandle.ToInt32() == 0))
return false;
}
int read = 0;
if (DeviceIoControl(this.devHandle, DIO_INIT, IntPtr.Zero, 0, IntPtr.Zero, 0, out read, IntPtr.Zero) == 0)
return false;
return true;
}
public bool shutdownCompass()
{
if (this.devHandle != IntPtr.Zero)
{
int read = 0;
if (DeviceIoControl(this.devHandle, DIO_QUIT, IntPtr.Zero, 0, IntPtr.Zero, 0, out read, IntPtr.Zero) == 0)
return false;
this.devHandle = IntPtr.Zero;
}
return true;
}
public override string ToString()
{
return ("X: " + buffer[0].ToString() +
"\r\nY: " + buffer[1].ToString() +
"\r\nZ: " + buffer[2].ToString() +
"\r\nA: " + buffer[3].ToString() +
"\r\nS: " + buffer[4].ToString() +
"\r\n");
}
public bool updateData()
{
byte[] buf = new byte[10];
IntPtr getMemory = new IntPtr();
if ((this.devHandle == IntPtr.Zero) | (this.devHandle == IntPtr.Zero))
{
return false;
}
getMemory = LocalAlloc(0x40, buf.Length);
Marshal.Copy(buf, 0, getMemory, buf.Length);
int read = 0;
int result = DeviceIoControl(this.devHandle, DIO_READ, IntPtr.Zero, 0, getMemory, buf.Length, out read, IntPtr.Zero);
Marshal.Copy(getMemory, buf, 0, buf.Length);
if (getMemory != IntPtr.Zero)
{
LocalFree(getMemory);
}
if (result == 0)
{
return false;
}
Buffer.BlockCopy(buf, 0, buffer, 0, buf.Length);
return true;
}
# region PInvokes
[DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LocalAlloc(int uFlags, int uBytes);
[DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LocalFree(IntPtr hMem);
[DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RegisterService(string lpszType, int dwIndex, string lpszLib, int dwInfo);
[DllImport("coredll.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("coredll.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern int DeviceIoControl([In] IntPtr hDevice, [In] int dwIoControlCode, [In] IntPtr lpInputBuffer, [In] int nInBufferSize, [Out] IntPtr lpOutBuffer, [In] int nOutBufferSize, out int lpBytesReturned, [In] IntPtr lpOverlapped);
#endregion
}
}
Dear сolleagues and development guru!
I have some stopper in Power Management. I hope somebody will help me to understand.
Technologies: C#, .NET 3.5
The main point of application:
Make beep periodically.
Main problem is:
When PocketPC in active mode application running correct, but when PocketPC went to sleep mode it does not make a beep. As I understood speaker is turned off in sleep mode (like all other devices in PocketPC).
Question:
How can I turn on speaker in sleep mode?
Simply what I need is turn on speaker, make beep and back speaker to previous mode without wake up all device.
I tried code like this:
Code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Media;
using System.Threading;
using System.Runtime.InteropServices;
namespace TestProject
{
static class Program
{
public const int POWER_NAME = 0x00000001;
public enum DevicePowerState : int
{
Unspecified = -1,
D0 = 0,
D1,
D2,
D3,
D4,
}
[DllImport("coredll.dll", SetLastError = true)]
public static extern int SetDevicePower(
string pvDevice,
int dwDeviceFlags,
DevicePowerState DeviceState
);
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr SetPowerRequirement(
string device,
DevicePowerState state,
uint deviceFlags,
IntPtr systemState,
ulong stateFlags
);
[DllImport("coredll.dll", SetLastError = true)]
public static extern int ReleasePowerRequirement(IntPtr handle);
[MTAThread]
static void Main()
{
MessageBox.Show("Application is running...", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
string deviceName = "WAV1:"; // speaker
for (; ; )
{
// set D0 power value
IntPtr handle = SetPowerRequirement(deviceName, DevicePowerState.D0, 1, IntPtr.Zero, 0);
// make a beep
SystemSounds.Beep.Play();
// set default power value
ReleasePowerRequirement(handle);
Thread.Sleep(10000);
}
}
}
}
And like this:
Code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Media;
using System.Threading;
using System.Runtime.InteropServices;
namespace TestProject
{
static class Program
{
public const int POWER_NAME = 0x00000001;
public enum DevicePowerState : int
{
Unspecified = -1,
D0 = 0,
D1,
D2,
D3,
D4,
}
[DllImport("coredll.dll", SetLastError = true)]
public static extern int SetDevicePower(
string pvDevice,
int dwDeviceFlags,
DevicePowerState DeviceState
);
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr SetPowerRequirement(
string device,
DevicePowerState state,
uint deviceFlags,
IntPtr systemState,
ulong stateFlags
);
[DllImport("coredll.dll", SetLastError = true)]
public static extern int ReleasePowerRequirement(IntPtr handle);
[MTAThread]
static void Main()
{
MessageBox.Show("Application is running...", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
string deviceName = "WAV1:"; // speaker
int resDevicePowerOn = SetDevicePower(deviceName, POWER_NAME, DevicePowerState.D0);
for (; ; )
{
// make a beep
SystemSounds.Beep.Play();
Thread.Sleep(10000);
}
}
}
}
But any of these codes does not work - there are no beep in sleep mode.
Hi, I just happened to find an article from codeproject.
You may want to take a look at it.
http://www.codeproject.com/KB/mobile/WiMoPower1.aspx
I think what you need is cerunappattime.
It seems you are enabling the audio device to run in low-power mode, but you are not actually telling the CPU to keep running ("unattended mode").
Lookup PowerPolicyNotify, specifically PPN_UNATTENDEDMODE.
You may also need to periodically call SHIdleTimerResetEx and/or SystemIdleTimerReset to keep the device active in unattended mode or it will still truely suspend.
Prevent the system enter sleep mode
The system will stop all activitis when the system enter sleep mode.So you must prevent the system enter sleep mode.
The system sleep mode:
on -> unattended -> suspend
You can run:
on -> unattended -> resuming
my code:time up is system on
ex file:screenblack.rar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace ScreenBlack
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//取得系統狀態 Get system state
[DllImport("Coredll.dll", SetLastError = true)]
public static extern int GetSystemPowerState(StringBuilder PowerState, int length, ref int Flags);
//設定系統電源狀態
[DllImport("Coredll.dll")]
public static extern int SetSystemPowerState(int psState, int StateFlags, int Options);
public const int POWER_STATE_ON = 0x12010000;
public const int NULL = 0;
public const int POWER_FORCE = 0x00001000;
private void Form1_Load(object sender, EventArgs e)
{
//設定 timer1 的設定值 0.5 秒 Set timer1 activie in 0.5 second
timer1.Interval = 500;
//啟動定時器 Start timer1
timer1.Enabled = true;
}
//宣告變數判斷是否有改變 suspend 0:沒有改變 1:有改變
int change = 0;
//宣告變數判斷執行秒數 10
int second = 20;
private void timer1_Tick(object sender, EventArgs e)
{
//設定其標為 0
int flags = 0;
//取得系統狀態名稱 on、suspend、unattended、resuming
StringBuilder stb = new StringBuilder(260);
//取得系統狀態 Get Current system state
GetSystemPowerState(stb, stb.Capacity, ref flags);
//判斷目前系統狀態
if (flags.ToString() == "4194304")
{
//改變 unattended 為 resuming Chang unattended into resuming
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Power\State\Suspend", true);
reg.SetValue("Flags", "268435456", RegistryValueKind.DWord);
reg.Close();
//設定 reg 有改變
change = 1;
}
else if (flags.ToString() == "268435456")
{
//秒數減一
second--;
//判斷 second 是否等於零
if (second == 0)
{
//************
// your code
//************
//system on
SetSystemPowerState(NULL, POWER_STATE_ON, POWER_FORCE);
//設定秒數
second = 20;
}
}
else
{
//判斷是否有改變
if (change == 1)
{
//改變 unattended 為 resuming Chang unattended into resuming
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Power\State\Suspend", true);
reg.SetValue("Flags", "2097152", RegistryValueKind.DWord);
reg.Close();
//設定 reg 有改變
change = 0;
//設定秒數
second = 20;
}
}
}
}
}