38 lines
1.1 KiB
C
38 lines
1.1 KiB
C
#pragma once
|
|
#include <windows.h>
|
|
#include <tchar.h>
|
|
|
|
int addregistry(LPCWSTR filetarget, LPCWSTR nameReg)
|
|
{
|
|
// Đường dẫn tới file exe
|
|
const TCHAR* exePath = filetarget;
|
|
|
|
// Tên registry key
|
|
const TCHAR* regKey = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
|
|
|
|
// Tên giá trị registry
|
|
const TCHAR* valueName = nameReg;
|
|
|
|
// Mở khóa registry
|
|
HKEY hKey;
|
|
LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, regKey, 0, KEY_SET_VALUE, &hKey);
|
|
if (result != ERROR_SUCCESS) {
|
|
_tprintf(_T("Không thể mở khóa registry. Lỗi: %ld\n"), result);
|
|
return 1;
|
|
}
|
|
|
|
// Đặt giá trị cho registry key
|
|
result = RegSetValueEx(hKey, valueName, 0, REG_SZ, (const BYTE*)exePath, (_tcslen(exePath) + 1) * sizeof(TCHAR));
|
|
if (result != ERROR_SUCCESS) {
|
|
_tprintf(_T("Không thể đặt giá trị registry. Lỗi: %ld\n"), result);
|
|
RegCloseKey(hKey);
|
|
return 1;
|
|
}
|
|
|
|
// Đóng khóa registry
|
|
RegCloseKey(hKey);
|
|
|
|
_tprintf(_T("Đã thêm vào registry key thành công.\n"));
|
|
return 0;
|
|
}
|