I really need your help. I tried everything but the result is always the same, nothing. Please advise.
And this is my code
#include <windows.h>
#include <iostream>
#include <windef.h>
using namespace std;
int main (void)
{
HKEY hKey;
LONG regOpenResult;
const char PATH[] = "C:\\Users\\les\\Documents\\visual studio 2010\\Projects\\registryTester\\Debug\\registryTester.exe";
开发者_如何学运维 RegCreateKeyExW(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
0,
NULL,REG_OPTION_VOLATILE,
KEY_SET_VALUE,
NULL,
&hKey,
NULL
);
RegSetValueExW(hKey, L"regTest", 0, REG_SZ, (BYTE*)PATH, strlen(PATH));
RegCloseKey(hKey);
}
You're using the unicode version of the RegSetValueEx, but passing it a char* string. You should change the declaration of PATH to:
const wchar_t PATH[] = L"C:\\Users\\les\\Documents\\visual studio 2010\\Projects\\registryTester\\Debug\\registryTester.exe";
And use wcslen
instead of strlen
since it's a wchar_t string. Since it's measured in bytes (not characters) and needs to include the null terminator, the size parameter should be wcslen(PATH)*2+2
.
Check the return status of each API call!!!! Use GetLastError () for each failure.
Knowing the error should lead you to resolve the problem.
Otherwise, you're flying completely blind...
Yes, PATH shouldn't be an 8-bit character string if you're calling a 16-bit Unicode API. But it's not necessarily the only problem.
And yes, "permissions" could very definitely be an issue if you're using Vista, Windows 7 or higher.
精彩评论