www.pudn.com > disk-filter.gz > Install_UsbfDlg.cpp, change:2006-05-15,size:13259b


// Install_UsbfDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Install_Usbf.h" 
#include "Install_UsbfDlg.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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CInstall_UsbfDlg dialog 
 
CInstall_UsbfDlg::CInstall_UsbfDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CInstall_UsbfDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CInstall_UsbfDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CInstall_UsbfDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CInstall_UsbfDlg) 
	DDX_Control(pDX, IDC_LIST_LOG, m_LogList); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CInstall_UsbfDlg, CDialog) 
	//{{AFX_MSG_MAP(CInstall_UsbfDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDOK, OnInstall) 
	ON_BN_CLICKED(ID_UNINSTALL, OnUninstall) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CInstall_UsbfDlg message handlers 
 
BOOL CInstall_UsbfDlg::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_LogList.InsertColumn(0,"Log Message",LVCFMT_LEFT, 300); 
 
	GetDlgItem( IDOK )->EnableWindow(TRUE); 
	GetDlgItem( ID_UNINSTALL )->EnableWindow(TRUE); 
	 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CInstall_UsbfDlg::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 CInstall_UsbfDlg::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 CInstall_UsbfDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
#include  <winsvc.h> 
#include  <string.h> 
#include  <wchar.h> 
#include  <winerror.h> 
 
#define   MAJOR_VERSION  5 
 
#define   WIN2K_MINOR    0 
#define   WINXP_MINOR    1 
 
#define   WIN2K_BUILD    2195 
#define   WINXP_BUILD    2600 
#define   WIN2K3_BUILD   3790 
 
void CInstall_UsbfDlg::OnInstall()  
{ 
	CHAR ErrorBuf[256]; 
 
	OSVERSIONINFO  osVersion; 
	osVersion.dwOSVersionInfoSize  = sizeof(OSVERSIONINFO); 
	 
	GetVersionEx(&osVersion); 
	if( osVersion.dwMajorVersion != MAJOR_VERSION ) 
	{ 
		sprintf(ErrorBuf, "安装失败: %s\n", "不支持此操作系统"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), ErrorBuf ); 
		 
		return ; 
	} 
 
	CHAR currentDir[MAX_PATH]; 
	GetCurrentDirectory( MAX_PATH, currentDir  ); 
 
	switch( osVersion.dwBuildNumber ) 
	{ 
	case    WIN2K_BUILD: 
		strcat(currentDir,"\\diskf_scu_2195.sys"); 
		break; 
 
	case    WINXP_BUILD: 
		strcat(currentDir,"\\diskf_scu.sys"); 
		break; 
 
	case    WIN2K3_BUILD: 
		strcat(currentDir,"\\diskf_scu_3790.sys"); 
		break; 
 
	default: 
		 
		sprintf(ErrorBuf, "安装失败: %s\n", "不支持此操作系统"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), ErrorBuf ); 
		 
		return ; 
	} 
 
	CHAR SystemDir[MAX_PATH]; 
	GetSystemDirectory(  SystemDir ,MAX_PATH); 
	strcat( SystemDir,"\\Drivers\\diskfscu.sys"); 
	if( !CopyFile( currentDir, SystemDir, FALSE) ) 
	{ 
		sprintf(ErrorBuf, "安装失败: %s\n", "拷贝驱动程序失败!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), ErrorBuf ); 
		return ; 
	} 
 
	SC_HANDLE   hSCM = NULL; 
	SC_HANDLE   hService = NULL; 
	 
	hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 
	if(NULL == hSCM ) 
	{ 
		sprintf(ErrorBuf, "安装失败: %s\n", "无法打开SCM!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), ErrorBuf ); 
		return ; 
	} 
 
	hService = CreateService(hSCM, 
				             "diskfa", 
							 "Disk Performence Monitor", 
							 SERVICE_ALL_ACCESS, 
							 SERVICE_KERNEL_DRIVER, 
							 SERVICE_BOOT_START, 
							 SERVICE_ERROR_NORMAL, 
							 "system32\\DRIVERS\\diskfscu.sys", 
							 "System Bus Extender", 
							 NULL, 
							 NULL, 
							 NULL, 
							 NULL); 
 
	if(NULL == hService ) 
	{ 
		DWORD dwError = GetLastError( ); 
		if( dwError = 0x00000431 ) 
			sprintf(ErrorBuf, "安装信息: %s\n", "驱动服务已经安装!"); 
		else 
			sprintf(ErrorBuf, "安装失败: %s\n", "无法安装服务!"); 
 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), ErrorBuf ); 
	} 
	 
	if( hSCM != NULL ) 
	{ 
		CloseServiceHandle( hSCM ); 
	} 
 
	if( hService != NULL ) 
	{ 
		CloseServiceHandle( hService );  
	} 
 
	 
	HKEY hKEY = HKEY_LOCAL_MACHINE; 
	BOOL bl=FALSE; 
	DWORD type=REG_MULTI_SZ; 
	HKEY hSubKey; 
 
	long ret=::RegOpenKeyEx(hKEY, 
		                    "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E967-E325-11CE-BFC1-08002BE10318}", 
							0, 
							KEY_READ|KEY_WRITE, 
							&hSubKey); 
     
	if( ERROR_SUCCESS == ret ) 
	{ 
	    UCHAR data[256]; 
        memset( data, 0, 256 ); 
        DWORD dwSize = 256; 
		ULONG type = REG_MULTI_SZ; 
 
		RegQueryValueEx( hSubKey, 
			             "UpperFilters", 
						 0, 
						 &type, 
						 data,  
						 &dwSize 
						 ); 
 
        char*  start =(char*) data; 
		DWORD  processLen = 0; 
		 
		while( processLen < dwSize ) 
		{ 
			char* p =	strstr( (const char *) start, "diskfa" ); 
			if( p != NULL ) 
			{ 
				sprintf(ErrorBuf, "安装信息: %s\n", "Filter键已经安装!"); 
				m_LogList.InsertItem(m_LogList.GetItemCount( ), ErrorBuf ); 
				RegCloseKey( hKEY ); 
				RegCloseKey( hSubKey ); 
				return ; 
			} 
			else  
			{ 
				processLen  += strlen((char *) start ) + 1; 
				start = (char *) data + processLen; 
			} 
		} 
 
		// 
		// 
		// 
		data[dwSize-1] = 'd'; 
		data[dwSize+0] = 'i'; 
		data[dwSize+1] = 's'; 
		data[dwSize+2] = 'k'; 
		data[dwSize+3] = 'f'; 
		data[dwSize+4] = 'a'; 
		data[dwSize+5] = 0; 
		data[dwSize+6] = 0; 
	 
 
		ret = RegSetValueEx( hSubKey, 
		                     "UpperFilters", 
							 0, 
							 REG_MULTI_SZ, 
							 data, 
							 dwSize+7  
							 ); 
 
		sprintf(ErrorBuf, "安装成功"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), ErrorBuf ); 
 
		RegCloseKey( hKEY ); 
		RegCloseKey( hSubKey ); 
	} 
	TryReboot(); 
	return; 
} 
 
 
VOID CInstall_UsbfDlg::TryReboot() 
{ 
	if( IDYES == ::MessageBox(NULL,"系统设置已经改变,需要重启才能生效","重启系统",MB_YESNO|MB_ICONWARNING) ) 
	{ 
		HANDLE hToken;  
		TOKEN_PRIVILEGES tkp;  
		 
		// Get a token for this process.  
		 
		OpenProcessToken(GetCurrentProcess(),  
			TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, 
			&hToken) ; 
		 
		 
		// Get the LUID for the shutdown privilege.  
		 
		LookupPrivilegeValue(NULL,  
			SE_SHUTDOWN_NAME,  
			&tkp.Privileges[0].Luid);  
		 
		tkp.PrivilegeCount = 1;  // one privilege to set     
		tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
		 
		// Get the shutdown privilege for this process.  
		 
		AdjustTokenPrivileges(hToken,  
			FALSE, &tkp, 0,  
			(PTOKEN_PRIVILEGES)NULL, 0);  
		 
		 
		if (!ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0))  
		{ 
			::MessageBox(NULL,"安装程序无法重启系统,需要手动重启","无法重启",MB_OK|MB_ICONWARNING);			 
		}		 
	} 
} 
 
void CInstall_UsbfDlg::OnUninstall()  
{ 
 
	CHAR  errorBuf[256]; 
	BOOL  bRebootNeed = FALSE;  
	// 
	//At first, delete driver file 
	// 
 
	CHAR SystemDir[MAX_PATH]; 
	GetSystemDirectory(  SystemDir ,MAX_PATH); 
	strcat( SystemDir,"\\Drivers\\diskf_scu.sys"); 
     
	if( !DeleteFile( SystemDir ) ) 
	{ 
		sprintf( errorBuf, "卸载信息: 删除驱动文件失败!\n"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf ); 
	} 
	else 
	{ 
		sprintf( errorBuf, "卸载信息: 成功删除驱动文件!\n"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf ); 
		bRebootNeed = TRUE; 
	} 
 
    // 
	//Second, delete service 
	// 
	SC_HANDLE   hSCM = NULL; 
	SC_HANDLE   hService = NULL; 
	 
	hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 
	if(NULL == hSCM ) 
	{ 
		sprintf(errorBuf, "卸载信息: %s\n", "无法打开SCM!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf ); 
 
		goto DelReg; 
	} 
 
	hService = OpenService(hSCM,       // SCManager database  
                           TEXT("diskfa"), // name of service  
						   DELETE);            // only need DELETE access  
  
    if (hService == NULL) 
    {  
		sprintf(errorBuf, "卸载信息: %s\n", "无法打开驱动服务!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf ); 
 
		goto DelReg; 
    } 
  
    if (!DeleteService(hService) )  
    { 
		sprintf(errorBuf, "卸载信息: %s\n", "无法删除驱动服务!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf ); 
 
		goto DelReg; 
    } 
    else  
	{ 
		sprintf(errorBuf, "卸载信息: %s\n", "成功删除驱动服务!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf );  
 
		bRebootNeed = TRUE; 
	} 
 
DelReg: 
	if( hSCM != NULL ) 
	{ 
		CloseServiceHandle( hSCM ); 
	} 
 
	if( hService != NULL ) 
	{ 
		CloseServiceHandle( hService );  
	} 
 
	// 
	//Third, Delete Register key 
	// 
 
	HKEY hKEY = HKEY_LOCAL_MACHINE; 
	BOOL bl=FALSE; 
	DWORD type=REG_MULTI_SZ; 
	HKEY hSubKey; 
 
	long ret=::RegOpenKeyEx(hKEY, 
		                    "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E967-E325-11CE-BFC1-08002BE10318}", 
							0, 
							KEY_READ|KEY_WRITE, 
							&hSubKey); 
     
	if( ERROR_SUCCESS != ret ) 
	{ 
		sprintf(errorBuf, "卸载信息: %s\n", "无法打开注册表!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf ); 
	} 
 
	UCHAR data[256]; 
	memset( data, 0, 256 ); 
	DWORD dwSize = 256; 
 
	RegQueryValueEx( hSubKey, 
			         "UpperFilters", 
					 0, 
					 &type, 
					 data,  
					 &dwSize 
					 ); 
 
	char*  start =(char*) data; 
	DWORD  processLen = 0; 
	 
	CHAR*   pUsbcnfStart = NULL; 
	 
	while( processLen < dwSize ) 
	{ 
		pUsbcnfStart =	strstr( (const char *) start, "diskfa" ); 
		if( pUsbcnfStart != NULL ) 
		{ 
			break ; 
		} 
		else  
		{ 
			processLen  += strlen((char *) start ) + 1; 
			start = (char *) data + processLen; 
		} 
	} 
 
	// 
 
	// 
	if( pUsbcnfStart != NULL ) 
	{ 
		// 
		//It is last entry 
		// 
		if( pUsbcnfStart[7] == 0 ) 
		{ 
			pUsbcnfStart[0]= 0 ; 
		} 
		else 
		{ 
			memcpy( pUsbcnfStart, 
				    pUsbcnfStart + 7, // 7 == strlen( "usbcnf") + 1 
					dwSize-((ULONG )pUsbcnfStart -(ULONG) data) - 7); 
		} 
 
		ret = RegSetValueEx( hSubKey, 
			                 "UpperFilters", 
							 0, 
							 REG_MULTI_SZ, 
							 data, 
							 dwSize - 7  
							 ); 
 
		sprintf(errorBuf, "卸载信息: 删除filter键值成功!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf ); 
		bRebootNeed = TRUE; 
	} 
	else 
	{ 
		sprintf(errorBuf, "卸载信息: %s\n", "注册表中没有需要删除的值!"); 
		m_LogList.InsertItem(m_LogList.GetItemCount( ), errorBuf ); 
	} 
	 
	RegCloseKey( hKEY ); 
	RegCloseKey( hSubKey ); 
 
	if( bRebootNeed ==  TRUE ) 
		TryReboot( ); 
}