Files
EgtLock/RegForMachineId.cpp
T
Dario Sassi 23b8eba120 EgtLock :
- aggiornamenti per cambio nome include di base.
2015-05-11 20:53:58 +00:00

99 lines
3.3 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2014-2014
//----------------------------------------------------------------------------
// File : RegForMachineId.cpp Data : 08.09.14 Versione : 1.5i2
// Contenuto : Funzioni per salvataggio e lettura MachineId da Registry.
//
//
//
// Modifiche : 08.09.14 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//------------------ Include -------------------------------------------------
#include "stdafx.h"
#define NOMINMAX
#include <windows.h>
#include "RegForMachineId.h"
#include "/EgtDev/Include/EgtStringConverter.h"
using namespace std ;
//------------------------------------------------------------------------------------
#define RKEY_EGT_POS L"Software\\EgalTech\\Position"
//------------------------------------------------------------------------------------
LONG CreateAndSetKey( HKEY hKey, const wchar_t* wszSubKey, const wchar_t* wszValName, const wchar_t* wszVal) ;
LONG GetKey( HKEY hKey, const wchar_t* wszSubKey, const wchar_t* wszValName, wchar_t* wszVal, DWORD* pdwLen) ;
//------------------------------------------------------------------------------------
bool
SaveMachineIdOnReg( const string& sMachineId)
{
return ( CreateAndSetKey( HKEY_CURRENT_USER, RKEY_EGT_POS, L"", stringtoW( sMachineId)) == ERROR_SUCCESS) ;
}
//------------------------------------------------------------------------------------
bool
LoadMachineIdFromReg( string& sMachineId)
{
const int LEN_BUF = 50 ;
wcharBuffer wBuffer( LEN_BUF) ;
DWORD dwLen = LEN_BUF ;
if ( GetKey( HKEY_CURRENT_USER, RKEY_EGT_POS, L"", wBuffer.data(), &dwLen) == ERROR_SUCCESS) {
wBuffer.terminate( dwLen) ;
sMachineId = wcharBtoA( wBuffer) ;
return true ;
}
else {
sMachineId.clear() ;
return false ;
}
}
//-----------------------------------------------------------------------------
LONG
CreateAndSetKey( HKEY hKey, const wchar_t* wszSubKey, const wchar_t* wszValName, const wchar_t* wszVal)
{
// creazione della chiave
HKEY hSubKey ;
LONG lResult = ::RegCreateKeyExW( hKey, wszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_WRITE | KEY_READ, NULL,
&hSubKey, NULL) ;
if ( lResult != ERROR_SUCCESS)
return lResult ;
// assegnazione del valore
lResult = ::RegSetValueExW( hSubKey, wszValName, 0, REG_SZ, (LPBYTE) wszVal, (DWORD) 2 * lstrlen( wszVal) + 2) ;
// rilascio l'handle alla chiave
::RegCloseKey( hSubKey) ;
return lResult ;
}
//-----------------------------------------------------------------------------
LONG
GetKey( HKEY hKey, const wchar_t* wszSubKey, const wchar_t* wszValName, wchar_t* wszVal, DWORD* pdwLen)
{
// apro la chiave
HKEY hSubKey ;
LONG lResult = ::RegOpenKeyExW( hKey, wszSubKey, 0, KEY_READ, &hSubKey) ;
if ( lResult != ERROR_SUCCESS)
return lResult ;
// leggo il valore
*pdwLen *= 2 ;
lResult = ::RegQueryValueExW( hSubKey, wszValName, NULL, NULL, (LPBYTE) wszVal, pdwLen) ;
*pdwLen /= 2 ;
// rilascio l'handle alla chiave
::RegCloseKey( hSubKey) ;
return lResult ;
}