Windows註冊表操作基礎代碼

來源:互聯網
上載者:User

Windows註冊表操作基礎代碼

 

    Windows下對註冊表進行操作使用的一段基礎代碼Reg.h:

#pragma once
#include<assert.h>
#include<windows.h>
class Reg
{
    HKEY hkey;
public:
    void open(HKEY root,char*subKey);//開啟註冊表鍵,不存在則建立
    void del(HKEY root,char*subKey);//刪除註冊表鍵
    void close();//關閉註冊表鍵
    void setValue(char*name,char*data);//設定註冊表值,不存在則建立
    void getValue(char*name,char*value);//擷取註冊表值
    void delValue(char*name);//刪除註冊表值
};

void Reg::open(HKEY root,char*subKey)
{
    long lret=RegCreateKeyEx(root,(LPCTSTR)subKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hkey,NULL);
    assert(lret==ERROR_SUCCESS);
}
void Reg::del(HKEY root,char*subKey)
{
    long lret=RegDeleteKey(root,(LPCTSTR)subKey);
    assert(lret==ERROR_SUCCESS);
}
void Reg::close()
{
    long lret=RegCloseKey(hkey);
    assert(lret==ERROR_SUCCESS);
}
void Reg::setValue(char*name,char*data)
{
    long lret=RegSetValueEx(hkey,(LPCTSTR)name,0,REG_SZ,(BYTE*)data,(DWORD)strlen(data));
    assert(lret==ERROR_SUCCESS);
}
void Reg::getValue(char*name,char*value)
{
    long lret=RegQueryValueEx(hkey,(LPCTSTR)name,0,(LPDWORD)REG_SZ,(BYTE*)value,(LPDWORD)strlen(value));
    assert(lret==ERROR_SUCCESS);
}
void Reg::delValue(char*name)
{
    long lret=RegDeleteValue(hkey,(LPCTSTR)name);
    assert(lret==ERROR_SUCCESS);
}

   對註冊表操作需要保證編譯器處於管理員權限,否則無法正常執行註冊表操作。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.