C++ ReGigValk函数输出误差
我正试图通过选中regedit,在注册表的卸载文件夹中搜索已知程序。My函数当前查找程序,但由于某些原因,它在下一次迭代之前不会更新RegGetValue函数的输出值。因此,它会打印正确的注册表项及其前置项。有什么想法吗 我使用的是Windows 10 64位工作站,英特尔处理器使用Visual Studio 2015,如果有必要的话 main.cpp 注册表.h Registry.cppC++ ReGigValk函数输出误差,c++,visual-studio-2015,64-bit,registry,windows-10,C++,Visual Studio 2015,64 Bit,Registry,Windows 10,我正试图通过选中regedit,在注册表的卸载文件夹中搜索已知程序。My函数当前查找程序,但由于某些原因,它在下一次迭代之前不会更新RegGetValue函数的输出值。因此,它会打印正确的注册表项及其前置项。有什么想法吗 我使用的是Windows 10 64位工作站,英特尔处理器使用Visual Studio 2015,如果有必要的话 main.cpp 注册表.h Registry.cpp 我发现你的代码有很多问题 您正在混合std::wcout和_tprintf,这会导致缓冲冲突 您错误地混合
我发现你的代码有很多问题 您正在混合std::wcout和_tprintf,这会导致缓冲冲突 您错误地混合了char和wchar\t数据 在每次循环迭代中都会泄漏guidPath 调用RegGetValue时,您没有初始化nameSize 您没有将代码设置为正确访问32位WOW64节点密钥 试试类似的东西 main.cpp 注册表.h Registry.cpp
文件如何打印以调试而不泄漏路径?您必须删除使用新[]分配的任何内容。否则,请改用std::vector或std::basic_字符串。另外,您应该使用std::wcout而不是_printf。您不应该是X86REG_PATH,而是使用RRF_子项\u WOW6432KEY标志。我不知道正在定义注册表子项。所以我肯定会使用它们,我知道要删除一个heap对象;我刚刚染上了我的臭虫。当前逻辑的问题是,当我在找到键的第一个实例后进行调试时(我将第一个if语句放入其中的原因),下一次迭代不会更新compareName字段,并将打印下一个guidPath.ok。我明天上班去试试。你有什么好的文档可以推荐阅读吗?我对windows定义的类型没有经验,只是为了学习而练习。还有,为什么要显式使用wchar函数而不是tchar函数?你的解决方案奏效了。我遇到的问题是没有将nameSize值重置为它可以看到的最大长度。谢谢。@traviscok TCHAR用于向后兼容单个代码库中针对Win9x/ME和NT4+的旧代码。没有人再支持Win9x/ME了。Windows NT4+都是Unicode,您应该只使用Unicode API。除非你真的需要,否则不要使用ANSI API。我的员工说,由于所有产品的兼容性,所以使用TCHAR代替所有的WC++和Char代码。但我不认为我们假设低于XP,所以我会问一下。谢谢你的意见。
#include "Registry.h"
#include <Windows.h>
#include <tchar.h>
void main()
{
Registry test(_T("Microsoft SQL Server 2012 Native Client "));
}
#pragma once
#include <Windows.h>
#include <string>
#define REG_PATH L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
#define X86REG_PATH L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
class Registry
{
public:
Registry(TCHAR* name);
~Registry();
TCHAR* findGUID();
TCHAR* getDisplayName();
TCHAR* getGUID();
TCHAR* getVersion();
TCHAR* getPublisher();
TCHAR* getInstallDate();
private:
TCHAR* displayName;
TCHAR* guid;
TCHAR* version;
TCHAR* publisher;
TCHAR* installDate;
};
#pragma once
#include "Registry.h"
#include <Windows.h>
#include <iostream>
#include <tchar.h>
#include <fstream>
Registry::Registry(TCHAR* name)
{
HKEY hKey;
LPCTSTR lpSubKey;
DWORD ulOptions;
REGSAM samDesired;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_PATH, NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
{
std::wofstream file;
file.open("test.txt");
int index = 0;
HKEY UninstallDir = hKey;
TCHAR subKey[MAX_PATH];
DWORD subKeySize = MAX_PATH;
while (RegEnumKeyEx(hKey, index, subKey, &subKeySize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
HKEY guid;
TCHAR* guidPath = new TCHAR[MAX_PATH];
_tcscpy_s(guidPath, MAX_PATH, REG_PATH);
_tcscat_s(guidPath, MAX_PATH, subKey);
TCHAR compareName[MAX_PATH];
DWORD nameSize;
//print all registry keys to file
file << index << ": " << guidPath << std::endl;
int test;
RegGetValue(HKEY_LOCAL_MACHINE, guidPath, _T("DisplayName"), RRF_RT_ANY, NULL, &compareName, &nameSize);
//compare all registry keys *temporary to debug
if (_tcscmp(guidPath, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{49D665A2-4C2A-476E-9AB8-FCC425F526FC}")) == 0)
{
std::wcout << guidPath << " found" << std::endl;
}
if (_tcscmp(compareName, name) == 0)
{
_tprintf(_T("%d: %s\n"), index, guidPath);
}
//print if found
index++;
subKeySize = 260;
}
file.close();
}
else
{
std::cout << "Could not open registry key." << std::endl;
}
//temporary to see console
std::cin.get();
}
//still need to be completed
Registry::~Registry()
{
}
TCHAR* Registry::findGUID()
{
return _T("");
}
TCHAR* Registry::getDisplayName()
{
return _T("");
}
TCHAR* Registry::getGUID()
{
return _T("");
}
TCHAR* Registry::getVersion()
{
return _T("");
}
TCHAR* Registry::getPublisher()
{
return _T("");
}
TCHAR* Registry::getInstallDate()
{
return _T("");
}
#include <Windows.h>
#include "Registry.h"
void main()
{
Registry test(L"Microsoft SQL Server 2012 Native Client");
}
#pragma once
#include <Windows.h>
#include <string>
class Registry
{
public:
Registry(const std::wstring &name);
~Registry();
std::wstring findGUID();
std::wstring getDisplayName();
std::wstring getGUID();
std::wstring getVersion();
std::wstring getPublisher();
std::wstring getInstallDate();
private:
std::wstring displayName;
std::wstring guid;
std::wstring version;
std::wstring publisher;
std::wstring installDate;
};
#pragma once
#include <Windows.h>
#include "Registry.h"
#include <iostream>
#include <fstream>
#define REG_PATH L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
Registry::Registry(const std::wstring &name)
{
HKEY hKey;
// If you want to open the 32bit Wow64Node key,
// DO NOT open the key directly! Open the 64bit
// key and include the KEY_WOW64_32KEY flag
// so it will redirect to the Wow64Node key...
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_PATH, NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
{
std::wofstream file;
file.open(L"test.txt");
WCHAR subKey[MAX_PATH];
DWORD subKeySize = MAX_PATH;
WCHAR compareName[MAX_PATH];
DWORD nameSize;
int index = 0;
while (RegEnumKeyEx(hKey, index, subKey, &subKeySize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
// print all registry keys to file
file << index << L": " << REG_PATH << subKey << std::endl;
int test;
nameSize = sizeof(compareName);
RegGetValue(hKey, NULL, L"DisplayName", RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ | RRF_ZEROONFAILURE, NULL, compareName, &nameSize);
//compare all registry keys *temporary to debug
if (wcscmp(subKey, L"{49D665A2-4C2A-476E-9AB8-FCC425F526FC}") == 0)
{
std::wcout << subKey << L" found" << std::endl;
}
if (name == compareName)
{
std::wcout << name << L" found" << std::endl;
}
//print if found
index++;
subKeySize = MAX_PATH;
}
file.close();
}
else
{
std::wcout << L"Could not open registry key." << std::endl;
}
//temporary to see console
std::wcin.get();
}
//still need to be completed
Registry::~Registry()
{
}
std::wstring Registry::findGUID()
{
return L"";
}
std::wstring Registry::getDisplayName()
{
return L"";
}
std::wstring Registry::getGUID()
{
return L"";
}
std::wstring Registry::getVersion()
{
return L"";
}
std::wstring Registry::getPublisher()
{
return L"";
}
std::wstring Registry::getInstallDate()
{
return L"";
}