• windows 注册表编程


    例子:将本地计算机的Monitor ID写入到注册表中

     (1)获取MonitorID

       

    BOOLEAN DeviceMonitorService::EnumClassDevice(const GUID * InterfaceClassGuid)
    {
    
        WriteEventLogEntry(L"EnumClassDevice ",
            EVENTLOG_ERROR_TYPE);
        HDEVINFO DeviceInfoSet;
        HDEVINFO NewDeviceInfoSet;
    
        SP_DEVINFO_DATA DeviceInfoData;
        ZeroMemory(&DeviceInfoData, sizeof(DeviceInfoData));
        DeviceInfoData.cbSize = sizeof(DeviceInfoData);
        DeviceInfoData.DevInst = 0;
        DeviceInfoData.ClassGuid = *InterfaceClassGuid;
        DeviceInfoData.Reserved = 0;
    
        DWORD i;
        // 创建空设备信息列表     
        DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL);
        BOOLEAN ret = FALSE;
        if (DeviceInfoSet == INVALID_HANDLE_VALUE)
        {
            writeErrorLog(L"CreateDeviceInfoList failed: ", GetLastError());
            return ret;
        }
    
        // 根据接口类型获得新的设备信息列表     
    
        NewDeviceInfoSet = SetupDiGetClassDevsEx(
            InterfaceClassGuid,
            NULL,
            NULL,
            DIGCF_PRESENT | DIGCF_DEVICEINTERFACE,
            DeviceInfoSet,// 之前创建的设备信息列表     
            NULL,
            NULL
            );
        if (NewDeviceInfoSet == INVALID_HANDLE_VALUE)
        {
            writeErrorLog(L"SetupDiGetClassDevsEx failed: ", GetLastError());
            return ret;        
        }
    
        for (i = 0;; i++)
        {
            BOOL bResult = SetupDiEnumDeviceInfo(NewDeviceInfoSet,
                i, &DeviceInfoData);
            if (!bResult)
            {
                if (GetLastError() != NO_ERROR &&GetLastError() != ERROR_NO_MORE_ITEMS)
                {
                    writeErrorLog(L"SetupDiEnumDeviceInfo ERROR  ", GetLastError());
                    return ret;
                }
                break;
            }
            else
            {
                char DeviceID[MAX_DEV_LEN];
                bool resHardwareID = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, 0, (PBYTE)DeviceID, MAX_DEV_LEN, NULL);
                if (!resHardwareID)
                {
                    ZeroMemory(&DeviceID, sizeof(DeviceID));
                }
                else{
                    //写入注册表,并break;
                    setRegKey(DeviceID, name_monitor);
                }
            }
        }
        SetupDiDestroyDeviceInfoList(DeviceInfoSet);
        return TRUE;
    }

     (2)将MonitorID写入到注册表

    LPCTSTR SubKey = L"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

    void setRegKey(char* value, LPCWSTR name){
        //将Ansi字符串转换成Unicode字符串
    	LPWSTR pwszOut = NULL;// Typedef WCHAR* LPWSTR
    	if (value != NULL)
    	{
    		// Double NULL Termination
    		int nOutputStrLen = MultiByteToWideChar(CP_ACP, 0, value, -1, NULL, 0);//获取字符的长度
    		pwszOut = new TCHAR[nOutputStrLen]; 
    
    		memset(pwszOut, 0, nOutputStrLen);
    		MultiByteToWideChar(CP_ACP, 0, value, -1, pwszOut, nOutputStrLen);
    	}
    
    	HKEY key = NULL;
    	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKey, 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS){//打开注册表项
    		writeDebugEventLog(L"[Service]   RegOpenKeyEx  ok ", EVENTLOG_INFORMATION_TYPE);
    		if (RegSetValueEx(key, name, 0, REG_SZ, (unsigned const char*)pwszOut, sizeof(TCHAR)*(lstrlenW(pwszOut) + 1)) == ERROR_SUCCESS){
    			writeDebugEventLog(L"[Service]   setRegKey  ok ", EVENTLOG_INFORMATION_TYPE);
    		}
    		else{
    			writeErrorLog(L"[Service]   RegSetValueEx    ", GetLastError());
    		}
    	}
    	else {
    		writeErrorLog(L"[Service]   RegOpenKeyEx     ", GetLastError());
    	}
    	if (key != NULL)	RegCloseKey(key);
    
    	if (pwszOut != NULL) delete[] pwszOut;
    }
    

      

     (3)从注册表中读取MonitorID

    void readRegKey(LPCWSTR name,LPWSTR id){
    	HKEY key;
    	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKey, 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY, &key) == ERROR_SUCCESS){
    
    		DWORD nType, nLen;
    		WCHAR buff[MAX_DEV_LEN]; 
    		RegQueryValueEx(key, name, 0, &nType, (LPBYTE)buff, &nLen);
    		writeDebugEventLog(L"[MonitorHelp]   readRegKey  RegQueryValueEx ok ", EVENTLOG_INFORMATION_TYPE);
    		RegCloseKey(key);
    		lstrcpyW(id,buff);
    	}
    	else{
    		printf("readRegKey    RegOpenKeyEx fail");
    		writeDebugEventLog(L"[MonitorHelp]   readRegKey  RegQueryValueEx fail ", EVENTLOG_INFORMATION_TYPE);
    	}
    }
    

      

  • 相关阅读:
    DOS批处理高级教程(三) : 批处理变量和set命令详解
    DOS批处理高级教程(二) DOS循环: 语句命令FOR、IF
    DOS批处理高级教程(一) 批处理基础
    win7下部署个人网站教程
    Ubuntu安装后常见部署
    python3 生成钻石展位后台报表记录
    The Zen of Python
    4刀最多切割一个正方体为多少部分
    Python基础讲义第二弹面向对象编程(淘宝平台模拟为例)
    python基础讲义第一弹
  • 原文地址:https://www.cnblogs.com/meizixiong/p/4474935.html
Copyright © 2020-2023  润新知