C# code to soft reset a Windows Mobile device
Here is the C# code to soft reset a Windows Mobile device (using P/Invoke)
[DllImport("coredll.dll", SetLastError = true)]
static extern int SetSystemPowerState(string psState, int StateFlags, int Options);
const int POWER_FORCE = 4096;
const int POWER_STATE_RESET = 0x00800000;
private void SoftReset()
{
SetSystemPowerState(null, POWER_STATE_RESET, POWER_FORCE);
}
How to get information about softwares installed on Windows Mobile devices?
Best method is to ask the configuration service provider (CSP).
Step 1: Add Reference to Microsoft.WindowsMobile.Configuration
Step 2: Add the following statements at the top of the c# code file.
using System.Xml;
using Microsoft.WindowsMobile.Configuration;
Step 3: Prepare a csp string (xml format) and pass it to ProcessConfiguration method of ConfigurationManager. Return value is the xml string which contains all currently installed softwares on the device.
private void ListInstalledSoftwares()
{
string cspString = "<wap-provisioningdoc><characteristic-query type=\"UnInstall\"></characteristic-query></wap-provisioningdoc>";
XmlDocument xmlResult = null;// Use CSP to get list of installed applications
try
{
XmlDocument configDoc = new XmlDocument();
configDoc.LoadXml(cspString);
xmlResult = ConfigurationManager.ProcessConfiguration(configDoc, false);
Debug.WriteLine(xmlResult.InnerXml);
}
catch (Exception ex)
{
Debug.WriteLine("Failed to get list of installed applications. CSP failure. [Exception: " + ex.Message + "]");
}
}
How to disable “Unsigned Prompt Policy” on Windows Mobile?
Many Windows Mobile (Pocket PC) devices ship with one-tier security configuration enabled. That means if an unsigned application is started, then the user is prompted whether to allow the unsigned application to run or not. If the user based on his/her judgment allows the application to run, the application runs in privileged mode whereby it can access all system APIs and protected registry keys.
Now this feature can be very annoying during development. So rather than signing the executable the developer can temporarily disable the Unsigned Prompt Policy by making some registry changes.
Steps to disable Unsigned Prompt Policy on Windows Mobile:
- Use Remote Registry Editor or a third-party tool such as PHM Registry Editor.
- Set the following registry value to 1 to disable Unsigned Prompt Policy. (Default value is 0).
; Unsigned Prompt Policy
[HKEY_LOCAL_MACHINE\Security\Policies\Policies]
"0000101a"=dword:1Note: Create the above registry entry if it does not already exist.
How to get process id and thread id from a Window Handle in .NET CF?
Specify the namespace for doing P/Invoke stuff i.e. calling Win32 API functions from managed code.
using System.Runtime.InteropServices;
GetWindowThreadProcessId Win32 function retrieves the identifiers of the process and thread that created the specified window.
Here is how we declare GetWindowThreadProcessId for use in managed code (c#).
[DllImport("coredll.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
Description:
- hWnd is the window handle
- lpdwProcessId stores the process identifier after the method returns
- return value of the function is the id of the thread that created the window
Calling GetWindowThreadProcessId via P/Invoke:
// Set the hWnd value below with window handle of your interest
IntPtr hWnd = this.Handle;
uint processid = 0;
uint threadid = GetWindowThreadProcessId((IntPtr)hWnd, out processid);
And there you go....
Simulating key presses in your program with keybd_event function
Out of need or curiosity you might want to know how to send key presses from your native application. The core of the solution involves calling keybd_event() function which has the following prototype.
VOID keybd_event(BYTE bVk, BYTE bScan, DWORD dwFlags, DWORD dwExtraInfo);
bVk is the virtual-key code of the character. bScan is the hardware scan code. dwFlags is an important field. If KEYEVENTF_KEYUP is specified in dwFlags field, the key is being released. If not specified, the key is being pressed.
Here is a function which simulates pressing of alphanumeric characters. See winuser.h for all available virtual key codes.
void SendStringKeys(char* pszChars)
{
while (*pszChars != NULL)
{
if ( (*pszChars >= 'A') && (*pszChars <= 'Z') )
keybd_event(VK_SHIFT, 0, KEYEVENTF_SILENT, 0);keybd_event(toupper(*pszChars), 0, KEYEVENTF_SILENT, 0);
keybd_event(toupper(*pszChars), 0, KEYEVENTF_KEYUP|KEYEVENTF_SILENT, 0);if ( (*pszChars >= 'A') && (*pszChars <= 'Z') )
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP|KEYEVENTF_SILENT, 0);pszChars++;
}
}
And here is an example of how to call SendStringKeys function.
int _tmain(int argc, _TCHAR* argv[])
{
SendStringKeys("Hello World.");
return 0;
}