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

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

Programming elementDescription
RegCloseKeyThis function releases the handle of the specified key.
RegCreateKeyExThis function creates the specified key. If the key already exists in the registry, the function opens it.
RegDeleteKeyThis function deletes a named subkey from the specified registry key.
RegDeleteValueThis function removes a named value from the specified registry key.
RegEnumKeyExThis function enumerates subkeys of the specified open registry key.
RegEnumValueThis function enumerates the values for the specified open registry key.
RegFlushKeyThis function writes all the attributes of the specified open registry key into the registry.
RegNotifyChangeKeyValueThis function notifies the caller about changes to the attributes or contents of a specified registry key.
RegOpenKeyExThis function opens the specified key.
RegQueryInfoKeyThis function retrieves information about a specified registry key.
RegQueryValueExThis function retrieves the type and data for a specified value name associated with an open registry key.
RegSetValueExThis 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自带的帮助。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值