⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expiredlg.cpp

📁 演示如何动态加载一个驱动程序ENUMPCI.sys,应用程序为expire.exe, 该程序可枚举出当前系统上的设备id 和 厂商id
💻 CPP
字号:
// ExpireDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Expire.h"
#include "ExpireDlg.h"
#include "winioctl.h"
#include "Ioctl.h"
#include "Winsvc.h"
#include "NTEnumPCI.H"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 struct buffer1
{
 USHORT DeviceID;
 USHORT VendorID;
 UCHAR  SubClass;
 UCHAR  BaseClass;
}structbuf;
void CExpireDlg::OnStartservice() 
{
	// TODO: Add your control notification handler code here
 	
}

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()

/////////////////////////////////////////////////////////////////////////////
// CExpireDlg dialog

CExpireDlg::CExpireDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CExpireDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CExpireDlg)
		// 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 CExpireDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExpireDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CExpireDlg, CDialog)
	//{{AFX_MSG_MAP(CExpireDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_DISPLAY, OnDisplay)
	ON_BN_CLICKED(IDC_CREATESERVICE, OnCreateservice)
	ON_BN_CLICKED(IDC_STARTSERVICE, OnStartservice)
	ON_BN_CLICKED(IDC_DELETESERVICE, OnDeleteservice)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExpireDlg message handlers

BOOL CExpireDlg::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

   SERVICE_STATUS ss;
	char* DriverName="ENUMPCI.sys";
	char* DisplayName="ENEMPCI";
	char* ServiceName="ENUMPCI";
/////////////////////////////////////////////////////////////////////////////
//Open service control manager
	SC_HANDLE hDriver;
	SC_HANDLE  hSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
	if(hSCManager==NULL)
	{
		//printf("Could not open Service Control Manager\n");
		return FALSE;
	}
////////////////////////////////////////////////////////////////////////////////
//Open driver service
	hDriver=OpenService(hSCManager,"ENUMPCI",SERVICE_ALL_ACCESS);
	if(hDriver==NULL)
	{
//If can't open, create driver service
		CString DriverPath;
		GetDriverPath(DriverPath);
		hDriver=CreateService(hSCManager,ServiceName,DisplayName,
	    	SERVICE_ALL_ACCESS,
		    SERVICE_KERNEL_DRIVER,
    		SERVICE_DEMAND_START,
	    	SERVICE_ERROR_NORMAL,
	    	DriverPath,
	    	NULL,NULL,NULL,NULL,NULL);
	//	Sleep(1000);
    	if(hDriver==NULL)
		{
	    //	printf("Could not install driver with Service Control Manager");
	    	CloseServiceHandle(hSCManager);
	    	return FALSE;
		}
	}
////////////////////////////////////////////////////////////////////////////////
//Start driver service
	ControlService(hDriver,SERVICE_CONTROL_INTERROGATE,&ss);
	StartService(hDriver,0,NULL);
	for(int seconds=0;seconds<10;seconds++)
	{
	 ControlService (hDriver,SERVICE_CONTROL_INTERROGATE,&ss);
     if(ss.dwCurrentState == SERVICE_RUNNING)
		 break;
	 Sleep(1000);
	}
////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
HANDLE hENUMPCI=CreateFile("\\\\.\\ENUMPCI", GENERIC_READ|GENERIC_WRITE, 0,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if( hENUMPCI==INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}
    ULONG BufferSize;
	DWORD BytesReturned;
    DeviceIoControl(hENUMPCI,IOCTL_ENUMPCI_GET_BUFFER_SIZE,
							NULL, 0,					// Input
							&BufferSize, sizeof(ULONG),	// Output
							&BytesReturned, NULL);
		
	DWORD TxdBytes;
	ULONG  Rvalue;
	USHORT    Rvalue1;
    DWORD dwNewPtr;
	CString strinfo;
	CString strinfo1;
    for(ULONG i=0;i<BufferSize/sizeof(structbuf);i++)
	{
	 dwNewPtr=SetFilePointer( hENUMPCI,i*sizeof(structbuf), NULL, FILE_BEGIN);
	 if( !ReadFile( hENUMPCI, &Rvalue,sizeof(ULONG), &TxdBytes, NULL))
		;//printf("Could not read value");
	 else 
	 {
	  
	  strinfo.Format("%x",Rvalue);
	  strinfo.MakeUpper();
	 }
     dwNewPtr=SetFilePointer(hENUMPCI,(i*sizeof(structbuf)+sizeof(ULONG)),NULL,FILE_BEGIN);
     if( !ReadFile( hENUMPCI, &Rvalue1,sizeof(SHORT), &TxdBytes, NULL))
		;//printf("Could not read value");
	 else 
	 {
		 
		 strinfo1.Format("%x",Rvalue1);
		 strinfo1.MakeUpper();
		// if(strcmp(strinfo1,"300")==0)
		// {
			 strinfo=strinfo+"  "+strinfo1;
			 m_list.AddString(strinfo);
		// }
	 }
	}
	
	CloseHandle(hENUMPCI);
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//stop service
	if(hDriver!=NULL)
	{
		ControlService (hDriver,SERVICE_CONTROL_INTERROGATE,&ss);
		if (ss.dwCurrentState == SERVICE_RUNNING)
		{
			if(!ControlService (hDriver,SERVICE_CONTROL_STOP,&ss))
			{
			//	printf("Could not stop driver\n");
				CloseServiceHandle(hSCManager);
				CloseServiceHandle(hDriver);
				return FALSE;
			}
		}
	}
    for(int time=0;time<10;time++)
	{   
	 ControlService (hDriver,SERVICE_CONTROL_INTERROGATE,&ss);
	 if(ss.dwCurrentState == SERVICE_STOPPED)
		 break;
	 Sleep(1000);
	}
//////////////////////////////////////////////////////////////////////////////
//delete service
	if(!DeleteService(hDriver))
	{
		//printf("Could not delete service\n");
		CloseServiceHandle(hSCManager);
		CloseServiceHandle(hDriver);
		return 0;
	}
	CloseServiceHandle(hSCManager);
	CloseServiceHandle(hDriver);
		

		
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CExpireDlg::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 CExpireDlg::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 CExpireDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CExpireDlg::OnDisplay() 
{
	// TODO: Add your control notification handler code here
 

}

void CExpireDlg::OnCreateservice() 
{
	// TODO: Add your control notification handler code here

}

void CExpireDlg::OnDeleteservice() 
{
	// TODO: Add your control notification handler code here

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -