2016-07-01 3 views
-1

regedit를 검사하여 해당 프로그램에 대한 레지스트리 제거 폴더를 검색하려고합니다. 내 함수는 현재 프로그램을 찾지 만 어떤 이유로 인해 다음 반복까지 RegGetValue 함수의 출력 값을 업데이트하지 않습니다. 따라서 올바른 레지스트리 키와 그 전임자를 인쇄합니다. 어떤 아이디어?C++ RegGetValue 함수 출력 오류

필자는 Visual Studio 2015를 사용하는 Intel 프로세서가 장착 된 Windows 10 64 비트 워크 스테이션을 사용합니다.

MAIN.CPP

#include "Registry.h" 
#include <Windows.h> 
#include <tchar.h> 

void main() 
{ 
    Registry test(_T("Microsoft SQL Server 2012 Native Client ")); 
} 

Registry.h

#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; 
}; 

Registry.cpp 나는 많은 문제와 볼

#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(""); 
} 
+0

'파일 << 지수 << ":"<

+0

경로를 누출하지 않고 디버그하려면 어떻게 인쇄합니까? –

+0

당신은'new []'로 할당 한 것을'[]'삭제해야합니다. 그렇지 않으면 대신'std :: vector' 또는'std :: basic_string'을 사용하십시오. 또한'_printf()'대신'std :: wcout'을 사용해야합니다. 그리고'X86REG_PATH'를 전혀 쓰면 안되며 대신'RRF_SUBKEY_WOW6432KEY' 플래그를 사용하십시오. –

답변

2

당신의 코드.

버퍼링 충돌을 일으키는 std::wcout_tprintf()을 혼합합니다.

charwchar_t 데이터를 잘못 혼합하고 있습니다.

모든 루프 반복마다 guidPath이 유출되고 있습니다.

RegGetValue()을 호출 할 때 nameSize을 초기화하지 않습니다.

32 비트 Wow64Node 키에 올바르게 액세스하도록 코드를 설정하지 않았습니다.

대신이 같은 것을 시도해보십시오.

MAIN.CPP

#include <Windows.h> 
#include "Registry.h" 

void main() 
{ 
    Registry test(L"Microsoft SQL Server 2012 Native Client"); 
} 

Registry.h

#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; 
}; 

Registry.cpp

#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""; 
} 
+0

오케이. 직장에서 내일 노력할 것입니다. 읽을만한 좋은 문서가 있습니까? 나는 윈도우 정의 타입에 대한 경험이 없으며 단지 학습 목적으로 연습하고 있습니다. –

+0

또한 wchar 함수와 _tchar 함수를 명시 적으로 사용하는 이유는 무엇입니까? –

+0

당신의 솔루션이 효과가있었습니다. 내가 가진 문제는 nameSize 값을 볼 수있는 최대 길이로 다시 설정하지 않는 것입니다. 고맙습니다. –