VC操作登錄檔

wzm10455發表於2014-08-18


#include "stdafx.h"
#include "windows.h"
#include "string.h"

int _tmain(int argc, _TCHAR* argv[])
{
	HKEY hKey; //定義有關的hKey,在查詢結束時要關閉。 
	LPCTSTR path = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";

	LONG return0=(::RegOpenKeyEx(HKEY_CURRENT_USER,path,0,KEY_ALL_ACCESS,&hKey));
	if(return0!=ERROR_SUCCESS)
	{
		::MessageBox(NULL, L"錯誤:無法開啟有關的鍵!",NULL,MB_ICONERROR | MB_OK);
		return 1;
	}else
	{
		//RegSetValueEx(hKey, L"MSAWIN", 0, REG_SZ, (const BYTE *)path, strlen((const char *)(path)) );
	    RegDeleteValue(hKey, TEXT("MSAWIN"));
		
	}
	LPBYTE username_Get=new BYTE[80];
	DWORD type_1=REG_SZ;
	DWORD cbData_1=80;
	LONG return1=::RegQueryValueEx(hKey,L"MSAWIN",NULL,&type_1,username_Get,&cbData_1);
	if(return1!=ERROR_SUCCESS)
	{
		::MessageBox(NULL, L"錯誤:無法查詢有關注冊表資訊!",NULL,MB_ICONERROR | MB_OK);
		return 1;
	}else
	{
		::MessageBox(NULL, L"登錄檔註冊成功!",NULL,MB_ICONERROR | MB_OK);
	}
	//程式結束前關閉已經開啟的hKey
	RegCloseKey(hKey);
	return 0;
}

提示:HKEY_CURRENT_USER與HKEY_LOCAL_MACHINE是有區別的,當以HKEY_LOCAL_MACHINE操作時需要以管理員身份執行該程式,否則操作失敗。

相關文章