C++ 如何读取/更改另一个windows服务创建的注册表值?
因此,基本上我有一个服务,这是作为我的程序更新经理。这是我所有的努力,使一个自动更新程序,不需要用户登录 因此,我的更新管理器在安装时会使用以下代码创建一些初始注册表值/结构:C++ 如何读取/更改另一个windows服务创建的注册表值?,c++,windows,registry,access-denied,access-rights,C++,Windows,Registry,Access Denied,Access Rights,因此,基本上我有一个服务,这是作为我的程序更新经理。这是我所有的努力,使一个自动更新程序,不需要用户登录 因此,我的更新管理器在安装时会使用以下代码创建一些初始注册表值/结构: LPCWSTR strInITKeyName = L"SOFTWARE\\InIT\\"; DWORD rtime = 0; HKEY InITKey; LONG nInITError; nInITError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strInITKeyName, 0, 0,
LPCWSTR strInITKeyName = L"SOFTWARE\\InIT\\";
DWORD rtime = 0;
HKEY InITKey;
LONG nInITError;
nInITError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strInITKeyName, 0, 0, &InITKey);
if (ERROR_NO_MATCH == nInITError || ERROR_FILE_NOT_FOUND == nInITError)
{
std::cout << "Registry key not found. Setting up..." << std::endl;
long nError = RegCreateKeyEx(HKEY_LOCAL_MACHINE, strInITKeyName, 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &InITKey, NULL);
if (ERROR_SUCCESS != nError)
std::cout << "Error: Could not create registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl << "\tERROR: " << nError << std::endl;
else
{
std::cout << "Successfully created registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl;
// See https://www.experts-exchange.com/questions/10171094/Using-RegSetKeySecurity.html for example
//SECURITY_DESCRIPTOR sd;
//PACL pDacl = NULL;
//RegSetKeySecurity(InITKey);
}
}
else if (nInITError == ERROR_ACCESS_DENIED)
{
long nError = RegCreateKeyEx(HKEY_LOCAL_MACHINE, strInITKeyName, 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &InITKey, NULL);
if (ERROR_SUCCESS != nError)
std::cout << "Error: Could not create registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl << "\tERROR: " << nError << std::endl;
else
std::cout << "Successfully created registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl;
}
else if (ERROR_SUCCESS != nInITError)
{
std::cout << "Cannot open registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl << "\tERROR: " << nInITError << std::endl;
rtime = 0;
}
// Generate guid
//
GUID guid;
HRESULT hr = CoCreateGuid(&guid);
// Convert the GUID to a string
OLECHAR* guidString;
StringFromCLSID(guid, &guidString);
// Setup registry values
// Sets clientguid value and ties to strInITKeyName
std::wstring clientguid = guidString; // InITKey
clientguid = clientguid.substr(1, 36);
LONG nClientGUIDError = RegSetValueEx(InITKey, L"clientguid", NULL, REG_SZ, (const BYTE*)clientguid.c_str(), (clientguid.size() + 1) * sizeof(wchar_t));
if (nClientGUIDError)
std::cout << "Error: " << nClientGUIDError << " Could not set registry value: " << "clientguid" << std::endl;
else
std::wcout << "Successfully set InIT clientguid to " << clientguid << std::endl;
// ensure memory is freed
::CoTaskMemFree(guidString);
RegCloseKey(InITKey);
LPCWSTR strInITKeyName=L“软件\\InIT\\”;
DWORD rtime=0;
HKEY InITKey;
长九度误差;
nInITError=RegOpenKeyEx(HKEY_LOCAL_MACHINE、strInITKeyName、0、0和InITKey);
if(ERROR_NO_MATCH==nInITError | | ERROR_FILE_NOT_FOUND==nInITError)
{
std::cout调用RegOpenKeyEx()
时,需要使用按位OR(|
)运算符,而不是逻辑OR(|
)运算符。更改:
KEY_READ || KEY_WOW64_64KEY
致:
但是,当更新管理器调用RegOpenKeyEx()
时,它根本没有指定任何访问权限,而是将samDesired
参数设置为0。它应该将其设置为至少KEY\u SET\u VALUE
。当它调用RegCreateKeyEx()
ifRegOpenKeyEx()时
失败,它正在将samDesired
设置为KEY\u ALL\u ACCESS
。不要这样做。只使用实际需要的访问权限(KEY\u SET\u VALUE
等)
在任何情况下,都不需要同时调用RegOpenKeyEx()
和RegCreateKeyEx()
。只需自己调用RegCreateKeyEx()
。它将打开一个现有密钥,并创建一个不存在的密钥。它的dwDisposition
输出参数将告诉您发生了什么
此代码中还有其他错误。例如,将错误的HKEY
传递给regqueryvaluex()
,以及未正确检查错误访问被拒绝的错误代码(使用=
赋值运算符而不是=
比较运算符),以及内存泄漏
请尝试类似以下内容:
更新管理器:
LPCWSTR strInITKeyName = L"SOFTWARE\\InIT\\";
HKEY InITKey;
DWORD dwDisposition;
LONG nError = RegCreateKeyEx(HKEY_LOCAL_MACHINE, strInITKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE | KEY_WOW64_64KEY, NULL, &InITKey, &dwDisposition);
if (ERROR_SUCCESS != nError)
{
std::cout << "Error: Could not open/create registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl << "\tERROR: " << nError << std::endl;
}
else
{
std::cout << "Successfully " << ((REG_CREATED_NEW_KEY == dwDisposition) ? "created" : "opened") << " registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl;
// Generate guid and convert to a string
//
std::wstring clientguid;
GUID guid;
HRESULT hr = CoCreateGuid(&guid);
if (FAILED(hr))
{
std::cout << "Error: Could not generate clientguid" << std::endl << "\tERROR: " << (int)hr << std::endl;
}
else
{
WCHAR guidString[40] = {0};
int len = StringFromGUID2(guid, guidString, 40);
if (len > 2)
{
// Sets clientguid value and ties to strInITKeyName
clientguid.assign(&guidString[1], len-2);
}
}
// Setup registry values
nError = RegSetValueEx(InITKey, L"clientguid", NULL, REG_SZ, (const BYTE*) clientguid.c_str(), (clientguid.size() + 1) * sizeof(wchar_t));
if (ERROR_SUCCESS != nError)
std::cout << "Error: Could not set registry value: clientguid" << std::endl << "\tERROR: " << nError << std::endl;
else
std::wcout << "Successfully set InIT clientguid to " << clientguid << std::endl;
RegCloseKey(InITKey);
}
LPCWSTR strInITKeyName=L“软件\\InIT\\”;
HKEY InITKey;
德沃德;
LONG nError=RegCreateKeyEx(HKEY_LOCAL_MACHINE,strInITKeyName,0,NULL,REG_OPTION_NON_VOLATILE,KEY_SET_VALUE | KEY_WOW64_64KEY,NULL,&InITKey,&dwDisposition);
如果(错误\u成功!=n错误)
{
标准::cout
KEY_READ | KEY_WOW64_64KEY
LPCWSTR strInITKeyName = L"SOFTWARE\\InIT\\";
HKEY InITKey;
DWORD dwDisposition;
LONG nError = RegCreateKeyEx(HKEY_LOCAL_MACHINE, strInITKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE | KEY_WOW64_64KEY, NULL, &InITKey, &dwDisposition);
if (ERROR_SUCCESS != nError)
{
std::cout << "Error: Could not open/create registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl << "\tERROR: " << nError << std::endl;
}
else
{
std::cout << "Successfully " << ((REG_CREATED_NEW_KEY == dwDisposition) ? "created" : "opened") << " registry key HKEY_LOCAL_MACHINE\\" << strInITKeyName << std::endl;
// Generate guid and convert to a string
//
std::wstring clientguid;
GUID guid;
HRESULT hr = CoCreateGuid(&guid);
if (FAILED(hr))
{
std::cout << "Error: Could not generate clientguid" << std::endl << "\tERROR: " << (int)hr << std::endl;
}
else
{
WCHAR guidString[40] = {0};
int len = StringFromGUID2(guid, guidString, 40);
if (len > 2)
{
// Sets clientguid value and ties to strInITKeyName
clientguid.assign(&guidString[1], len-2);
}
}
// Setup registry values
nError = RegSetValueEx(InITKey, L"clientguid", NULL, REG_SZ, (const BYTE*) clientguid.c_str(), (clientguid.size() + 1) * sizeof(wchar_t));
if (ERROR_SUCCESS != nError)
std::cout << "Error: Could not set registry value: clientguid" << std::endl << "\tERROR: " << nError << std::endl;
else
std::wcout << "Successfully set InIT clientguid to " << clientguid << std::endl;
RegCloseKey(InITKey);
}
...
// Query clientguid from registry
HKEY InITKey;
LONG nError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\InIT\\", 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &InITKey);
if (ERROR_SUCCESS != nError)
{
std::stringstream errstr;
errstr << nError;
this->m_ServiceLogger->info("Error: " + errstr.str() + " RegOpenKeyEx failed");
}
else
{
this->m_ServiceLogger->info("Getting reg");
std::vector<BYTE> buffer(TOTALBYTES + sizeof(wchar_t), 0); // extra room for a null terminator, in case it is missing in the Registry data
DWORD cbData = TOTALBYTES;
do
{
nError = RegQueryValueExW(InITKey, L"clientguid", 0, NULL, &buffer[0], &cbData);
if (ERROR_MORE_DATA != nError)
break;
// Get a buffer that is big enough if not already
this->m_ServiceLogger->info("Resizing clientguid buffer");
buffer.resize(cbData + sizeof(wchar_t));
}
while (true);
if (ERROR_SUCCESS == nError)
{
std::wstring clientguid = (WCHAR*) &buffer[0];
std::string cg(clientguid.begin(), clientguid.end());
this->m_ClientGuid = cg;
this->m_ServiceLogger->info("Clientguid yo: " + cg);
}
else if (ERROR_ACCESS_DENIED == nError)
{
this->m_ServiceLogger->info("ClientGUID: Access Denied");
}
else
{
std::stringstream errstr;
errstr << nError;
this->m_ServiceLogger->info("Error: " + errstr.str() + " RegQueryValueEx failed");
}
RegCloseKey(InITKey);
if (!this->checkRegistryValues())
{
this->generateRegistry();
}
}
...