应用程序中对注册表的编辑

来源:互联网 发布:2017双十一成交数据 编辑:程序博客网 时间:2024/05/19 02:45

在WinCE中的应用程序如需对注册表编辑,可使用如下几个函数:

Programming element Description RegCloseKey This function releases the handle of the specified key. RegCreateKeyEx This function creates the specified key. If the key already exists in the registry, the function opens it. RegDeleteKey This function deletes a named subkey from the specified registry key. RegDeleteValue This function removes a named value from the specified registry key. RegEnumKeyEx This function enumerates subkeys of the specified open registry key. RegEnumValue This function enumerates the values for the specified open registry key. RegFlushKey This function writes all the attributes of the specified open registry key into the registry. RegNotifyChangeKeyValue This function notifies the caller about changes to the attributes or contents of a specified registry key. RegOpenKeyEx This function opens the specified key. RegQueryInfoKey This function retrieves information about a specified registry key. RegQueryValueEx This function retrieves the type and data for a specified value name associated with an open registry key. RegSetValueEx This function stores data in the value field of an open registry key.

 

我们重点使用RegOpenKeyEx,RegSetValueEx,RegQueryValueEx和RegCloseKey这几个函数,其功能如上面所介绍。对一个注册表操作,需先用RegOpenKeyEx打开,再用RegSetValueEx,RegQueryValueEx查询或设置,最后用RegCloseKey关闭。

下面是一个对注册表设置的例子:

void RegSwitch(LPCWSTR ImKey, LPCWSTR ImIconKey, 
        WCHAR * Im240X320, WCHAR * Im320X240, WCHAR * ImIcon)
{
    LONG lret;
    HKEY hKEY;

    if(GetSystemMetrics(SM_CYSCREEN) > 240)
    {
        lret = RegOpenKeyEx(RESKEY,ImKey,0,KEY_READ,&hKEY);
        if(lret==ERROR_SUCCESS) 
        {
            RegSetValueEx(hKEY,VALUENAME,0,REG_SZ,
                    (LPBYTE)Im240X320,
                    sizeof(WCHAR)*(wcslen(Im240X320)+1));

            lret = RegOpenKeyEx(RESKEY,ImIconKey,0,KEY_READ,&hKEY);
            if(lret==ERROR_SUCCESS) 
            {
                RegSetValueEx(hKEY,VALUENAME,0,REG_SZ,
                        (LPBYTE)ImIcon,
                        sizeof(WCHAR)*(wcslen(ImIcon)+1));
            }
        }
        RegCloseKey(hKEY);
    }
    else
    {
        lret = RegOpenKeyEx(RESKEY,ImKey,0,KEY_READ,&hKEY);
        if(lret==ERROR_SUCCESS) 
        {
            RegSetValueEx(hKEY,VALUENAME,0,REG_SZ,
                    (LPBYTE)Im320X240,
                    sizeof(WCHAR)*(wcslen(Im320X240)+1));

            lret = RegOpenKeyEx(RESKEY,ImIconKey,0,KEY_READ,&hKEY);
            if(lret==ERROR_SUCCESS) 
            {
                RegSetValueEx(hKEY,VALUENAME,0,REG_SZ,
                        (LPBYTE)ImIcon,
                        sizeof(WCHAR)*(wcslen(ImIcon)+1));
            }
        }
        RegCloseKey(hKEY);
    }
}

下面是一个对注册表查询的例子,该例子实现了查询注册表中的屏幕旋转键值,并使屏幕旋转:

#include "stdafx.h"
#include "windows.h"

int WINAPI WinMain(HINSTANCE hInstance,
                HINSTANCE hPrevInstance,
                LPTSTR     lpCmdLine,
                int       nCmdShow)
{
    DEVMODE devMode;
    LONG lret;
    LONG regStatus;
    HKEY hKEY;
    INT keyValue;
    LPCWSTR subKey = L"System//GDI//Rotation";
    LPCWSTR valueName = L"Angle";
    DWORD pRegType,pRegKey,pRegKeySize;

    lret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,subKey,0,KEY_READ,&hKEY);

    if(lret==ERROR_SUCCESS) 
    {
        regStatus = RegQueryValueEx(hKEY,valueName,0,&pRegType,(LPBYTE)&pRegKey,&pRegKeySize);
        if(regStatus == ERROR_SUCCESS && pRegType == REG_DWORD)
        {
            memset(&devMode,0x00,sizeof(devMode));
            devMode.dmSize = sizeof(devMode);
            devMode.dmFields = DM_DISPLAYORIENTATION;
            ChangeDisplaySettingsEx(NULL,&devMode,NULL,CDS_TEST,NULL);

            keyValue = (INT)pRegKey;

            switch(keyValue)
            {
                case 90:
                    devMode.dmDisplayOrientation = DMDO_90;
                    break;
                case 180:
                    devMode.dmDisplayOrientation = DMDO_180;
                    break;
                case 270:
                    devMode.dmDisplayOrientation = DMDO_270;
                    break;
                default:
                    devMode.dmDisplayOrientation = DMDO_0;
                    break;
            }

            devMode.dmFields = DM_DISPLAYORIENTATION;

            if (DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettingsEx(NULL,

                    &devMode, NULL, CDS_RESET, NULL)) 
            { 
                RETAILMSG(1, (L"ChangeDisplaySettingsEx changed rotation angle to %d", keyValue)); 
            } 
            else 
            { 
                RETAILMSG(1, (L"ChangeDisplaySettingsEx failed to change the rotation angle to %d", keyValue)); 
            }
        }
        RegCloseKey(hKEY);
    }

    return 0;
}

 函数中的参数具体意义可以参考PB自带的帮助。

 
原创粉丝点击