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

📄 testhello.cpp

📁 一个简单的windows驱动源代码
💻 CPP
字号:
//
//Created by	
//Tsinghua University E.E.	Tanzhangxi(xtan) 1999.9.7
//

// testhello.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <winioctl.h>
#include <setupapi.h>
#include "..\hellowdmioctl.h"

#include <objbase.h>
#include <initguid.h>

DEFINE_GUID(GUID_HELLOWDM, 0x3d93c5c0, 0x0085, 0x11d1, 0x82, 0x1e, 0x00, 
    0x80, 0xc8, 0x83, 0x27, 0xab);


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	// setup device interface
	HDEVINFO info = SetupDiGetClassDevs((LPGUID) &GUID_HELLOWDM, NULL, 
                                        NULL, DIGCF_PRESENT | 
                                        DIGCF_INTERFACEDEVICE);

    if (info == INVALID_HANDLE_VALUE)
    {
        printf("Error %d trying to open enumeration handle for GUID_HELLOWDM\n", GetLastError());
        exit(1);
    }
	
	SP_INTERFACE_DEVICE_DATA ifdata;
    ifdata.cbSize = sizeof(ifdata);
    
	if (!SetupDiEnumDeviceInterfaces(info, NULL, (LPGUID) &GUID_HELLOWDM,
                                     0, &ifdata))
    {
        printf("Error %d trying to enumerate interfaces\n", 
            GetLastError());
        SetupDiDestroyDeviceInfoList(info);
        exit(1);
     }
	
	DWORD needed;
    SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &needed, NULL);
    PSP_INTERFACE_DEVICE_DETAIL_DATA detail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc(needed);

	if (!detail)
    {
        printf("Error %d trying to get memory for interface detail\n", GetLastError());
        SetupDiDestroyDeviceInfoList(info);
        exit(1);
    }

    detail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
    if (!SetupDiGetDeviceInterfaceDetail(info, &ifdata, detail, needed, NULL, NULL))
    {
        printf("Error %d getting interface detail\n", GetLastError());
        free((PVOID) detail);
        SetupDiDestroyDeviceInfoList(info);
        exit(1);
    }

    char name[MAX_PATH];
    strncpy(name, detail->DevicePath, sizeof(name));
    free((PVOID) detail);
    SetupDiDestroyDeviceInfoList(info);

	//open hellowdm.sys
    HANDLE hfile = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (hfile == INVALID_HANDLE_VALUE)
    {
        printf("Error %d trying to open %s\n", GetLastError(), name);
        exit(1);
    }
	
	DWORD junk;
    //test DeviceIoControl Send HELLOWDM_IOCTL_HELLO control code
	if(!DeviceIoControl(hfile,HELLOWDM_IOCTL_HELLO,NULL,0,NULL,0,&junk,NULL))
    {
		printf("Direct IOCTL failed with code %d\n", GetLastError());
	}
	//if succuss,You'll see "Hello from WDM." within the Kernel mode Debugger,such as SoftIce
	CloseHandle(hfile);
	return 0;
}



⌨️ 快捷键说明

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