#include "stdafx.h"
#include "utils.h"
#include "RegegitKey.h"
#include <windows.h>

CRegegitKey::CRegegitKey(void)
{
}

CRegegitKey::~CRegegitKey(void)
{
}

void CRegegitKey::insert_exe(HKEY hKEY)
{
	WCHAR pFileName[MAX_PATH]; 
	int nPos = GetCurrentDirectory(MAX_PATH, pFileName);

	CString exeName = CString(pFileName) + EXE_NAME;
	LPCTSTR strUserValue = exeName.GetString();

	long ret = ::RegSetValueEx(hKEY, REGEDIT_EXE_PATH, 0, REG_SZ, (const BYTE *) strUserValue, wcslen(strUserValue) * 2+1);
}

void CRegegitKey::insert_flag(HKEY hKEY)
{
	LPCTSTR strUserValue = _T("true");
	long ret = ::RegSetValueEx(hKEY, LOAD_APP_FLAG, 0, REG_SZ, (const BYTE *) strUserValue, wcslen (strUserValue) * 2+1);
}

void CRegegitKey::insert_out(HKEY hKEY)
{
	WCHAR pFileName[MAX_PATH]; 
	GetAppPath(pFileName);

	CString exeName = CString(pFileName) + _T("\\image");

	CString strUser = _T("path");
	LPCTSTR strUserValue = exeName.GetString();
	long ret = ::RegSetValueEx(hKEY, strUser, 0, REG_SZ, (const BYTE *) strUserValue, wcslen(strUserValue) * 2+1);
}

BOOL CRegegitKey::check_reg()
{
	HKEY hKEY;
	long ret0=(RegOpenKeyEx(KEY_ROOT, SUB_KEY_PARENT, 0, KEY64 | KEY_READ |KEY_WRITE, &hKEY));
	if (ret0 != ERROR_SUCCESS) {
		return FALSE;
	}

	LPBYTE getValue = new BYTE[80];//得到的键值
	DWORD keyType = REG_SZ;//定义数据类型
	DWORD DataLen = 80;//定义数据长度
	CString strUser = LOAD_APP_FLAG;//要查询的键名称
	long ret1 = RegQueryValueEx(hKEY, strUser, NULL, &keyType, getValue, &DataLen);
	if (ret1 != ERROR_SUCCESS)
	{
		delete getValue;
		return FALSE;
	}

	if (*getValue == 't') {
		RegCloseKey(hKEY);
		delete getValue;
		return FALSE;
	}

	insert_exe(hKEY);
	insert_flag(hKEY);
	insert_out(hKEY);
	insert_local_path();

	RegCloseKey(hKEY);
	delete getValue;

	return TRUE;
}

BOOL CRegegitKey::read_key(LPCWSTR key, LPBYTE value, LPDWORD dataLength)
{
	HKEY hKEY;
	long ret0=(RegOpenKeyEx(KEY_ROOT, SUB_KEY_PARENT, 0, KEY64 | KEY_READ |KEY_WRITE, &hKEY));
	if (ret0 != ERROR_SUCCESS) {
		return FALSE;
	}

	LPBYTE getValue = new BYTE[256];
	DWORD keyType = REG_SZ;
	long ret1 = RegQueryValueEx(hKEY, key, NULL, &keyType, value, dataLength);
	if (ret1 != ERROR_SUCCESS)
	{
		delete getValue;
		return FALSE;
	}

	RegCloseKey(hKEY);
	delete getValue;

	return TRUE;
}

BOOL CRegegitKey::read_app_key(LPCWSTR key, LPBYTE value, LPDWORD dataLength)
{
	HKEY hKEY;
	long ret0=(RegOpenKeyEx(HKEY_CURRENT_USER, APP_KEY_ROOT, 0, KEY64 | KEY_READ |KEY_WRITE, &hKEY));
	if (ret0 != ERROR_SUCCESS) {
		return FALSE;
	}

	LPBYTE getValue = new BYTE[256];
	DWORD keyType = REG_SZ;
	long ret1 = RegQueryValueEx(hKEY, key, NULL, &keyType, value, dataLength);
	if (ret1 != ERROR_SUCCESS)
	{
		delete getValue;
		return FALSE;
	}

	RegCloseKey(hKEY);
	delete getValue;

	return TRUE;
}

void CRegegitKey::insert_local_path()
{
	HKEY hKey;
	DWORD dw;
	LONG reg = RegCreateKeyEx(HKEY_CURRENT_USER, APP_KEY_ROOT, 0, NULL,
		REG_OPTION_NON_VOLATILE, KEY64|KEY_READ|KEY_WRITE, NULL, &hKey, &dw);

	WCHAR path[MAX_PATH];
	GetAppPath(path);

	CString strUser(APP_PATH);
	LPCTSTR strUserValue(path);
	long ret = ::RegSetValueEx(hKey, strUser, 0, REG_SZ, (const BYTE *) strUserValue, wcslen(strUserValue) * 2+1);
	RegCloseKey(hKey);
}
