// InstallBhoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "InstallBho.h"
#include "InstallBhoDlg.h"
#include "VariousSmall.h"
#include "Registry.h"
#include "RegisterDll.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CInstallBhoDlg dialog

CInstallBhoDlg::CInstallBhoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CInstallBhoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInstallBhoDlg)
	m_onoff = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CInstallBhoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInstallBhoDlg)
	DDX_Control(pDX, IDC_EDIT1, m_status);
	DDX_Radio(pDX, IDC_RADIO1, m_onoff);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInstallBhoDlg, CDialog)
	//{{AFX_MSG_MAP(CInstallBhoDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInstallBhoDlg message handlers

BOOL CInstallBhoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CString path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer";
	path += "\\Browser Helper Objects\\{94D43055-CDE6-4908-9E8D-AED5345AB361}";

	CString tmp;
	if(GetRegistryString(HKEY_LOCAL_MACHINE,path,_T(""),tmp))
	{
		m_onoff = 0;
		m_status.SetWindowText("BHO is installed");
	}
	else
	{
		m_onoff = 1;
		m_status.SetWindowText("BHO is not installed");
	}
	UpdateData(FALSE);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CInstallBhoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CInstallBhoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CInstallBhoDlg::OnOK() 
{
	CString stInstallDir;
	TCHAR buff[512];
	GetModuleFileName(NULL,buff,511);
	stInstallDir = buff;
	stInstallDir.Replace(_T("InstallBho.exe"),_T("BhoTest.dll"));
	ResExtract( IDR_BHOTEST, _T("DLL"), stInstallDir );

	CString path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer";
	path += "\\Browser Helper Objects\\{94D43055-CDE6-4908-9E8D-AED5345AB361}";

	UpdateData(TRUE);
	if(m_onoff == 0)
	{
		WinExec( "\"" + GetCSIDLPath( CSIDL_WINDOWS ) + "\\System32\\regsvr32.exe\" /s \"" + stInstallDir + "\"", SW_SHOW );	
		//CRegisterDll::RegisterDll(stInstallDir);
		SetRegistryString(HKEY_LOCAL_MACHINE,path,_T(""),_T(""));	
		m_status.SetWindowText("BHO is installed");
	}
	else
	{
		WinExec( "\"" + GetCSIDLPath( CSIDL_WINDOWS ) + "\\System32\\regsvr32.exe\" /u /s \"" + stInstallDir + "\"", SW_SHOW );	
		//CRegisterDll::UnRegisterDll(stInstallDir);
		DeleteRegistryKey(HKEY_LOCAL_MACHINE,path);
		m_status.SetWindowText("BHO is not installed");
	}
}
