Log in

View Full Version : How to write your own "shutdown" application


rave
05-13-2003, 04:24 AM
Here is a simple guide on how to create your own "Shutdown" application. I wrote this out of annoyance towards commercial suspend and reset applications, which in my opinion cost too much for their worth in source code. I also noticed that out of the ten thousand shutdown apps out there, nothing is what I prefer them to be: a free app that both suspends and resets according to a choice in a menu, and with a window that quickly pops up without any of those slow special effects. Besides, who wouldn't want complete control on how their application behaves? :)

My source for this is the eVT help file. All the info you need is there; all I did was to just consolidate them.

Suspend:

To turn off your Pocket PC via software, you need to simulate pressing the power button. You can do that using these API commands:

keybd_event(VK_OFF,0,KEYEVENTF_SILENT,0);

keybd_event(VK_OFF,0,KEYEVENTF_SILENT | KEYEVENTF_KEYUP,0);

sleep(60);

Both keybd_event() and sleep() reside in Coredll.dll. If you are coding in e-Visual Basic, you need to "declare" that you are using some functions in this DLL. Put these at the beginning of your code:

Declare Sub keybd_event Lib "coredll" (ByVal bVk As Integer, ByVal bScan As Integer, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Declare Sub Sleep Lib "coredll" (ByVal dwMilliseconds As Long)

Now you can call these functions within eVB. Here's how I coded them:

Call keybd_event(223, 0, 4, 0)

Call keybd_event(223, 0, 6, 0)

Call Sleep(60)

Simply put these three statements in a subroutine, and you can now call that subroutine to put your device to sleep. You may also want to put an App.End statement after these statements to exit the application upon suspending.

Now, you would notice that I've hard coded some numbers. These are simply the pre-defined constants. 223 is VK_OFF, 4 is KEYEVENTF_SILENT, and 6 is [KEYEVENTF_SILENT | KEYEVENTF_KEYUP]. The latter is simply a bitwise XOR between KEYEVENTF_SILENT (which is 4) and KEYEVENTF_KEYUP (which is 2). Hehehe, I know that hardcoding constants is bad practice, but I did it anyway since the app is small and I'm the only one who's gonna use it. ;)

The inclusion of the Sleep() function is best explained by the help file: "The sleep statement allows time for the system to suspend and prevents your application from executing further after you have called for the Suspend to occur."

Shutdown:

Soft resetting your PPC requires only one call to a function in Coredll.dll. Here's my eVB code for it:

Declare Function KernelIoControl Lib "coredll" (ByVal dwIoControlCode As Long, ByVal lpInBuf As String, ByVal nInBufSize As Long, lpOutBuf As String, ByVal lnOutBufferSize As Long, ByVal lpBytesReturned As Long) As Boolean

Dim bReset As Boolean

Dim lValue As Long

bReset = KernelIoControl(16842812, "", 0, "", 0, lValue)

16842812 is also known as the predefined constant, IOCTRL_HAL_REBOOT. It's obviously the control code that causes the system to reboot. bReset and lValue are simply dummy variables that blindly accept values returned by KernelIoControl().

Note that KernelIoControl is device independent. It's just an interface between the kernel and any OEM-specific device drivers. Its main purpose is to get processor information, but apparently can perform other functions as well.

User Interface
I leave that part up to you. Some users want three confirmations before resetting, while some do not. Code according to your preference.

er... Why did you code in eVB instead of eVC?
Because I don't know how to use eVC (yet). ;) I figured that instead of coding this at a turtle's pace in eVC, it's better to just get it over with using eVB since it's just a small application. It works for me since the eVB runtimes are already in my device's ROM. Besides, I'd rather spend my effort applying my L337 Photoshop SkillZ.

Okay! I'll just copy this code verbatim, compile, then run!
Feel free to do so. But make sure to have a recent backup handy. This worked for me, but I can't say that for all machines.

-----

Anyway, that's that. Should you have any questions or corrections, I'd be happy to read your replies. :)

Pony99CA
05-13-2003, 07:50 AM
Here is a simple guide on how to create your own "Shutdown" application. I wrote this out of annoyance towards commercial suspend and reset applications, which in my opinion cost too much for their worth in source code. I also noticed that out of the ten thousand shutdown apps out there, nothing is what I prefer them to be: a free app that both suspends and resets according to a choice in a menu, and with a window that quickly pops up without any of those slow special effects. Besides, who wouldn't want complete control on how their application behaves? :)
It's always good to see programming information, but what didn't you like about Philippe Majerus' free Task Manager (http://www.phm.lu/Products/PocketPC/taskmgr.asp)?

Steve

rave
05-13-2003, 03:46 PM
Actually, I wanted something like Michael Schwarz' free Shutdown application, minus the full 1-second delay before the menu shows up. I know that the fade-to-grey effect is nice to look at, but it isn't practical. So I did some quick coding in eVB to get what I wanted. Then I posted my findings here out of elation. Oh, if only you could see my L337 UI. :D

Regarding the PHM Task Manager, I chose not to install it for two reasons: first, there is some code in it that can cause a hard reset, and second, the software is considered as beta. That's not really a good combination of attributes for a program to be installed on your device. Besides, it's a bit overkill for a shutdown app. I have mine linked to an icBar button.

Going back to the full-blown task manager, I'd actually recommend this instead: http://www.citadeldevelopment.com/products/pockettools.asp

Pony99CA
05-13-2003, 11:29 PM
Going back to the full-blown task manager, I'd actually recommend this instead:
http://www.citadeldevelopment.com/products/pockettools.asp
I've had Pocket Tools installed on my iPAQ 3870 since I got it. It is so much more than just a task manager, though. :-)

Steve