📄 regmani.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
//******************************************************************************
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//******************************************************************************
/*++
Module Name:
Regmani.cpp
Abstract:
This file implements the a registry manipulation class
--*/
#include <windows.h>
#include <types.h>
#include <ceddk.h>
#include "regmani.h"
//---------------------------------------------------------------------------------------
// Open a key by the given registry path
//---------------------------------------------------------------------------------------
BOOL
CRegManipulate::OpenKey(LPCTSTR szPath, PHKEY phKey){
if(szPath == NULL || phKey == NULL)
return FALSE;
if (RegOpenKeyEx( m_hMainKey, szPath, 0, 0, phKey) != ERROR_SUCCESS) {
*phKey = NULL;
return FALSE;
}
return TRUE;
}
//---------------------------------------------------------------------------------------
// Verify a registry key does exist
//---------------------------------------------------------------------------------------
BOOL
CRegManipulate::IsAKeyValidate(LPCTSTR szPath){
if(szPath == NULL)
return FALSE;
HKEY hKey;
//try to open this key
if (RegOpenKeyEx( m_hMainKey, szPath, 0, 0, &hKey) != ERROR_SUCCESS) {
return FALSE;
}
RegCloseKey(hKey);
return TRUE;
}
//---------------------------------------------------------------------------------------
//retrieve one value item according to the index value
//---------------------------------------------------------------------------------------
LONG
CRegManipulate::EnumAValue(HKEY hKey, DWORD dwIndex, PREG_KEY_INFO pKInfo){
LONG retVal = ERROR_SUCCESS;
TCHAR szValName[128];
BYTE tempData[128];
DWORD dwValLen = 128;
DWORD dwType, dwDataLen = 128;
if(dwIndex == 0){//first one, so clear pKInfo stucture
pKInfo->uItems = 0;
pKInfo->uBitmap = 0;
}
//query this value
retVal = RegEnumValue( hKey,
dwIndex,
szValName,
&dwValLen,
NULL,
&dwType,
tempData,
&dwDataLen);
if(retVal != ERROR_SUCCESS){//sth. wrong
if(retVal == ERROR_INVALID_DATA)//it could caused by mui_sz:, just ignore it here
return ERROR_SUCCESS;
else//otherwise something is really wrong
return retVal;
}
//store this value
if(StoreAnItem(pKInfo, szValName, dwValLen, dwIndex, dwType, tempData, dwDataLen) == FALSE)
return ERROR_INVALID_DATA;
else
return ERROR_SUCCESS;
}
//---------------------------------------------------------------------------------------
//store a value item into data structure
//---------------------------------------------------------------------------------------
BOOL
CRegManipulate::StoreAnItem(PREG_KEY_INFO pKeyInfo, LPCTSTR szValName, DWORD dwNameLen, DWORD dwIndex, DWORD dwType, PBYTE pData, DWORD dwLen){
if(pKeyInfo == NULL || szValName == NULL || dwNameLen == 0|| pData == NULL || dwLen == 0)
return FALSE;
switch(dwType){
case REG_BINARY: //binary data
memcpy((PBYTE)&(pKeyInfo->u[dwIndex].binData), pData, dwLen);
pKeyInfo->dwSize[dwIndex] = dwLen;
break;
case REG_DWORD: //dword data
memcpy((PBYTE)&(pKeyInfo->u[dwIndex].dwData), pData, sizeof(DWORD));
pKeyInfo->dwSize[dwIndex] = dwLen;
break;
case REG_SZ: //string
memcpy((PBYTE)(pKeyInfo->u[dwIndex].szData), pData, dwLen);
pKeyInfo->dwSize[dwIndex] = dwLen;
break;
case REG_MULTI_SZ: //multiple-string
memcpy((PBYTE)(pKeyInfo->u[dwIndex].szData), pData, dwLen);
pKeyInfo->dwSize[dwIndex] = dwLen;
break;
default: //unknow type
return FALSE;
}
memcpy((PBYTE)(pKeyInfo->szValName[dwIndex]), szValName, dwNameLen*sizeof(TCHAR));
pKeyInfo->dwType[dwIndex] = dwType;
//set bitmap
pKeyInfo->uBitmap |= 1<<pKeyInfo->uItems;
pKeyInfo->uItems ++;
return TRUE;
}
//---------------------------------------------------------------------------------------
//convert multi_sz to dword list
//---------------------------------------------------------------------------------------
BOOL
CRegManipulate::MultiSZtoDataList(PDWORD pdwDataList, PBYTE pData, DWORD dwLen){
if(pdwDataList == NULL || pData == NULL || dwLen == 0)
return FALSE;
PWCHAR szPT = (PWCHAR)pData;
PWCHAR endP;
DWORD dwNum = 0;
for(UINT i = 0; i < REG_MULTIPLE_DATA_NUM; i ++){
if (*szPT == (WCHAR)'\0') //end
break;
//convert one data
pdwDataList[i+1] = wcstoul(szPT, &endP, 16);
if (szPT == endP) //got nothing
break;
dwNum++;
//move to next substring
szPT = ++endP;
}
pdwDataList[0] = dwNum;
return TRUE;
}
//---------------------------------------------------------------------------------------
//Write a value item in data structure to registry
//---------------------------------------------------------------------------------------
BOOL
CRegManipulate::SetAnItem(HKEY hKey, LPCTSTR szValName, DWORD dwType, U_ITEM u, DWORD dwSize){
if(hKey == NULL || szValName == NULL)
return FALSE;
if(dwSize < 0 || dwSize > REG_STRING_LENGTH)
return FALSE;
switch(dwType){
case REG_BINARY:
if(RegSetValueEx(hKey, szValName, 0, REG_BINARY, u.binData, dwSize) != ERROR_SUCCESS){
NKMSG(_T("Can not set binary data for item %s"), szValName);
return FALSE;
}
break;
case REG_SZ:
if(RegSetValueEx(hKey, szValName, 0, REG_SZ, (PBYTE)(u.szData), dwSize) != ERROR_SUCCESS){
NKMSG(_T("Can not set string data for item %s"), szValName);
return FALSE;
}
break;
case REG_DWORD:
if(SetSingleVal(hKey, szValName, u.dwData) == FALSE){
NKMSG(_T("Can not set dword data for item %s"), szValName);
return FALSE;
}
break;
case REG_MULTI_SZ:
if(RegSetValueEx(hKey, szValName, 0, REG_MULTI_SZ, (PBYTE)(u.szData), dwSize) != ERROR_SUCCESS){
NKMSG(_T("Can not set string data for item %s"), szValName);
return FALSE;
}
break;
default:
return TRUE;
}
return TRUE;
}
//---------------------------------------------------------------------------------------
//retrieve a reg key's info into the structure
//---------------------------------------------------------------------------------------
BOOL CRegManipulate::GetAKey(PREG_KEY_INFO pKeyInfo){
if(pKeyInfo == NULL)
return FALSE;
HKEY hCurKey = NULL;
if(OpenKey(pKeyInfo->szRegPath, &hCurKey) == FALSE)
return FALSE;
DWORD dwIndex = 0;
LONG ret = ERROR_SUCCESS;
//retrieve each value item
while((ret = EnumAValue(hCurKey, dwIndex, pKeyInfo)) == ERROR_SUCCESS)
dwIndex ++;
if(ret != ERROR_NO_MORE_ITEMS){ //failed
NKMSG(_T("enumerate key %s failed!"), pKeyInfo->szRegPath);
RegCloseKey(hCurKey);
return FALSE;
}
RegCloseKey(hCurKey);
return TRUE;
}
//---------------------------------------------------------------------------------------
//create a key in registry
//---------------------------------------------------------------------------------------
BOOL CRegManipulate::SetAKey(PREG_KEY_INFO pKeyInfo, BOOL bNew){
if(pKeyInfo == NULL)
return FALSE;
HKEY hCurKey = NULL;
DWORD dwDispostion = 0;
LONG retVal = ERROR_SUCCESS;
//this key already exists, delete it if needed
BOOL bOldkeyExist = FALSE;
if(OpenKey(pKeyInfo->szRegPath, &hCurKey) == TRUE){
if(bNew == TRUE){//we require to create a fresh new key
RegCloseKey(hCurKey);
hCurKey = NULL;
if(RegDeleteKey(m_hMainKey, pKeyInfo->szRegPath) != ERROR_SUCCESS){
NKMSG(_T("Can not delete key %s"), pKeyInfo->szRegPath);
return FALSE;
}
}
else{
bOldkeyExist = TRUE;
}
}
//try creating this key
if(bOldkeyExist == FALSE){
retVal = RegCreateKeyEx(m_hMainKey,
pKeyInfo->szRegPath,
0, NULL, 0, 0, NULL,
&hCurKey,
&dwDispostion);
if(retVal != ERROR_SUCCESS){
NKMSG(_T("Can not create key %s"), pKeyInfo->szRegPath);
return FALSE;
}
}
UINT uItemVa = 0;
BOOL bRet;
UINT uBits = pKeyInfo->uBitmap;
for(UINT i = 0; i < pKeyInfo->uItems; i++){
while(!(uBits & 1) ){
uBits = uBits >> 1;
uItemVa ++;
if(uItemVa >= REG_ITEM_NUM) {//sth. wrong
RegCloseKey(hCurKey);
NKMSG(_T("Item number does not match the bitmap!"));
return FALSE;
}
}
//set each value item
bRet = SetAnItem(hCurKey,
pKeyInfo->szValName[uItemVa],
pKeyInfo->dwType[uItemVa],
pKeyInfo->u[uItemVa],
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -