Windows CE 6.0 registry example and registry monitoring function

Source: Internet
Author: User
Tags builtin

Windows CE 6.0 has four basic registry key values: hkey_classes_root, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, and HKEY_USERS. The others are the subkeys of these four registries. Take a built-in serial port driver as an example. Its description in the Registry File platform. Reg is as follows:
[HKEY_LOCAL_MACHINE/Drivers/builtin/serial]
"Prefix" = "com"
"DLL" = "$ (_ tgtplat_pfx) _ serial. dll"
"Flags" = DWORD: 0
"Index" = DWORD: 1
Prefix and DLL are essential. index indicates the device serial number. prefix indicates the prefix of the device file name, the device name of the registry sub-key is "COM1:", which can be called by createfile. DLL is the name of the dynamic link library. If the value of flags is 1, the system is not loaded at startup. If the value of flags is 0, the driver is loaded at system startup.
There are two methods to operate the registry. Method 1: in the directory of % wince dir % public % common % oak % Inc %, the cregedit. h file defines a class cregistryedit to encapsulate registry operations. Another method is to use the APIS provided by Windows CE to perform registry key operations. The statement is as follows:
(1) Use the Registry class cregistryedit provided by the system
The class is defined in the Regedit. h file. In the build function, the handle of the current registry subkey is obtained. There are three constructors. The first one is constructed by calling hkey = opendevicekey (text ("HKEY_LOCAL_MACHINE/Drivers/builtin/serial") in the full path; the second is to call regopenkeyex (HKEY_LOCAL_MACHINE, text ("drivers // builtin // serial"), & hkey) if the parent registry subkey is known ). The third method is to use regcreatekeyex ().
In the destructor, call regclosekey (hkey) to disable the reference to the registry subkey. You can use the getregvalue method to read the registry key, and the regsetvalueex method to write the registry key. The implementation of this method is also through the Windows ce api. For details, refer to the next section.
(2) Use Windows CE API
Regcreatekeyex is a new registry key. If this key already exists, you can directly open the get handle. To obtain a registry key value, you must first call regopenkeyex. For example, regopenkeyex (HKEY_LOCAL_MACHINE, text ("drivers // builtin // serial"), 0, 0, & hkey ), the hkey is the handle of the registry subkey we obtained. Next, the operation on the registry subkey is implemented through the hkey.
With the hkey, you can read and write the content of the registry subkey. If you want to read the prefix of the sub-key, call the regqueryvalueex (hkey, text ("prefix") function, null, & lptype, & lpdata, & lpcbdata), lptype, lpdata is the returned type and value of prefix. In this example, lptype = REG_SZ and lpdata = text ("com "). The write operation is the opposite of a read operation. regsetvalueex (hkey, text ("prefix"), null, REG_SZ, pbyte (text ("TST ")), wcslen (text ("TST") * 2), where the item to be written is a prefix item, type: REG_SZ, value: Text ("TST "), the last parameter is the write size in bytes. Pay attention to the second parameter. If this item exists in the registry, rewrite its value. If no, a new registry key is created.
In some cases, we need to delete a registry key. You only need to call regdeletekey (hkey, text ("Index") to delete the index registry key. To disable a registry subkey, you only need to call regclosekey (hkey.

(3) Instances

Under the HKEY_CURRENT_USER Root Key, create the mysoftware // regtest key and write the person's name and author's age information under this key. The data types are string and integer respectively.

Hkey hopenkey;

DWORD dwopenstyle;

Long lresult = 0;

Lpctstr keyname = l "mysoftware // regtest"; // defines the key name

Lresult = regcreatekeyex (HKEY_CURRENT_USER, keyname, 0, l "",

0, 0, null, & hopenkey, & dwopenstyle); // open or create a specified key

Assert (lresult = error_srccess );

 

Lpctstr strkeyname = l "author"; // write key value, string type

Lpctstr strkeyvalue = l "";

Lresult = regsetvalueex (hopenkey, strkeyname, 0, REG_SZ, (byte *) strkeyvalue, wcslen (strkeyvalue) * 2 );

Assert (lresult = error_srccess );

 

Lpctstr dwkeyname = l "Age"; // write key value, integer type

DWORD dwkeyvalue = 25;

Lresult = regsetvalueex (hopenkey, dwkeyname, 0, REG_SZ, (byte *) dwkeyvalue, wcslen (dwkeyvalue) * 2 );

Assert (lresult = error_srccess );

 

Regclosekey (hopenkey );

If you want to read the value written above, the method is as follows:

DWORD dwkeyvaluetype = 0;

Tchar strkeyvalue [500];

Zeromemory (strkeyvalue, 500*2 );

Dwkeyvaluelength = 500*2;

Lresult = regqueryvalueex (hopenkey, strkeyname, 0, & dwkeyvaluetype, (byte *) strkeyvalue, & dwkeyvaluelength );

(4) registrypolicycallback, a Registry monitoring function, is used to monitor registry entries. Once a change occurs, a callback function is executed. For example, when we operate the control on the upper-layer application interface, it will be directly reflected in the registry, then the underlying registry monitoring callback function will be executed to control the driver for corresponding actions. The following uses a dynamic link library of the vibrator motor as an example.

# Define device_mis_file_name l "mis1:" // motor device
Handle hmisc = NULL; // motor handle

Bool g_bscreentapsenable = false; // enable and disable motor vibration

Bool g_bexited = false; // The control thread exits.
Void startvibrator () // start Vibration
{
If (hmisc = NULL)
{
Hmisc = createfile (device_mis_file_name, 0, 0, null, 0, 0, null );
}
If (hmisc)
{
Int isetvalue = 0;
DWORD dwbufsize = sizeof (INT );
DWORD dwretsize = 0;
Isetvalue = 1; // vibration, implemented through IOCTL
If (deviceiocontrol (hmisc, ioctl_mis_it_vibrator, & isetvalue, dwbufsize, null, 0, & dwretsize, null ))
{
Retailmsg (0, (text ("miccco_touch ioctl_mis_it_vibrator () Success/R/N "));
}
Else

{
Retailmsg (0, (text ("miccco_touch ioctl_mis_it_vibrator () failure/R/N ")));
}
}
}

Void stopvibrator () // stop the vibration
{
If (hmisc = NULL)
{
Hmisc = createfile (device_mis_file_name, 0, 0, null, 0, 0, null );
}
If (hmisc)
{
Int isetvalue = 0;
DWORD dwbufsize = sizeof (INT );
DWORD dwretsize = 0;
Isetvalue = 0; // close the vibration
If (deviceiocontrol (hmisc, ioctl_mis_it_vibrator, & isetvalue, dwbufsize, null, 0, & dwretsize, null ))
{
Retailmsg (1, (text ("miccco_touch ioctl_mis_it_vibrator () Success dwretsize/R/N "));
}
Else

{
Retailmsg (1, (text ("miccco_touch ioctl_mis_it_vibrator () failure/R/N ")));
}
}
}

DWORD winapi dotouchvibratethreadprc (lpvoid lpparameter) // The thread function created in dllmain
{
Cesetthreadpriority (getcurrentthread (), 100); // sets the thread priority.
While (1)
{
Waitforsingleobject (g_hvibratorevent, infinite); // wait for the click event to occur
If (g_bexited) // If the DLL is suspended, exit the loop

Break;
Startvibrator ();
Sleep (g_dwtime); // motor vibration time
Stopvibrator ();
}
Return 0;
}

Static void screentapscallbackfunc (hregpolicy hnotify, DWORD dwuserdata, const pbyte pdata, const uint cbdata) // callback function of the Registry monitoring function to handle Registry Changes
{
If (* pdata = 1)
{
G_bscreentapsenable = true; // enable the motor
}
Else
{
G_bscreentapsenable = false; // disable the motor
}
}

Bool dllmain (handle hinstdll, DWORD dwreason, lpvoid lpvreserved) // DLL Main Function
{
If (dwreason = dll_process_attach)
{
Hkey = NULL;
DWORD dwdisposition = 0, dwvalue;
DWORD dwsize = sizeof (DWORD );
If (error_success = regcreatekeyex (HKEY_CURRENT_USER, _ T ("controlpanel // touchvibration"), 0, null, 0, 0, null, & hkey, & dwdisposition ))) // create a key value
{
If (dwdisposition = reg_created_new_key) // you can check whether the key is a new one.
{
Dwvalue = 0;
G_bscreentapsenable = false; // initialize motor prohibited
Regsetvalueex (hkey, _ T ("screentaps"), 0, REG_DWORD, (byte *) & dwvalue, sizeof (DWORD ));
}

Else // if it is an enabled existing key value
{
If (error_success = regqueryvalueex (hkey, _ T ("screentaps"), 0, (uchar *) & (dwvalue), & dwsize) & dwvalue = 1) // enable or disable a motor based on the registry value
{
G_bscreentapsenable = true;
}

Else
{
G_bscreentapsenable = false;
}
}
Regclosekey (hkey );
}

 

If (error_success = regcreatekeyex (HKEY_CURRENT_USER, _ T ("controlpanel"), 0, null, 0, 0, null, & hkey, & dwdisposition )))
{
Registrypolicycallback (hkey, _ T ("touchvibration"), _ T ("screentaps"), screentapscallbackfunc, 0,
Null, & g_hregpolicy_screentaps); // registers a function that monitors certain key values.
Regclosekey (hkey );
}
G_bexited = false; // indicates the initialization exit flag.
G_hvibratorevent = createevent (null, false, false, null );
If (g_hvibratorevent)
{
Createthread (null, 0, dotouchvibratethreadprc, null, 0, null); // create a trigger flag and thread
}
Disablethreadlibrarycils (hmodule) hinstdll); // disable DLL reload
}

If (dwreason = dll_process_detach)

{
If (g_hregpolicy_screentaps)

{

Registryclosenotification (g_hregpolicy_screentaps); // cancel registry monitoring
G_hregpolicy_screentaps = NULL;

}
If (g_hvibratorevent)
{
G_bexited = true; // causes the thread to exit
Closehandle (g_hvibratorevent );
}
}
Return (true );

}

Bool dotouchvibrate (DWORD dwlength) // DLL exposure Function
{
If (g_bscreentapsenable) // The message obtained from the registry to determine whether the action is performed.
{
Resetevent (g_hvibratorevent); // prevents continuous vibrations. Clear the previous events before each vibration event.
Dwlength> 200? G_dwtime = 200: g_dwtime = dwlength;
Setevent (g_hvibratorevent );
}
Return (true );
}

 

Http://developer.51cto.com/art/200907/133698.htm ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.