void C注册表操作_自启动runDlg::OnBnClickedButton2()
{
HKEY hRoot = HKEY_LOCAL_MACHINE;
wchar_t *szSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;//打开指定子键
DWORD dwDisposition = REG_OPENED_EXISTING_KEY;
//如果不存在就创建
LONG lRet = RegCreateKeyEx(
hRoot,
szSubKey,
0,
NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
NULL,
&hKey,
&dwDisposition
);
if (lRet != ERROR_SUCCESS)
return;
//得到当前执行文件的文件名(包含路径)
wchar_t szModule[MAX_PATH];
GetModuleFileName(NULL, szModule, MAX_PATH);
//创建一个新的键值,设置键值数据为文件
lRet = RegSetValueEx(
hKey,
L"名称2",
0,
REG_SZ,
(BYTE*)L"C:\\2.exe",
50
);
if (lRet == ERROR_SUCCESS)
AfxMessageBox((L"成功"));
//关闭子键句柄
}
void C注册表操作_自启动runDlg::OnBnClickedButton3()
{
HKEY hKey;
RegCreateKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", &hKey);
RegSetValue(hKey, 0, REG_SZ, L"C:\\1.exe", 6);
RegCloseKey(hKey);
}