📄 installdriverdlg.cpp
字号:
// InstallDriverDlg.cpp : implementation file
//
#include "stdafx.h"
#include "InstallDriver.h"
#include "InstallDriverDlg.h"
#include "Winsvc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#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()
/////////////////////////////////////////////////////////////////////////////
// CInstallDriverDlg dialog
CInstallDriverDlg::CInstallDriverDlg(CWnd* pParent /*=NULL*/)
: CDialog(CInstallDriverDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CInstallDriverDlg)
m_cDriverName = _T("");
m_cFilePath = _T("");
m_strStartParam = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CInstallDriverDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CInstallDriverDlg)
DDX_Control(pDX, IDC_START_TYPE, m_cStartType);
DDX_Text(pDX, IDC_DRIVER_NAME, m_cDriverName);
DDX_Text(pDX, IDC_FILE_PATH, m_cFilePath);
DDX_Text(pDX, IDC_START_PARAM, m_strStartParam);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CInstallDriverDlg, CDialog)
//{{AFX_MSG_MAP(CInstallDriverDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_GET_FILE_PATH, OnGetFilePath)
ON_BN_CLICKED(IDC_INSTALL, OnInstall)
ON_BN_CLICKED(IDC_UNINSTALL, OnUninstall)
ON_BN_CLICKED(IDC_START_SERVICE, OnStartServer)
ON_BN_CLICKED(IDC_STOP_SERVICE, OnStopService)
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInstallDriverDlg message handlers
BOOL CInstallDriverDlg::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
// TODO: Add extra initialization here
m_cStartType.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CInstallDriverDlg::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 CInstallDriverDlg::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 CInstallDriverDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CInstallDriverDlg::OnGetFilePath()
{
CFileDialog cFileDlg(TRUE,".sys",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Driver Files (*.sys)|*.sys|Service Files(*.exe)|*.exe||",NULL);
UpdateData();
if(cFileDlg.DoModal() == IDOK)
{
m_cFilePath = cFileDlg.GetPathName();
m_cDriverName = cFileDlg.GetFileTitle();
UpdateData(FALSE);
}
}
void CInstallDriverDlg::OnInstall()
{
char cSystemRoot[MAX_PATH];
char *cpFile;
SC_HANDLE schSCManager,schService;
int iStartType = 0;
CString strStartType,strFileInfo;
UpdateData();
if(m_cFilePath.IsEmpty())
{
MessageBox("请先选择驱动文件",PRODUCT_NAME);
return;
}
if(m_cDriverName.IsEmpty())
{
MessageBox("请先指定驱动名",PRODUCT_NAME);
return;
}
cpFile = strrchr(m_cFilePath,'\\');
GetSystemDirectory(cSystemRoot,MAX_PATH);
sprintf(m_cDstFile,"%s\\drivers%s",cSystemRoot,cpFile);
if(!CopyFile(m_cFilePath,m_cDstFile,FALSE))
{
MessageBox("拷贝文件失败!",PRODUCT_NAME);
return;
}
schSCManager = OpenSCManager( NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if(schSCManager == NULL)
{
MessageBox("打开服务管理器失败!",PRODUCT_NAME);
return ;
}
m_cStartType.GetLBText(m_cStartType.GetCurSel(),strStartType);
if(!strStartType.Compare("SERVICE_BOOT_START"))
iStartType= SERVICE_BOOT_START;
else if(!strStartType.Compare("SERVICE_SYSTEM_START"))
iStartType= SERVICE_SYSTEM_START;
else if(!strStartType.Compare("SERVICE_AUTO_START"))
iStartType= SERVICE_AUTO_START;
else if(!strStartType.Compare("SERVICE_DEMAND_START"))
iStartType= SERVICE_DEMAND_START;
else if(!strStartType.Compare("SERVICE_DISABLED"))
iStartType= SERVICE_DISABLED;
if(!m_strStartParam.IsEmpty())
strFileInfo.Format("%s %s",m_cFilePath,m_strStartParam);
else
strFileInfo = m_cFilePath;
m_cFilePath.MakeLower();
if(m_cFilePath.Find(".exe",m_cFilePath.GetLength()-4)>0)
schService = CreateService( schSCManager, // SCManager database
m_cDriverName, // name of service
m_cDriverName, // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_FILE_SYSTEM_DRIVER,//SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, // service type
iStartType, // start type
SERVICE_ERROR_NORMAL, // error control type
strFileInfo, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL // no password
);
else
schService = CreateService( schSCManager, // SCManager database
m_cDriverName, // name of service
m_cDriverName, // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_KERNEL_DRIVER, // service type
iStartType, // start type
SERVICE_ERROR_NORMAL, // error control type
strFileInfo, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL // no password
);
if ( schService == NULL )
{
MessageBox("创建服务失败!",PRODUCT_NAME);
CloseServiceHandle( schService );
return ;
}
else
MessageBox("创建服务成功!",PRODUCT_NAME);
CloseServiceHandle(schSCManager);
CloseServiceHandle( schService );
}
void CInstallDriverDlg::OnUninstall()
{
SC_HANDLE schSCManager,schService;
UpdateData();
if(m_cDriverName.IsEmpty())
{
MessageBox("请先指定驱动名",PRODUCT_NAME);
return;
}
schSCManager = OpenSCManager( NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if(schSCManager == NULL)
{
MessageBox("打开服务管理器失败!",PRODUCT_NAME);
return ;
}
schService = OpenService( schSCManager,
m_cDriverName,
SERVICE_ALL_ACCESS
);
if ( schService == NULL )
{
MessageBox("删除服务失败!",PRODUCT_NAME);
CloseServiceHandle(schSCManager);
return;
}
if(!DeleteService( schService ))
MessageBox("删除服务失败!",PRODUCT_NAME);
else
MessageBox("删除服务成功!",PRODUCT_NAME);
CloseServiceHandle(schSCManager);
CloseServiceHandle( schService );
}
void CInstallDriverDlg::OnStartServer()
{
SC_HANDLE schSCManager,schService;
BOOL ret;
UpdateData();
if(m_cDriverName.IsEmpty())
{
MessageBox("请先指定驱动名",PRODUCT_NAME);
return;
}
schSCManager = OpenSCManager( NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if(schSCManager == NULL)
{
MessageBox("打开服务管理器失败!",PRODUCT_NAME);
return ;
}
schService = OpenService( schSCManager,
m_cDriverName,
SERVICE_ALL_ACCESS
);
if ( schService == NULL )
{
MessageBox("打开服务失败!",PRODUCT_NAME);
CloseServiceHandle(schSCManager);
return;
}
ret = StartService( schService, 0, NULL )|| GetLastError() == ERROR_SERVICE_ALREADY_RUNNING || GetLastError() == ERROR_SERVICE_DISABLED;
CloseServiceHandle(schSCManager);
CloseServiceHandle( schService );
if(!ret)
{
ret = GetLastError();
MessageBox("启动服务失败!",PRODUCT_NAME);
}
else
MessageBox("启动服务成功!",PRODUCT_NAME);
}
void CInstallDriverDlg::OnStopService()
{
SC_HANDLE schSCManager,schService;
SERVICE_STATUS serviceStatus;
BOOL ret;
UpdateData();
if(m_cDriverName.IsEmpty())
{
MessageBox("请先指定驱动名",PRODUCT_NAME);
return;
}
schSCManager = OpenSCManager( NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if(schSCManager == NULL)
{
MessageBox("打开服务管理器失败!",PRODUCT_NAME);
return ;
}
schService = OpenService( schSCManager,
m_cDriverName,
SERVICE_ALL_ACCESS
);
if ( schService == NULL )
{
MessageBox("打开服务失败!",PRODUCT_NAME);
CloseServiceHandle(schSCManager);
return;
}
ret = ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus );
CloseServiceHandle(schSCManager);
CloseServiceHandle( schService );
if(!ret)
MessageBox("停止服务失败!",PRODUCT_NAME);
else
MessageBox("停止服务成功!",PRODUCT_NAME);
}
void CInstallDriverDlg::OnButton1()
{
HANDLE hFile;
char cDeviceName[MAX_PATH];
UpdateData();
if( (GetVersion() & 0xFF) >= 5 )
wsprintf( cDeviceName, TEXT("\\\\.\\Global\\%s"), m_cDriverName );
else
wsprintf( cDeviceName, TEXT("\\\\.\\%s"), m_cDriverName );
hFile = CreateFile( cDeviceName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if(hFile == ((HANDLE)-1))
MessageBox("打开文件失败",PRODUCT_NAME);
else
MessageBox("打开文件成功",PRODUCT_NAME);
CloseHandle(hFile);
}
void CInstallDriverDlg::OnAbout()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -