Додаток Г.1 Лістинг потоку зчитування даних реєстру

//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include <string.h>

#include <windows.h>

#include <limits.h>

#include <vector>

#include "database.h"

#include "dbthread.h"

#include "main.h"

#include "wait.h"

#pragma package(smart_init)

USEFORM("main.cpp", Window);

static const HKEY hkey[] = { HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,

HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA,

HKEY_CURRENT_CONFIG, HKEY_DYN_DATA };

static const UnicodeString hstr[] = { "HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER",

"HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_PERFORMANCE_DATA",

"HKEY_CURRENT_CONFIG", "HKEY_DYN_DATA" };

template <class IntegerType>

IntegerType BitsToInt(IntegerType& result, char* bits, bool little_endian = true)

{

result = 0;

if (little_endian)

for (int n = sizeof(result); n >= 0; n--)

result = (result << 8) + bits[ n ];

else

for (unsigned n = 0; n < sizeof(result); n++)

result = (result << 8) + bits[ n ];

return result;

}

AnsiString BitsToBin(AnsiString& result, char* bits, int size)

{

for (int n = 0; n < size; n++)

{

int tmp = bits[ n ];

if (tmp < 0) tmp+=256;

result+= IntToHex(tmp, 2) + " ";

}

return result;

}

AnsiString BitsToMulti(AnsiString& result, char* bits, int size)

{

LPSTR ptr;

TStringList *L = new TStringList;

ptr = bits;

while(*ptr!= 0)

{

L->Add(ptr);

ptr += strlen(ptr)+1;

}

result = L->Text;

return result;

}

AnsiString StrToBin(AnsiString& str, AnsiString &bin)

{

__int64 integer = StrToInt64(str);

while (integer>0)

{

bin+=integer%2;

integer = integer >> 1;

}

}

void FindKeys(TRegistry *Reg, TTreeNode *Current, vector <UnicodeString> *Path, TTreeView *RegView, DB *db)

{

TStringList *List = new TStringList();

Reg->GetKeyNames(List);

RegView->Items->AddChild(Current,List->Strings[0]);

Current = Current->getFirstChild();

for (int i=0; i<List->Count; i++)

{

if (i) Current = RegView->Items->Add(Current, List->Strings[i]);

Reg->OpenKeyReadOnly(List->Strings[i]);

AnsiString path, name, type, value;

path = Reg->RootKeyName;

if ((Reg->CurrentPath.w_str())[0]!='\\') path+= '\\';

path+= Reg->CurrentPath;

DWORD ValueType, BufferSize = Reg->GetDataSize(name);

LPSTR ptr, Buffer;

RegQueryValueExA(Reg->CurrentKey,name.c_str(),NULL, NULL, NULL, &BufferSize);

Buffer = new BYTE[BufferSize];

RegQueryValueExA(Reg->CurrentKey,name.c_str(),NULL, &ValueType, Buffer, &BufferSize);

type = "REG_SZ";

value = static_cast <AnsiString> (Buffer);

name = "(Default)";

AnsiString query = "INSERT INTO Keys VALUES (\""+

path+"\", \""+name+"\", \""+type+"\", \""+value+"\")";

db->SQLRun(query.c_str());

delete [] Buffer;

TStringList *Keys = new TStringList();

Reg->GetValueNames(Keys);

while (Keys->Count)

{

AnsiString path, name, type, value;

try

{

path = Reg->RootKeyName;

if ((Reg->CurrentPath.w_str())[0]!='\\') path+= '\\';

path+= Reg->CurrentPath;

name = Keys->Strings[0];

DWORD ValueType, BufferSize;

LPSTR ptr, Buffer;

RegQueryValueExA(Reg->CurrentKey,name.c_str(),NULL, NULL, NULL, &BufferSize);

Buffer = new BYTE[BufferSize];

RegQueryValueExA(Reg->CurrentKey,name.c_str(),NULL, &ValueType, Buffer, &BufferSize);

switch (ValueType)

{

case REG_NONE:

{

type = "REG_NONE";

BitsToBin(value, Buffer, BufferSize);

break;

}

case REG_SZ:

{

type = "REG_SZ";

value = /*static_cast <AnsiString> (*/Buffer;

break;

}

case REG_EXPAND_SZ:

{

type = "REG_EXPAND_SZ";

value = static_cast <AnsiString> (Buffer);

break;

}

case REG_BINARY:

{

type = "REG_BINARY";

BitsToBin(value, Buffer, BufferSize);

break;

}

case REG_DWORD:

{

type = "REG_DWORD";

unsigned int tmp;

BitsToInt<unsigned int>(tmp, Buffer, true);

value = IntToStr((int)tmp);

break;

}

case REG_DWORD_BIG_ENDIAN:

{

type = "REG_DWORD_BIG_ENDIAN";

unsigned int tmp;

BitsToInt<unsigned int>(tmp, Buffer, false);

value = IntToStr((int)tmp);

break;

}

case REG_LINK:

{

type = "REG_LINK";

value = static_cast <AnsiString> (Buffer);

break;

}

case REG_MULTI_SZ:

{

type = "REG_MULTI_SZ";

BitsToMulti(value, Buffer, BufferSize);

break;

}

case REG_RESOURCE_LIST:

{

type = "REG_RESOURCE_LIST";

BitsToBin(value, Buffer, BufferSize);

break;

}

case REG_FULL_RESOURCE_DESCRIPTOR:

{

type = "REG_FULL_RESOURCE_DESCRIPTOR";

BitsToBin(value, Buffer, BufferSize);

break;

}

case REG_RESOURCE_REQUIREMENTS_LIST:

{

type = "REG_RESOURCE_REQUIREMENTS_LIST";

BitsToBin(value, Buffer, BufferSize);

break;

}

case REG_QWORD:

{

type = "REG_QWORD";

unsigned long tmp;

BitsToInt<unsigned long>(tmp, Buffer, true);

value = IntToStr((__int64)tmp);

break;

}

default:

{

break;

}

}

delete [] Buffer;

}

catch (...)

{

Keys->Delete(0);

continue;

}

AnsiString query = "INSERT INTO Keys VALUES (\""+

path+"\", \""+name+"\", \""+type+"\", \""+value+"\")";

db->SQLRun(query.c_str());

Keys->Delete(0);

}

if (Reg->HasSubKeys())

{

Path->push_back(Reg->CurrentPath);

FindKeys (Reg, Current, Path, RegView, db);

}

if (Path->size())

Reg->OpenKeyReadOnly(Path->back());

else Reg->CloseKey();

delete Keys;

}

Path->pop_back();

Reg->CloseKey();

delete List;

}

__fastcall dbCreate::dbCreate(bool CreateSuspended)

: TThread(CreateSuspended)

{

}

//---------------------------------------------------------------------------

void __fastcall dbCreate::Execute()

{

Window->SearchText->Enabled = false;

Window->SearchBtn->Enabled = false;

// ----- створення файлу БД

if (FileExists("reg.db")) remove("reg.db");

DB *db;

try {

db = new DB("reg.db");

} catch (DB::Err e)

{

ShowMessage(e.errstr);

exit(1);

}

db->SQLRun("CREATE TABLE Keys (\

root varchar, \

name varchar, \

type varchar, \

value varchar)");

db->SQLRun(".mode TABS");

// -----

for (int i=0; i<7; i++)

{

TRegistry *Reg = new TRegistry();

Reg->RootKey = hkey[i];

Reg->OpenKeyReadOnly("");

if (!Reg->HasSubKeys())

{

delete Reg;

continue;

}

vector <UnicodeString> *Path = new vector <UnicodeString>;

TTreeNode *Current = Window->RegView->Items->Add(NULL,hstr[i]);

FindKeys (Reg, Current, Path, Window->RegView, db);

delete Path;

delete Reg;

}

Waiting->Label1->Caption = "Запис бази даних";

Waiting->Label2->Caption = "успішно завершено!";

Window->RegView->SaveToFile("tree");

MessageBeep(MB_OK);

Window->SearchText->Enabled = true;

Window->SearchBtn->Enabled = true;

Sleep(1000);

Waiting->Close();

}

//---------------------------------------------------------------------------



Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: