Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
来源:互联网 发布:翡翠台电视直播软件 编辑:程序博客网 时间:2024/04/30 07:50
由于电脑经常会关闭,或者应用程序也会经常关闭,但有一些参数是经常需要保存。比如当你打开程序,并设置了窗口的大小,想每次打开时都设置窗口为上次打开的大小。这样就需要保存窗口的大小,那么窗口大小的参数保存到那里呢?其实在Windows里最方便的做法,就是保存到注册表里。又比如游戏登录时,总是想保存最后一个登录的用户,那么也需要保存这个用户到注册表里。其实注册表是Windows保存系统配置的数据库,比如不同的语言设置,不同的时区设置,不同的用户登录,不同的权限等等。下面就来学习怎么样使用函数RegOpenKeyEx来打开注册表里的键和用函数RegCreateKeyEx来创建新的键。
函数RegOpenKeyEx和RegCreateKeyEx声明如下:
WINADVAPI
LONG
APIENTRY
RegOpenKeyExA (
__in HKEY hKey,
__in_opt LPCSTR lpSubKey,
__reserved DWORD ulOptions,
__in REGSAM samDesired,
__out PHKEY phkResult
);
WINADVAPI
LONG
APIENTRY
RegOpenKeyExW (
__in HKEY hKey,
__in_opt LPCWSTR lpSubKey,
__reserved DWORD ulOptions,
__in REGSAM samDesired,
__out PHKEY phkResult
);
#ifdef UNICODE
#define RegOpenKeyEx RegOpenKeyExW
#else
#define RegOpenKeyEx RegOpenKeyExA
#endif // !UNICODE
WINADVAPI
LONG
APIENTRY
RegCreateKeyExA (
__in HKEY hKey,
__in LPCSTR lpSubKey,
__reserved DWORD Reserved,
__in_opt LPSTR lpClass,
__in DWORD dwOptions,
__in REGSAM samDesired,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__out PHKEY phkResult,
__out_opt LPDWORD lpdwDisposition
);
WINADVAPI
LONG
APIENTRY
RegCreateKeyExW (
__in HKEY hKey,
__in LPCWSTR lpSubKey,
__reserved DWORD Reserved,
__in_opt LPWSTR lpClass,
__in DWORD dwOptions,
__in REGSAM samDesired,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__out PHKEY phkResult,
__out_opt LPDWORD lpdwDisposition
);
#ifdef UNICODE
#define RegCreateKeyEx RegCreateKeyExW
#else
#define RegCreateKeyEx RegCreateKeyExA
#endif // !UNICODE
hKey是主键。
lpSubKey是子键。
ulOptions是选项。
samDesired是键的操作。
phkResult是打开的键返回。
lpClass是新键值。
调用函数的例子如下:
#001 //打注册表。HKEY_CURRENT_USER/"Software"/"Wincpp"/"testreg"
#002 //蔡军生 2007/11/02 QQ:9073204 深圳
#003 HKEY GetAppRegistryKey(void)
#004 {
#005 HKEY hAppKey = NULL;
#006 HKEY hSoftKey = NULL;
#007 HKEY hCompanyKey = NULL;
#008
#009 //打开HKEY_CURRENT_USER/"Software"。
#010 if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ,
#011 &hSoftKey) == ERROR_SUCCESS)
#012 {
#013 DWORD dw;
#014 //创建并打开HKEY_CURRENT_USER/"Software"/"Wincpp"
#015 if (RegCreateKeyEx(hSoftKey, _T("Wincpp"), 0, REG_NONE,
#016 REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
#017 &hCompanyKey, &dw) == ERROR_SUCCESS)
#018 {
#019 //创建并打开HKEY_CURRENT_USER/"Software"/"Wincpp"/"testreg"
#020 RegCreateKeyEx(hCompanyKey, _T("testreg"), 0, REG_NONE,
#021 REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
#022 &hAppKey, &dw);
#023 }
#024 }
#025
#026 //关闭打开的键值。
#027 if (hSoftKey != NULL)
#028 {
#029 RegCloseKey(hSoftKey);
#030 }
#031
#032 if (hCompanyKey != NULL)
#033 {
#034 RegCloseKey(hCompanyKey);
#035 }
#036
#037 return hAppKey;
#038 }
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1864163
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Windows API一日一练(87)CreateProcess函数
- 如何做好项目软件的分析
- Windows API一日一练(79)GlobalMemoryStatusEx函数
- Windows API一日一练(74)GetSystemInfo函数
- Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数
- Why "权限不够“?
- Windows API一日一练(69)GetTickCount函数
- 关于软件的架构设计
- Windows API一日一练(76)GlobalAlloc函数
- Windows API一日一练(78)HeapAlloc函数
- 转换mp3名称的小程序
- Windows API一日一练(86)GetClipboardData函数
- Windows API一日一练(60)CreateIoCompletionPort和GetQueuedCompletionStatus函数