WinCE registry value monitor - c#

Evening all,
I need some advice on monitoring a registry value in WinCE. I am writing a Windows Forms application which needs to monitor a value in registry, and fire an event when it changes, can anyone point me in the right direction as to how to accomplish this?
Many thanks in advance.

I managed to implement this using PInvoke code:
[DllImport("coredll.dll", SetLastError = true)]
static extern int RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out UIntPtr phkResult);
[DllImport("coredll.dll", SetLastError = true)]
static extern UIntPtr CeFindFirstRegChange(UIntPtr hKey, [In, MarshalAs(UnmanagedType.Bool)] bool bWatchSubtree, uint dwNotifyFilter);
[DllImport("coredll.dll", SetLastError = true)]
public static extern UInt32 WaitForSingleObject(UIntPtr Handle, UInt32 Wait);
[DllImport("coredll.dll", SetLastError = true)]
static extern Int32 CeFindNextRegChange(UIntPtr hChangeHandle);
[DllImport("coredll.dll", SetLastError = true)]
static extern Int32 CeFindCloseRegChange(UIntPtr hChangeHandle);
[DllImport("coredll.dll", SetLastError = true)]
public static extern int RegCloseKey(UIntPtr hKey);
and by using WaitForSingleObject.

Related

Programmatically listing all supported locales in Windows 10

Windows 10 supports custom locales, locales that do not have an LCID like older locales from older versions of Windows. In each update to Windows 10, Microsoft has been adding lesser used locales for minority and indigenous languages.
A recent update to Windows 10 added the the Võro kiil (vro) locale. When using EnumSystemLocalesEx to enumerate all supported locales, vro does not appear in any form. However, in the system settings UI for adding a new language or keyboard, Võro kiil does appear.
However, if the user then enables this language, when you call EnumSystemLocalesEx, vro, vro-Latn and vro-Latn-001 are now listed. If the user then removes this locale from the UI, it no longer appears in the results of this function call.
The question: is there a way (supported or otherwise) to get a list of all the known locales to the operating system regardless of whether the user has enabled them or not?
I find it very bizarre that this output includes other minority languages like Skolt Sami without requiring the user to enable it in advance.
I will quite happily accept an answer that uses the .NET framework if the API does not exist in the C/C++ APIs, so long as I can actually get this data.
Example code to generate the locale output:
#include <cstdio>
#include "stdafx.h"
#include "windows.h"
BOOL CALLBACK Callback(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
wprintf(L"%ls\n", pStr);
return TRUE;
}
int main()
{
EnumSystemLocalesEx(Callback, 0, 0, 0);
return 0;
}
With Võro kiil enabled from the "Region and Language" screen in System Settings, the final three results are vro-Latn, vro-Latn-001 and vro. When not enabled, they do not appear in the output at all.
Using .NET APIs seems to have the same behaviour.
#include "stdafx.h"
using namespace System;
using namespace System::Globalization;
int main()
{
System::Collections::IEnumerator^ enum0 = CultureInfo::GetCultures(CultureTypes::AllCultures)->GetEnumerator();
while (enum0->MoveNext())
{
CultureInfo^ ci = safe_cast<CultureInfo^>(enum0->Current);
Console::WriteLine("{0}", ci->Name);
}
}
In effect, the new language model for Windows means that there is no "list" beyond those that have historical LCIDs.
The Windows 8.1 and 10 settings tools link to bcp47langs.dll and winlangdb.dll, which provide functions for enabling languages and input methods so long as the provided input is a valid ISO 639-3 language code.
In some cases, if you want your language to appear in the UI or via these APIs, you must provide at least the script and sometimes the region. An example is myv-Cyrl for the Erzya language.
Using these APIs
Using an MSIL disassembler on a cmdlet bundled with PowerShell, I found a p/invoke definition that has allowed me to successfully use these APIs from C# and Rust code.
Here it is, for posterity:
// Decompiled with JetBrains decompiler
// Type: Microsoft.InternationalSettings.Commands.LPAPIWrapper
// Assembly: Microsoft.InternationalSettings.Commands, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: E0B49792-544F-4FBD-8C35-D4DA177385AF
// Assembly location: C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.InternationalSettings.Commands\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.InternationalSettings.Commands.dll
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Microsoft.InternationalSettings.Commands
{
internal class LPAPIWrapper
{
public static uint GEO_NATION = 1;
public static uint GEO_FRIENDLYNAME = 8;
public static uint GEOCLASS_NATION = 16;
public static uint GEOCLASS_REGION = 14;
[DllImport("kernelbase.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int NlsUpdateLocale(string LocaleName, int Flags);
[DllImport("intl.cpl", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int IntlUpdateSystemLocale(string LocaleName, int dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SystemParametersInfo(
uint Action,
uint UnsignedParam,
IntPtr Param,
uint WinIni);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SendNotifyMessage(
IntPtr wWwnd,
uint Msg,
IntPtr wParam,
string lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetSystemDefaultLocaleName(
StringBuilder LocaleName,
int LocaleNameSize);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetUserGeoID(uint GeoClass);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetUserGeoID(int GeoId);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetGeoInfo(
int Location,
uint GeoType,
StringBuilder GeoData,
int Length,
ushort LangID);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetUserLanguages(char Delimiter, [MarshalAs(UnmanagedType.HString)] ref string UserLanguages);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetUserLanguageInputMethods(
string Language,
char Delimiter,
[MarshalAs(UnmanagedType.HString)] ref string InputMethods);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int LcidFromBcp47([MarshalAs(UnmanagedType.HString)] string LanguageTag, ref int Lcid);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetPendingUserDisplayLanguage([MarshalAs(UnmanagedType.HString)] ref string language);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetUserDisplayLanguageOverride([MarshalAs(UnmanagedType.HString)] ref string language);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetUserDisplayLanguageOverride(string LanguageTag);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int ClearUserDisplayLanguageOverride();
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetHttpAcceptLanguageOptOut(ref bool IsOptOut);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetHttpAcceptLanguageOptOut();
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int ClearHttpAcceptLanguageOptOut();
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetUserLocaleFromLanguageProfileOptOut(ref bool IsOptOut);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetUserLocaleFromLanguageProfileOptOut();
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int ClearUserLocaleFromLanguageProfileOptOut();
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int RemoveInputsForAllLanguagesInternal();
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetInputMethodOverride(string TipString);
[DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int Bcp47GetIsoLanguageCode([MarshalAs(UnmanagedType.HString)] string languageTag, [MarshalAs(UnmanagedType.HString)] ref string isoLanguageCode);
[DllImport("ext-ms-win-globalization-input-l1-1-2.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int WGIGetDefaultInputMethodForLanguage(
[MarshalAs(UnmanagedType.HString)] string Language,
[MarshalAs(UnmanagedType.HString)] ref string DefaultTipString);
[DllImport("ext-ms-win-globalization-input-l1-1-2.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int WGITransformInputMethodsForLanguage(
[MarshalAs(UnmanagedType.HString)] string TipString,
[MarshalAs(UnmanagedType.HString)] string Language,
[MarshalAs(UnmanagedType.HString)] ref string TransformedTipString);
[DllImport("winlangdb.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetUserLanguages(char Delimiter, [MarshalAs(UnmanagedType.HString)] string UserLanguages);
[DllImport("winlangdb.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetLanguageNames(
string Language,
StringBuilder Autonym,
StringBuilder EnglishName,
StringBuilder LocalName,
StringBuilder ScriptName);
[DllImport("ext-ms-win-globalization-input-l1-1-2.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int WGIIsImeInputMethod([MarshalAs(UnmanagedType.HString)] string TipString, ref int result);
[DllImport("winlangdb.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int EnsureLanguageProfileExists();
[DllImport("input.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int InstallLayoutOrTip(string TipString, int Flags);
[DllImport("input.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetDefaultLayoutOrTip(string TipString, int Flags);
[DllImport("input.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetLayoutDescription(
string LayoutId,
StringBuilder LayoutDescription,
ref int DescriptionLength);
private LPAPIWrapper()
{
}
}
}

How can I get a list of processes running across multiple virtual desktops?

I am working on Window 7 and I have multiple desktops by using the Sysinternals Desktops utility. I just want to list all the processes running across all the desktops.
Process[] processlist = Process.GetProcesses();
foreach (Process p in processlist)
{
listBox1.Items.Add(p.ProcessName + " " + p.Id + " " + p.MainWindowTitle);
}
With this code I am able to get all the processes running on the current desktop, but I am not able to get the processes running inside different desktops.
Only Desktops is showing as a single process.
How can I get the child processes inside multiple desktops?
Yup i got it.. after doing RnD.
I am using user32.dll and some of these methods.
If you also want to achieve some thing use these functions.
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int _GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool EnumDesktops(IntPtr hwinsta, EnumDesktopProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = false)]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetProcessWindowStation();
[DllImport("user32.dll")]
private static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll")]
private static extern bool SwitchDesktop(IntPtr hDesktop);
By using these methods I am able to list down all the running processes on all the virtual desktop.

Laptop/Tablet BatteryCycleCount for Determining its Health

Please help me to get Laptop/Tablet "BatteryCycleCount" value. Based on this property we will determine the replacement of battery.
Below are some APIs, which i have found in my googling:
[DllImport("setupapi.dll", SetLastError = true)]
protected static extern IntPtr SetupDiGetClassDevs(ref Guid gClass, [MarshalAs(UnmanagedType.LPStr)] string strEnumerator, IntPtr hParent, uint nFlags);
[DllImport("setupapi.dll", SetLastError = true)]
protected static extern bool SetupDiEnumDeviceInterfaces(IntPtr lpDeviceInfoSet, uint nDeviceInfoData, ref Guid gClass, uint nIndex, ref SP_DEVICE_INTERFACE_DATA oInterfaceData);
[DllImport("setupapi.dll", SetLastError = true)]
protected static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr lpDeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA oInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA oDetailData, uint nDeviceInterfaceDetailDataSize, ref uint nRequiredSize, IntPtr lpDeviceInfoData);
[DllImport("kernel32.dll", SetLastError = true)]
protected static extern IntPtr CreateFile([MarshalAs(UnmanagedType.LPStr)] string strName, uint nAccess, uint nShareMode, IntPtr lpSecurity, uint nCreationFlags, uint nAttributes, IntPtr lpTemplate);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool DeviceIoControl([In] SafeHandle hDevice, [In] int dwIoControlCode, [In] IntPtr lpInBuffer, [In] int nInBufferSize, [Out] IntPtr lpOutBuffer, [In] int nOutBufferSize, out int lpBytesReturned, [In] IntPtr lpOverlapped);
Instead of direct use of the Windows API, you can request the performance counters in the category "BatteryStatus".
Edit :
The counters in the category "BatteryStatus" are multi instance : one per battery. On my laptop, I can write this C# code :
var counter = new PerformanceCounter("BatteryStatus", "RemainingCapacity", "ACPI\PNP0C0A\0_0", true);
var remainingCapacity = counter.NextValue();
You can view all the available counters in the category "BatteryStatus" with Performance Monitor ("perfmon" command).

How to filter keyboard inputs using C# or VB.Net, Everywhere when application is running?

How to filter inputs from keyboard in all windows applications while our written code is running and lives in notification area
it's like: user opens our application, now when typing in for example MS Word, when he types "s" it is said in our code to filter "s" and change it to "M" so if he press "s" on keyboard, "M" is typed in MS Word document.
is it possible at all?
You should use low-level hook:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
And do what you want in your own hook implementation.

Wrapper C# for kernel32.dll API

Any helper class anywhere which wrapps kernel32 APIs, with all functions-methods and structures? Or any wrapper generator?
I want ALL methods of kernel32.dll in C# like this:
[DllImport("kernel32.dll",EntryPoint="RtlMoveMemory")]
public static extern void RtlMoveMemory(int des, int src, int count);
[DllImport("kernel32.dll", EntryPoint = "OpenProcess")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
[DllImport("kernel32", CharSet = CharSet.Ansi)]
public extern static int GetProcAddress(int hwnd, string procedureName);
[DllImport("kernel32.dll", EntryPoint = "GetModuleHandle")]
public static extern int GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll", EntryPoint = "VirtualAllocEx")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32", EntryPoint = "CreateRemoteThread")]
public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);
[DllImport("kernel32.dll", EntryPoint = "WriteProcessMemory")]
public static extern IntPtr WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, uint size, IntPtr lpNumberOfBytesWritten);
I doubt it.
Have you seen http://www.pinvoke.net/?

Categories