#include <iostream> #include <assert.h> #include <windows.h> #include <tchar.h> #include <conio.h> #include <stdio.h> using namespace std; void wcharTochar(const wchar_t* wchar, char* chr, int length) { WideCharToMultiByte(CP_ACP, 0, wchar, -1, chr, length, NULL, NULL); } bool OpenRegKey(HKEY& hRetKey) { LPCWSTR sw = _T("SOFTWARE\Classes\FeikuaBrowserFile.myp\DefaultIcon"); wprintf(L"SW is %s ", sw); if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, sw, &hRetKey)) { return true; } printf("OpenRegKey return is false! "); return false; } bool QueryRegKey(LPCWSTR strSubKey, LPCWSTR strValueName, char* strValue, int length)//这里是传4个参数 { DWORD dwType = REG_SZ;//定义数据类型 DWORD dwLen = MAX_PATH; wchar_t data[MAX_PATH]; HKEY hKey; HKEY hSubKey; if (OpenRegKey(hKey)) { if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, strSubKey, &hSubKey)) { TCHAR buf[256] = { 0 }; if (ERROR_SUCCESS == RegQueryValueEx(hSubKey, strValueName, 0, &dwType, (LPBYTE)data, &dwLen)) { wcharTochar(data, strValue, length); wprintf(L"data = %s,len= %d ", data, strlen((const char*)data)); RegCloseKey(hKey); //关闭注册表 return true; } } RegCloseKey(hKey); //关闭注册表 } return false; } int main() { HKEY hKey = NULL; string result; LPCWSTR strSubKey = _T("SOFTWARE\Classes\FeikuaBrowserFile.myp\DefaultIcon"); LPCWSTR strValueName = _T(""); char strValue[256]; int length = 256; bool status = QueryRegKey(strSubKey, strValueName, strValue, length); printf("status is %d ", status); printf("result is %s ", strValue); return 0; }
默认值按空串处理