使用Windows API读注册表

//读取文本值 bool ReadString(HKEY key, CStringW valueName, CStringW &value) { DWORD type; BYTE s[_MAX_PATH]; //#define _MAX_PATH 260 /* max. length of full pathname */ DWORD len = _MAX_PATH; if (RegQueryValueEx(key, valueName, 0, &type, s, &len)==ERROR_SUCCESS) { value = (TCHAR*)s; return true; } return false; } //读取bool值 bool ReadBoolean(HKEY key, CStringW valueName, bool &value) { CStringW s; //先读取文本值 if (ReadString(key, valueName, s)) { //判断其是否为true value = (CStringW("true").CompareNoCase(s) == 0); return true; } return false; } //读取DWORD值 bool ReadDWORD(HKEY key, CStringW valueName, DWORD &value) { DWORD type; DWORD s; DWORD len = sizeof(DWORD); if (RegQueryValueEx(key, valueName, 0, &type, (LPBYTE)&s, &len)==ERROR_SUCCESS) { value = s; return true; } return false; } void ReadProps() { HKEY key; //打开键 RegOpenKey(HKEY_LOCAL_MACHINE,_T("Software//chain//cpp"), &key); CStringW people; DWORD age; bool IsMale; ReadString (key, _T("people"), people); ReadDWORD (key, _T("age"),age); ReadBoolean(key, _T("IsMale"), IsMale); RegCloseKey(key); _tprintf(_T("用户:%s/r/n年龄:%d/r/n性别:%s/r/n"), people, age, IsMale ? _T("男") : _T("女")); } int main() { setlocale(LC_ALL, "chs"); ReadProps(); return 0; } 

你可能感兴趣的:(API/MFC)