📄 giveioinstallerdlg.cpp
字号:
/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Rick Haubenstricker
* Perceptron, Inc
* 47827 Halyard Dr
* Plymouth, Michigan 48170, USA
*
* rickh@perceptron.com
*
*/
// GiveIOInstallerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GiveIOInstaller.h"
#include "GiveIOInstallerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
DWORD dwDebugFlag = 1L;
#else
DWORD dwDebugFlag = 0L;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGiveIOInstallerDlg dialog
CGiveIOInstallerDlg::CGiveIOInstallerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGiveIOInstallerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGiveIOInstallerDlg)
m_ServiceStatus = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGiveIOInstallerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGiveIOInstallerDlg)
DDX_Text(pDX, IDC_STATUS_EDIT, m_ServiceStatus);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGiveIOInstallerDlg, CDialog)
//{{AFX_MSG_MAP(CGiveIOInstallerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_INSTALL_BUTTON, OnInstallButton)
ON_BN_CLICKED(IDC_REMOVE_BUTTON, OnRemoveButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGiveIOInstallerDlg message handlers
BOOL CGiveIOInstallerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// update status display
if (!UpdateStatusDisplay())
return FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CGiveIOInstallerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CGiveIOInstallerDlg::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 CGiveIOInstallerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGiveIOInstallerDlg::OnInstallButton()
{
SC_HANDLE schSCManager = NULL;
// update status display
if (!UpdateStatusDisplay())
return;
// verify service and or file are not currently installed
if (m_DriverStatus.bServiceRunning &&
m_DriverStatus.bServiceInstalled &&
m_DriverStatus.bFileInstalled)
{
// service installed, running or file installed, post message and return
if (MessageBox("Service currently installed, remove service and install?",
"Error", MB_OKCANCEL | MB_ICONEXCLAMATION) != IDOK)
{
UpdateStatusDisplay();
return;
}
}
else if (m_DriverStatus.bServiceRunning ||
m_DriverStatus.bServiceInstalled ||
m_DriverStatus.bFileInstalled)
{
// service installed, running or file installed, post message and return
if (MessageBox("Service partially installed, remove service and install?",
"Error", MB_OKCANCEL | MB_ICONEXCLAMATION) != IDOK)
{
UpdateStatusDisplay();
return;
}
}
// display wait cursor
CWaitCursor wait;
// open service control manager
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
// unable to open service control manager
MessageBox("Failed to open service control manager", "Error", MB_OK | MB_ICONEXCLAMATION);
UpdateStatusDisplay();
return;
}
// stop service if running
if (m_DriverStatus.bServiceRunning)
{
if (!ServiceStop(schSCManager, DRIVER_SERVICE_NAME, dwDebugFlag))
{
MessageBox("Failed stopping existing service", "Error", MB_OK | MB_ICONEXCLAMATION);
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
}
// remove service if installed
if (m_DriverStatus.bServiceInstalled)
{
if (!ServiceRemove(schSCManager, DRIVER_SERVICE_NAME, dwDebugFlag))
{
MessageBox("Failed removing existing service, try ""Remove Service"" first", "Error", MB_OK | MB_ICONEXCLAMATION);
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
}
// remove system file if installed
if (m_DriverStatus.bFileInstalled)
{
if (!RemoveFile(DRIVER_FILE_NAME, dwDebugFlag))
{
MessageBox("Failed removing system file", "Error", MB_OK | MB_ICONEXCLAMATION);
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
}
// install system file
if (!InstallFile(IDR_GIVEIO, DRIVER_FILE_NAME, dwDebugFlag))
{
MessageBox("Error installing system file", "Error", MB_OK | MB_ICONINFORMATION );
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
// create service
if (!ServiceInstall(schSCManager, DRIVER_SERVICE_NAME, DRIVER_FILE_NAME, dwDebugFlag))
{
MessageBox("Failed to create service", "Error", MB_OK | MB_ICONINFORMATION );
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
// start service
if (!ServiceStart(schSCManager, DRIVER_SERVICE_NAME, dwDebugFlag))
{
MessageBox("Failed to create service", "Error", MB_OK | MB_ICONINFORMATION );
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
// close handle to service manager
CloseServiceHandle(schSCManager);
// update status display
if (!UpdateStatusDisplay())
return;
// restore cursor
wait.Restore( );
return;
}
void CGiveIOInstallerDlg::OnRemoveButton()
{
SC_HANDLE schSCManager = NULL;
// update status display
if (!UpdateStatusDisplay())
return;
// display wait cursor
CWaitCursor wait;
// open service control manager
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
// unable to open service control manager
MessageBox("Failed to open service control manager", "Error", MB_OK | MB_ICONEXCLAMATION);
UpdateStatusDisplay();
return;
}
// stop service if running
if (m_DriverStatus.bServiceRunning)
{
if (!ServiceStop(schSCManager, DRIVER_SERVICE_NAME, dwDebugFlag))
{
MessageBox("Failed stopping existing service", "Error", MB_OK | MB_ICONEXCLAMATION);
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
}
// remove service if installed
if (m_DriverStatus.bServiceInstalled)
{
if (!ServiceRemove(schSCManager, DRIVER_SERVICE_NAME, dwDebugFlag))
{
MessageBox("Failed removing existing service, try ""Remove Service"" again", "Error", MB_OK | MB_ICONEXCLAMATION);
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
}
// remove system file if installed
if (m_DriverStatus.bFileInstalled)
{
if (!RemoveFile(DRIVER_FILE_NAME, dwDebugFlag))
{
MessageBox("Failed removing system file", "Error", MB_OK | MB_ICONEXCLAMATION);
CloseServiceHandle(schSCManager);
UpdateStatusDisplay();
return;
}
}
// close handle to service manager
CloseServiceHandle(schSCManager);
// update status display
if (!UpdateStatusDisplay())
return;
// restore cursor
wait.Restore( );
return;
}
BOOL CGiveIOInstallerDlg::UpdateStatusDisplay()
{
if (!GetDriverStatus(DRIVER_SERVICE_NAME, DRIVER_FILE_NAME, &m_DriverStatus, dwDebugFlag))
{
MessageBox("Failed to get driver status", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
if (m_DriverStatus.bServiceRunning &&
m_DriverStatus.bServiceInstalled &&
m_DriverStatus.bFileInstalled)
{
// service fully installed
m_ServiceStatus.LoadString(IDS_SERVICE_RUNNING);
}
else if (m_DriverStatus.bServiceRunning ||
m_DriverStatus.bServiceInstalled ||
m_DriverStatus.bFileInstalled)
{
// service partially installed
m_ServiceStatus.LoadString(IDS_SERVICE_PARTIAL);
}
else
{
// service not installed
m_ServiceStatus.LoadString(IDS_SERVICE_NOTINSTALLED);
}
// update display
UpdateData(FALSE);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -