在CLion中构建Windows内核WDM驱动

news/2024/11/7 18:31:40/

在CLion中构建Windows内核WDM驱动

1、安装好CLion,VS2019,WDK

过程略

2、下载FindWDK.cmake

该文件来自于github/SergiusTheBest

# Redistribution and use is allowed under the OSI-approved 3-clause BSD license.
# Copyright (c) 2018 Sergey Podobry (sergey.podobry at gmail.com). All rights reserved.#.rst:
# FindWDK
# ----------
#
# This module searches for the installed Windows Development Kit (WDK) and 
# exposes commands for creating kernel drivers and kernel libraries.
#
# Output variables:
# - `WDK_FOUND` -- if false, do not try to use WDK
# - `WDK_ROOT` -- where WDK is installed
# - `WDK_VERSION` -- the version of the selected WDK
# - `WDK_WINVER` -- the WINVER used for kernel drivers and libraries 
#        (default value is `0x0601` and can be changed per target or globally)
#
# Example usage:
#
#   find_package(WDK REQUIRED)
#
#   wdk_add_library(KmdfCppLib STATIC KMDF 1.15
#       KmdfCppLib.h 
#       KmdfCppLib.cpp
#       )
#   target_include_directories(KmdfCppLib INTERFACE .)
#
#   wdk_add_driver(KmdfCppDriver KMDF 1.15
#       Main.cpp
#       )
#   target_link_libraries(KmdfCppDriver KmdfCppLib)
#if(DEFINED ENV{WDKContentRoot})file(GLOB WDK_NTDDK_FILES"$ENV{WDKContentRoot}/Include/*/km/ntddk.h")
else()file(GLOB WDK_NTDDK_FILES"C:/Program Files*/Windows Kits/10/Include/*/km/ntddk.h")
endif()if(WDK_NTDDK_FILES)list(GET WDK_NTDDK_FILES -1 WDK_LATEST_NTDDK_FILE)
endif()include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(WDK REQUIRED_VARS WDK_LATEST_NTDDK_FILE)if (NOT WDK_LATEST_NTDDK_FILE)return()
endif()get_filename_component(WDK_ROOT ${WDK_LATEST_NTDDK_FILE} DIRECTORY)
get_filename_component(WDK_ROOT ${WDK_ROOT} DIRECTORY)
get_filename_component(WDK_VERSION ${WDK_ROOT} NAME)
get_filename_component(WDK_ROOT ${WDK_ROOT} DIRECTORY)
get_filename_component(WDK_ROOT ${WDK_ROOT} DIRECTORY)message(STATUS "WDK_ROOT: " ${WDK_ROOT})
message(STATUS "WDK_VERSION: " ${WDK_VERSION})set(WDK_WINVER "0x0601" CACHE STRING "Default WINVER for WDK targets")set(WDK_ADDITIONAL_FLAGS_FILE "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/wdkflags.h")
file(WRITE ${WDK_ADDITIONAL_FLAGS_FILE} "#pragma runtime_checks(\"suc\", off)")set(WDK_COMPILE_FLAGS"/Zp8" # set struct alignment"/GF"  # enable string pooling"/GR-" # disable RTTI"/Gz" # __stdcall by default"/kernel"  # create kernel mode binary"/FIwarning.h" # disable warnings in WDK headers"/FI${WDK_ADDITIONAL_FLAGS_FILE}" # include file to disable RTC)set(WDK_COMPILE_DEFINITIONS "WINNT=1")
set(WDK_COMPILE_DEFINITIONS_DEBUG "MSC_NOOPT;DEPRECATE_DDK_FUNCTIONS=1;DBG=1")if(CMAKE_SIZEOF_VOID_P EQUAL 4)list(APPEND WDK_COMPILE_DEFINITIONS "_X86_=1;i386=1;STD_CALL")set(WDK_PLATFORM "x86")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)list(APPEND WDK_COMPILE_DEFINITIONS "_WIN64;_AMD64_;AMD64")set(WDK_PLATFORM "x64")
else()message(FATAL_ERROR "Unsupported architecture")
endif()string(CONCAT WDK_LINK_FLAGS"/MANIFEST:NO " #"/DRIVER " #"/OPT:REF " #"/INCREMENTAL:NO " #"/OPT:ICF " #"/SUBSYSTEM:NATIVE " #"/MERGE:_TEXT=.text;_PAGE=PAGE " #"/NODEFAULTLIB " # do not link default CRT"/SECTION:INIT,d " #"/VERSION:10.0 " #)# Generate imported targets for WDK lib files
file(GLOB WDK_LIBRARIES "${WDK_ROOT}/Lib/${WDK_VERSION}/km/${WDK_PLATFORM}/*.lib")    
foreach(LIBRARY IN LISTS WDK_LIBRARIES)get_filename_component(LIBRARY_NAME ${LIBRARY} NAME_WE)string(TOUPPER ${LIBRARY_NAME} LIBRARY_NAME)add_library(WDK::${LIBRARY_NAME} INTERFACE IMPORTED)set_property(TARGET WDK::${LIBRARY_NAME} PROPERTY INTERFACE_LINK_LIBRARIES  ${LIBRARY})
endforeach(LIBRARY)
unset(WDK_LIBRARIES)function(wdk_add_driver _target)cmake_parse_arguments(WDK "" "KMDF;WINVER" "" ${ARGN})add_executable(${_target} ${WDK_UNPARSED_ARGUMENTS})set_target_properties(${_target} PROPERTIES SUFFIX ".sys")set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "${WDK_COMPILE_FLAGS}")set_target_properties(${_target} PROPERTIES COMPILE_DEFINITIONS"${WDK_COMPILE_DEFINITIONS};$<$<CONFIG:Debug>:${WDK_COMPILE_DEFINITIONS_DEBUG}>;_WIN32_WINNT=${WDK_WINVER}")set_target_properties(${_target} PROPERTIES LINK_FLAGS "${WDK_LINK_FLAGS}")target_include_directories(${_target} SYSTEM PRIVATE"${WDK_ROOT}/Include/${WDK_VERSION}/shared""${WDK_ROOT}/Include/${WDK_VERSION}/km""${WDK_ROOT}/Include/${WDK_VERSION}/km/crt")target_link_libraries(${_target} WDK::NTOSKRNL WDK::HAL WDK::BUFFEROVERFLOWK WDK::WMILIB)if(CMAKE_SIZEOF_VOID_P EQUAL 4)target_link_libraries(${_target} WDK::MEMCMP)endif()if(DEFINED WDK_KMDF)target_include_directories(${_target} SYSTEM PRIVATE "${WDK_ROOT}/Include/wdf/kmdf/${WDK_KMDF}")target_link_libraries(${_target}"${WDK_ROOT}/Lib/wdf/kmdf/${WDK_PLATFORM}/${WDK_KMDF}/WdfDriverEntry.lib""${WDK_ROOT}/Lib/wdf/kmdf/${WDK_PLATFORM}/${WDK_KMDF}/WdfLdr.lib")if(CMAKE_SIZEOF_VOID_P EQUAL 4)set_property(TARGET ${_target} APPEND_STRING PROPERTY LINK_FLAGS "/ENTRY:FxDriverEntry@8")elseif(CMAKE_SIZEOF_VOID_P  EQUAL 8)set_property(TARGET ${_target} APPEND_STRING PROPERTY LINK_FLAGS "/ENTRY:FxDriverEntry")endif()else()if(CMAKE_SIZEOF_VOID_P EQUAL 4)set_property(TARGET ${_target} APPEND_STRING PROPERTY LINK_FLAGS "/ENTRY:GsDriverEntry@8")elseif(CMAKE_SIZEOF_VOID_P  EQUAL 8)set_property(TARGET ${_target} APPEND_STRING PROPERTY LINK_FLAGS "/ENTRY:GsDriverEntry")endif()endif()
endfunction()function(wdk_add_library _target)cmake_parse_arguments(WDK "" "KMDF;WINVER" "" ${ARGN})add_library(${_target} ${WDK_UNPARSED_ARGUMENTS})set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "${WDK_COMPILE_FLAGS}")set_target_properties(${_target} PROPERTIES COMPILE_DEFINITIONS "${WDK_COMPILE_DEFINITIONS};$<$<CONFIG:Debug>:${WDK_COMPILE_DEFINITIONS_DEBUG};>_WIN32_WINNT=${WDK_WINVER}")target_include_directories(${_target} SYSTEM PRIVATE"${WDK_ROOT}/Include/${WDK_VERSION}/shared""${WDK_ROOT}/Include/${WDK_VERSION}/km""${WDK_ROOT}/Include/${WDK_VERSION}/km/crt")if(DEFINED WDK_KMDF)target_include_directories(${_target} SYSTEM PRIVATE "${WDK_ROOT}/Include/wdf/kmdf/${WDK_KMDF}")endif()
endfunction()

3、在CLion中新建C++工程

修改工程中的CMakeLists.txt,改为如下内容,注意将对应的路径修改为FindWdk.cmake存在的路径

cmake_minimum_required(VERSION 3.22)
project(CLionWdkTest1)set(CMAKE_CXX_STANDARD 17)list(APPEND CMAKE_MODULE_PATH "E:\\Exp\\Kit") #双引号中填入FindWdk.cmake所在的目录
find_package(WDK REQUIRED)wdk_add_driver(CLionWdkTest1 # sys文件名WINVER 0x0A000000 # Windows 10 1507 "Threshold"main.cpp #source code file)

修改完成后点重新加载变更

4、设置工具链

CLion-文件-设置-构建、执行、部署中,将自动检测到的VS2019编译器设置为默认-保存设置

名称可以自己命名
在这里插入图片描述
在这里插入图片描述

5、编辑工程代码

编辑main.cpp,保存后构建

#include <ntddk.h>
#define C_ABI extern "C"VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{KdPrint(("driver unloading...\n"));UNREFERENCED_PARAMETER(DriverObject);
}C_ABI
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject,PUNICODE_STRING RegistryPath)
{UNREFERENCED_PARAMETER(DriverObject);UNREFERENCED_PARAMETER(RegistryPath);DbgBreakPoint();DriverObject->DriverUnload = DriverUnload;KdPrint(("driver loading...\n"));return STATUS_SUCCESS;
}

生成信息

====================[ 构建 | CLionWdkTest1 | Debug-VS2019KMD ]====================
"C:\Program Files\JetBrains\CLion\bin\cmake\win\bin\cmake.exe" --build E:\Exp\Toy\CLionWdkTest1\cmake-build-debug-vs2019kmd --target CLionWdkTest1 -j 12
[1/2] Building CXX object CMakeFiles\CLionWdkTest1.dir\main.cpp.obj
[2/2] Linking CXX executable CLionWdkTest1.sys构建 已完成

工程目录/cmake-build-debug-<你的工具链名称>/下找到.sys文件,例如"E:\Exp\Toy\CLionWdkTest1\cmake-build-debug-vs2019kmd\CLionWdkTest1.sys"

6、测试

在这里插入图片描述

7、其他

更多信息请查阅参考资料

参考资料

stackoverflow

SergiusTheBest/FindWDK


http://www.ppmy.cn/news/335236.html

相关文章

驱动开发:配置Visual Studio驱动开发环境

在正式开始驱动开发之前&#xff0c;需要自行搭建驱动开发的必要环境&#xff0c;首先我们需要安装Visual Studio 2013这款功能强大的程序开发工具&#xff0c;在课件内请双击ISO文件并运行内部的vs_ultimate.exe安装包&#xff0c;Visual Studio的安装非常的简单&#xff0c;您…

电源变频驱动主要有哪2种驱动方式?原理如何

将变频器的主电路连接到电源线后&#xff0c;要控制电动机的运行&#xff0c;必须将外围连接的控制电路连接到相应的端子&#xff0c;并将变频器的启动模式参数设置为外部操作模式。 电源变频驱动主要有哪2种驱动方式&#xff1f;原理如何 变频器控制电动机的运行&#xff0c;共…

VS2017、2019驱动WDF开发环境找不到头文件#include <ntddk.h>

一大堆的头文件找不到报错可能是因为SDK与WDF不对应&#xff0c; 例如&#xff1a; 先看自己的WDF安装了什么版本的&#xff0c;下图是没装&#xff11;&#xff10;.&#xff10;.&#xff11;&#xff19;&#xff10;&#xff14;&#xff11;.&#xff10;WDF的文件夹 如…

windows下WDK创建免费的测试证书,并签名windows驱动文件(附带测试效果)

windows下WDK创建免费的测试证书&#xff0c;并签名windows驱动文件&#xff08;附带测试效果&#xff09; 编译了一个sys驱动文件&#xff0c;每次加载使用都需要手动开启【禁用强制驱动签名】&#xff0c;很麻烦&#xff0c;用测试证书签名&#xff0c;再也不用这么麻烦了&a…

支付宝正式发布黑莓版客户端

5月28日&#xff0c;支付宝对外表示&#xff0c;该公司已正式发布黑莓版客户端&#xff0c;目前支付宝无线支付单项日交易笔数已接近20万笔。 据了解&#xff0c;自去年6月开始&#xff0c;支付宝已经相继发布了基于Windows Mobile、塞班、iPhone、J2ME、黑莓等手机系统平台的软…

黑莓bb10和Android,黑莓BB10.3.1更新发布,全面支持安卓应用

目前在国内市场仍拥有小众粉丝的黑莓手机日前发布了最新的BB10系统更新版本&#xff0c;最新版本来到了10.3.1&#xff0c;该版本刚刚发布&#xff0c;将陆续开始通过官方更新、亚马逊等应用商店开始推送。 据悉黑莓BB10.3.1版本最大的改变是增加了BlackBerry Blend的特性&…

黑莓9810JAVA,黑莓9810 中文刷机包

这是黑莓9810 中文刷机包&#xff0c;整理了一下有关黑莓9810的官方固件包了&#xff0c;有没有需要的呢&#xff0c;这里把固件包整理一下也是方便大家以后好下载了&#xff0c;因此有一些机友还是不知道在哪里下载官方的rom包&#xff0c;在这里说的固件rom包也就是原版的系统…

bb10系统支持java吗_黑莓BB10怎么样 BlackBerry 10系统好用吗?

曾几何时黑莓Blackberry OS是一款十分受用户欢迎的手机系统&#xff0c;不过随着手机系统市场已经被苹果iOS、谷歌安卓、微软Windows Phone三分天下&#xff0c;致使曾经的黑莓帝国逐渐沦陷&#xff0c;体验和性能都已经明显跟不上iOS与安卓等系统的脚步了&#xff0c;也因为如…