关于注册表

sjd163 2003-05-17 06:11:56
谁能给一个简单的注册表子键,建造、读写程序。
...全文
100 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sjd163 2003-05-17
  • 打赏
  • 举报
回复
需要一个例子。使用RegCreatKeyExt创建子键。
allenhai1980 2003-05-17
  • 打赏
  • 举报
回复
RegCreateKeyEx :)
allenhai1980 2003-05-17
  • 打赏
  • 举报
回复
HKEY hKeyNew;
DWORD dwDisposition;
bool bRet = RegCreatKeyEx(HKEY_CURRENT_USER, "Software\\RegApp\\1.0",
0, "RegApp", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
NULL, &hKeyNew, &dwDisposition);
hKeyNew为所打开或创建键的句柄,dwDisposition为相应的返回信息。
然后就可以用hKeyNew来对注册表进行一些操作。最后关闭。
真的没什么难的。你可以在任意地方写上上述语句,在注册表中用regedit就能看到。

sjd163 2003-05-17
  • 打赏
  • 举报
回复
有不累的吗?
allenhai1980 2003-05-17
  • 打赏
  • 举报
回复
累。。。
allenhai1980 2003-05-17
  • 打赏
  • 举报
回复
HKEY hOpenedKey;
DWORD dwDisposition;
BOOL bRet = (::RegCreateKeyEx(hKey, lpszSubKey, 0,
(char*)lpszClass, REG_OPTION_NON_VOLATILE, REY_ALL_ACCESS, NULL, &hOpenedKey, &dwDisposition) == ERROR_SUCCESS );
if(bRet) ::RegCloseKey( hOpenedKey );

example:
HKEY hKeyNew;
RegCreatKeyEx(HKEY_CURRENT_SURE,
"Software\\RegApp\\1.0",
0,
"RegApp",
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hkeyNew,
&dwDisposition);

HKEY hKey;
const char *lpszSubKey;
RegOpenKeyEx(hKey, lpszSubKey, 0, KEY_ALL_ACCESS, &m_hKey);
//打开一个给定正确路径的键或子键。hKey 在这里是根键的意思比如:HKE_CLASSES_ROOT,lpSubKey是指子键的名称,ulOptions是指定值,
通常被定义为0,samDesired是指访问类型,不同的访问类型有不同的操作效果。
PhkResult是指通过RegOpenKeyEx()函数来的到注册表的句柄,在通过句柄来进行操作。

DWORD dwSize = (DWORD) nSize;
BOOL bRet = (::RegQueryValueEx( m_hKey, lpszValueName, NULL, NULL, (unsigned char *) lpReturnBuffer, &dwSize ) == ERROR_SUCCESS );
//这个函数在操作时候,是通过所需注册表的句柄来实现的。注册表的句柄由调用
RegOpenKeyEx()和RegCreateKeyEx()函数得到的。而这个函数是用来读取注册表
的一个已知的键值的。用这个函数可以得到所需键值的名称,并且返回一个数据
,数据长度和数据类型。但是在这之前必须先获得这个注册表的句柄,句柄是通过
RegOpenKeyEx()与RegCreateEx()函数来获得,当需要设置和修改时,用户调用这
个函数。但是有一点要注意的是,为了使该函数可以访问,必须使用KEY_SET_VALUE权限来打开键。(这个权限是在RegCreateEx()或RegOpenKeyEx()函数内设置的)
sjd163 2003-05-17
  • 打赏
  • 举报
回复
需要一个例子。使用RegCreatKeyExt创建子键。
allenhai1980 2003-05-17
  • 打赏
  • 举报
回复
//一种简单方式:
SetRegistryKey(_T("键名")); //本程序使用的键名。
WriteProfileInt("键名", "子键", 值); //写
GetProfileInt("键名", "子键", 0); //读
//以上操作针对HKEY_CURRENT_USER/SOFTWARE/键名
guoxiny 2003-05-17
  • 打赏
  • 举报
回复
如何列举注册表所有的项和值

//The following example demonstrates the use of the RegQueryInfoKey, RegEnumKeyEx, and RegEnumValue functions. The hKey parameter passed to each function is a handle to an open key. This key must be opened before the function call and closed afterward.
// QueryKey - Enumerates the subkeys of key, and the associated
// values, then copies the information about the keys and values
// into a pair of edit controls and list boxes.
// hDlg - Dialog box that contains the edit controls and list boxes.
// hKey - Key whose subkeys and values are to be enumerated.

void QueryKey(HWND hDlg, HANDLE hKey)
{
CHAR achKey[MAX_PATH];
CHAR achClass[MAX_PATH] = ""; // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time

DWORD i, j;
DWORD retCode, retValue;

CHAR achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
CHAR achBuff[80];

// Get the class name and the value count.
RegQueryInfoKey(hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time

SetDlgItemText(hDlg, IDE_CLASS, achClass);
SetDlgItemInt(hDlg, IDE_CVALUES, cValues, FALSE);

SendMessage(GetDlgItem(hDlg, IDL_LISTBOX),
LB_ADDSTRING, 0, (LONG) "..");

// Enumerate the child keys, until RegEnumKeyEx fails. Then
// get the name of each child key and copy it into the list box.

SetCursor(LoadCursor(NULL, IDC_WAIT));
for (i = 0, retCode = ERROR_SUCCESS;
retCode == ERROR_SUCCESS; i++)
{
retCode = RegEnumKeyEx(hKey,
i,
achKey,
MAX_PATH,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == (DWORD)ERROR_SUCCESS)
{
SendMessage(GetDlgItem(hDlg, IDL_LISTBOX),
LB_ADDSTRING, 0, (LONG) achKey);
}
}
SetCursor(LoadCursor (NULL, IDC_ARROW));

// Enumerate the key values.
SetCursor(LoadCursor(NULL, IDC_WAIT));

if (cValues)
{
for (j = 0, retValue = ERROR_SUCCESS;
j < cValues; j++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retValue = RegEnumValue(hKey, j, achValue,
&cchValue,
NULL,
NULL, // &dwType,
NULL, // &bData,
NULL); // &bcData

if (retValue == (DWORD) ERROR_SUCCESS )
{
achBuff[0] = '\0';

// Add each value to a list box.
if (!lstrlen(achValue))
lstrcpy(achValue, "<NO NAME>");
wsprintf(achBuff, "%d) %s ", j, achValue);
SendMessage(GetDlgItem(hDlg,IDL_LISTBOX2),
LB_ADDSTRING, 0, (LONG) achBuff);
}
}

SetCursor(LoadCursor(NULL, IDC_ARROW));
}
sjd163 2003-05-17
  • 打赏
  • 举报
回复
up

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧