1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <Windows.h>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
namespace std {
typedef basic_string<TCHAR> _tstring;
}
//Class for closing a registry key
class CHKey
{
HKEY hKey;
public:
CHKey() : hKey() {}
explicit CHKey(HKEY hKey) : hKey(hKey) {}
CHKey(HKEY hParentKey, LPCSTR subKey, REGSAM access) : hKey() { RegOpenKeyExA(hParentKey, subKey, 0, access, &this->hKey); }
CHKey(HKEY hParentKey, LPCWSTR subKey, REGSAM access) : hKey() { RegOpenKeyExW(hParentKey, subKey, 0, access, &this->hKey); }
CHKey(CHKey && src) : hKey(hKey) { src.hKey=NULL; }
~CHKey() { RegCloseKey(hKey); }
operator HKEY() const { return hKey; }
private:
CHKey(CHKey const &); //Visual Studio 2010 doesn't know of "=delete"
CHKey& operator=(CHKey const &);
};
//String conversion functions
string GetStringA(string s) { return s; }
string GetStringA(wstring s, int codePage=CP_ACP)
{
int cbBufSize = WideCharToMultiByte(codePage, 0, s.c_str(), -1, NULL, 0, NULL, NULL);
vector<char> buf(cbBufSize);
WideCharToMultiByte(codePage, 0, s.c_str(), -1, &buf[0], cbBufSize, NULL, NULL);
string ret(&buf[0]);
return ret;
}
wstring GetStringW(string s, int codePage=CP_ACP)
{
int cchBufSize = MultiByteToWideChar(codePage, 0, s.c_str(), -1, NULL, 0);
vector<wchar_t> buf(cchBufSize);
MultiByteToWideChar(codePage, 0, s.c_str(), -1, &buf[0], cchBufSize);
wstring ret(&buf[0]);
return ret;
}
wstring GetStringW(wstring s) { return s; }
//String conversion functions with a code page ID
string GetStringA(string s, int codePage) { return GetStringA(GetStringW(s), codePage); }
wstring GetStringW(wstring s, int codePage) { (void)codePage; return s; }
//Registry functions
_tstring GetRegValueString(HKEY hKey, _tstring valueName)
{
DWORD type = REG_NONE;
DWORD cb = 0;
LSTATUS status = RegQueryValueEx(hKey, valueName.c_str(), NULL, &type, NULL, &cb);
if(status != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ))
return TEXT(""); //Should probably throw an exception instead.
//Round the byte size UP to a character size, and add a null character just to be sure
size_t cch = (cb + sizeof(TCHAR)-1) / sizeof(TCHAR) + 1;
vector<TCHAR> buf(cch);
status = RegQueryValueEx(hKey, valueName.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(&buf[0]), &cb);
if(status != ERROR_SUCCESS)
return TEXT(""); //Should probably throw an exception instead.
_tstring ret(&buf[0]);
return ret;
}
_tstring GetRegValueString(HKEY hKey, _tstring subKey, _tstring valueName)
{
CHKey shSubKey(hKey, subKey.c_str(), KEY_READ);
return GetRegValueString(shSubKey, valueName);
}
void ajouteUneLigneRapport(string uneLigne)
{
string const monFichier = "C:/temp/essai.txt";
std::ofstream file( monFichier.c_str(), std::ios_base::app );
file << uneLigne << endl;
}
int main(void)
{
_tstring s = GetRegValueString(HKEY_CURRENT_USER, TEXT("SOFTWARE\\0000\\"), TEXT("Essai"));
string s2 = GetStringA(s);
cout << "Data found (converted to extended ASCII) : " + s2 << endl;
ajouteUneLigneRapport(s2);
return 0;
} |
Partager