Skip to content

Instantly share code, notes, and snippets.

@NoTimeForHero
Created December 6, 2014 02:15
Show Gist options
  • Select an option

  • Save NoTimeForHero/51bcff47011536bf8626 to your computer and use it in GitHub Desktop.

Select an option

Save NoTimeForHero/51bcff47011536bf8626 to your computer and use it in GitHub Desktop.
BCC32 Registry Check
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
bool isNeedMoreThenOne;
int PackagesFounded;
char *neededKey;
wchar_t *neededKey_t;
std::wstring neededPackage;
LONG GetStringRegKey(HKEY hKey, const std::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue)
{
strValue = strDefaultValue;
WCHAR szBuffer[512];
DWORD dwBufferSize = sizeof(szBuffer);
ULONG nError;
nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize);
if (ERROR_SUCCESS == nError)
{
strValue = szBuffer;
}
return nError;
}
void QueryKey(HKEY hKey, std::string orig_key)
{
TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
DWORD cbName; // size of name string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys=0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, retCode;
TCHAR achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
// Get the class name and the value count.
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
if (cSubKeys)
{
for (i=0; i<cSubKeys; i++)
{
if (!isNeedMoreThenOne && PackagesFounded > 0) return;
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
HKEY hNewKey;
std::string path = TEXT(orig_key);
path.append("\\");
path.append(achKey);
const char * key_path = path.c_str();
if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, key_path, 0, KEY_READ, &hNewKey) == ERROR_SUCCESS) {
QueryKey(hNewKey,path);
}
RegCloseKey(hNewKey);
}
}
}
if (cValues)
{
for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retCode = RegEnumValue(hKey, i,
achValue,
&cchValue,
NULL,
NULL,
NULL,
NULL);
if (retCode == ERROR_SUCCESS )
{
if (strcmp(achValue,neededKey) == 0) {
std::wstring value;
GetStringRegKey(hKey, neededKey_t, value, L"NoNameExists");
if (value.substr(0,neededPackage.length()) == neededPackage) {
PackagesFounded++;
if (!isNeedMoreThenOne) return;
}
// Cледущий код вызывает мистическое падение программы
//std::wcout << wat.substr(0,9) << std::endl;
}
}
}
}
}
int FindPackagesByName (std::wstring PackageName, bool isFindAll = false) {
HKEY hFirstKey;
PackagesFounded = 0;
neededKey = "DisplayName";
neededKey_t = L"DisplayName";
neededPackage = PackageName;
isNeedMoreThenOne = isFindAll;
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"), 0, KEY_READ, &hFirstKey) == ERROR_SUCCESS )
{
QueryKey(hFirstKey, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"));
}
RegCloseKey(hFirstKey);
return PackagesFounded;
}
int main() {
std::cout << "Packages founded: " << FindPackagesByName(L"Microsoft Visual C++ 2008 Redistributable", true) << std::endl;
//std::cin >> i;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment