Windows API一日一练(63)RegOpenKeyEx和RegCreateKeyEx函数

<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>
由于电脑经常会关闭,或者应用程序也会经常关闭,但有一些参数是经常需要保存。比如当你打开程序,并设置了窗口的大小,想每次打开时都设置窗口为上次打开的大小。这样就需要保存窗口的大小,那么窗口大小的参数保存到那里呢?其实在 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 RegOpenKeyExRegOpenKeyExW
#else
#define RegOpenKeyExRegOpenKeyExA
#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 RegCreateKeyExRegCreateKeyExW
#else
#define RegCreateKeyExRegCreateKeyExA
#endif // !UNICODE
hKey 是主键。
lpSubKey 是子键。
ulOptions 是选项。
samDesired 是键的操作。
phkResult 是打开的键返回。
lpClass 是新键值。
调用函数的例子如下:
#001// 打注册表。 HKEY_CURRENT_USER\"Software"\"Wincpp"\"testreg"
#002// 蔡军生 2007/11/02 QQ:9073204 深圳
#003HKEY 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}



你可能感兴趣的:(游戏,windows,qq)