MFC中用c 語言對登錄檔操作(寫 開機自啟)

2021-08-10 09:43:17 字數 3984 閱讀 9260

一.開啟vs2015建立新專案->mfc應用程式->基於對話方塊

二.在對話方塊中新增兩個button按鈕,分別為:寫登錄檔、設定開機自啟

三.雙擊「寫登錄檔」進入**編寫,新增**如下:

void cregistrydlg::onbnclickedbutton2()

if (error_success != regsetvalue(hkey, nullptr, reg_sz, text("hi"), 2))

else

}

四.新增autostart函式並編寫如下**:

bool cregistrydlg::autostart()

;//定義乙個緩衝區,max_path定義了編譯器所支援的最長全路徑名的長度

getmodulefilename(null, pathstr, max_path);

hkey hsoftkey;

regopenkey(hkey_current_user, _t("software\\microsoft\\windows\\currentversion\\run"), &hsoftkey);//開啟乙個現有的登錄檔項

int len = _tcslen(pathstr);//判斷字串是否在字元數量上超過指定的長度

regsetkeyvalue(hsoftkey, 0, _t("測試自啟動"), reg_sz, pathstr, len);//在指定的登錄檔項中和子項中設定指定值的資料

byte content[max_path] = ;

dword ret = 0;

dword dwtype = reg_sz;

dword type2 = 0;

dword size;

ret = reggetvalue(hkey_current_user, _t("software\\microsoft\\windows\\currentversion\\run"), _t("測試自啟動"), rrf_rt_reg_sz, &type2, null, &size);

ret = reggetvalue(hkey_current_user, _t("software\\microsoft\\windows\\currentversion\\run"), _t("測試自啟動"), rrf_rt_reg_sz, &type2, content, &size);//檢索指定的登錄檔值的型別和資料

regclosekey(hsoftkey);//釋放指定註冊鍵的控制代碼

return

0;}

注意:autostart為自己新增函式,在類中需要宣告一下。為什麼連續讀兩次,看下部落格五.雙擊「設定開機自啟」進入**編寫,呼叫autostart函式,新增**如下:

void cregistrydlg::onbnclickedbutton3()

else

}

*重要函式:getmodulefilename//獲取當前程序已載入模組的檔案的完整路徑,該模組必須由當前程序載入。

語法:

dword winapi getmodulefilename(

_in_opt_ hmodule hmodule,

_out_ lptstr lpfilename,

_in_ dword nsize

);

引數:

hmodule long

乙個模組的控制代碼。可以是乙個dll模組,或者是乙個應用程式的例項控制代碼。如果該引數為null,

該函式返回該應用程式全路徑。

lpfilename string

指定乙個字串緩衝區,要在其中容納檔案的用null字元中止的路徑名,hmodule模組就是從這個檔案裝載進來的

nsize long

裝載到緩衝區lpfilename的最大字元數量

*重要函式:reggetvalue//檢索指定的登錄檔值的型別和資料

語法:

long winapi reggetvalue(

_in_ hkey hkey,

_in_opt_ lpctstr lpsubkey,

_in_opt_ lpctstr lpvalue,

_in_opt_ dword dwflags,

_out_opt_ lpdword pdwtype,

_out_opt_ pvoid pvdata,

_inout_opt_ lpdword pcbdata

);

詳細引數參考:*重要函式:regsetkeyvalue//在指定的登錄檔項和子項中設定指定值的資料

語法:

long winapi regsetkeyvalue(

_in_ hkey hkey,

_in_opt_ lpctstr lpsubkey,

_in_opt_ lpctstr lpvaluename,

_in_ dword dwtype,

_in_opt_ lpcvoid lpdata,

_in_ dword cbdata

);

引數:

hkey

[輸入]

乙個開啟登錄檔項的控制代碼。金鑰必須已經用key_set_value訪問許可權開啟。有關更多資訊,請參閱 登錄檔項安全和訪問許可權。

該控制代碼由 regcreatekeyex,regcreatekeytransacted,regopenkeyex或 regopenkeytransacted函式返回。它也可以是以下 預定義鍵之一:

hkey_classes_root

hkey_current_config

hkey_current_user

hkey_local_machine

hkey_users

lpsubkey

[in,可選]

金鑰的名稱和由hkey標識的金鑰的子金鑰。如果此引數為null,則使用hkey值在該金鑰中建立此值,並且該金鑰獲取預設的安全描述符。

lpvaluename

[in,可選]

資料將被更新的登錄檔值的名稱。

dwtype

[in]

由lpdata引數指向的資料的型別。有關可能的型別的列表,請參閱 登錄檔值型別。

lpdata

[in,可選]

要用指定的值名稱儲存的資料。

對於基於字串的型別(如reg_sz),該字串必須以空字元結尾。使用reg_multi_sz資料型別時,字串必須以兩個空字元結尾。

cbdata

[in]

lpdata引數指向的資訊的大小,以位元組為單位。如果資料型別為reg_sz,reg_expand_sz或reg_multi_sz,則cbdata必須包含終止空字元的大小

o(∩_∩)o哈哈~到此對登錄檔的操作就完成了

MFC 操作登錄檔

向登錄檔中新增值 先建立鍵,再新增。void cdlglogon initreginfo 獲取登錄檔值 傳入的引數是值名稱。如上面的admin。cstring cdlglogon getkeyvalue cstring strkeyname return 123456abcdefg 刪除登錄檔中的值...

C語言登錄檔操作例項

c語言登錄檔操作例項 例子1 include include int main void else char modlepath 256 char syspath 256 getmodulefilename 0,modlepath,256 取得程式名字 getsystemdirectory sysp...

C語言登錄檔操作例項

例子1 include include int main void else char modlepath 256 char syspath 256 getmodulefilename 0,modlepath,256 取得程式名字 getsystemdirectory syspath,256 ret...