{"componentChunkName":"component---src-templates-post-template-js","path":"/windows-windriver-001-tutorial-en","result":{"data":{"markdownRemark":{"id":"aad3f4db-704d-5969-beb3-6aa1495dcf7b","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/windows-windriver-001-tutorial\">original page</a>.</p>\n</blockquote>\n<p>When I tried Windows kernel debugging, one obstacle I ran into was that there are very few kernel drivers with detailed public specifications.</p>\n<p>If there wasn’t one, I figured I would just make one myself, so I started developing a kernel driver.</p>\n<p>For kernel driver development, I am basically using the following book as a reference.</p>\n<p>Reference: <a href=\"https://amzn.to/3H3WMoe\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Windows Kernel Driver Programming</a></p>\n<p>I summarized how to configure kernel debugging in the following article.</p>\n<p>Reference: <a href=\"/windows-windbg-004-kernel-debug\">A First Step Toward Kernel Debugging a Windows 10 Environment with WinDbg</a></p>\n<!-- omit in toc -->\n<h2 id=\"table-of-contents\" style=\"position:relative;\"><a href=\"#table-of-contents\" aria-label=\"table of contents permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Table of Contents</h2>\n<ul>\n<li>\n<p><a href=\"#create-your-first-driver\">Create your first driver</a></p>\n<ul>\n<li><a href=\"#what-is-wdm\">What is WDM?</a></li>\n<li><a href=\"#simple-code\">Simple code</a></li>\n<li><a href=\"#driverentry\">DriverEntry</a></li>\n<li><a href=\"#firstdriverunload\">FirstDriverUnload</a></li>\n<li><a href=\"#system-threads-kernel-mode-system-threads\">System threads (kernel-mode system threads)</a></li>\n<li><a href=\"#build-the-driver\">Build the driver</a></li>\n<li><a href=\"#set-the-target-platform\">Set the target platform</a></li>\n<li><a href=\"#load-the-kernel-driver\">Load the kernel driver</a></li>\n<li><a href=\"#the-service-cannot-be-stopped\">The service cannot be stopped</a></li>\n<li><a href=\"#reload-the-service\">Reload the service</a></li>\n<li><a href=\"#check-the-service\">Check the service</a></li>\n<li><a href=\"#confirm-that-the-driver-is-loaded-into-the-system\">Confirm that the driver is loaded into the system</a></li>\n<li><a href=\"#add-the-kdprint-macro\">Add the KdPrint macro</a></li>\n<li><a href=\"#configure-debugview\">Configure DebugView</a></li>\n</ul>\n</li>\n<li><a href=\"#analyze-the-custom-driver-with-kernel-debugging\">Analyze the custom driver with kernel debugging</a></li>\n<li><a href=\"#summary\">Summary</a></li>\n</ul>\n<h2 id=\"create-your-first-driver\" style=\"position:relative;\"><a href=\"#create-your-first-driver\" aria-label=\"create your first driver permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create your first driver</h2>\n<p>Let’s start by creating our first driver.</p>\n<p>Create a WDM project in Visual Studio.</p>\n<p>At the time of writing this article, on 2021/12/01, Visual Studio 2022 did not support kernel driver development.</p>\n<p>For that reason, if you have updated to Visual Studio 2022 or later, you need to be careful because you will not be able to create a WDM project.</p>\n<p>This time I am using Visual Studio 2019.</p>\n<h3 id=\"what-is-wdm\" style=\"position:relative;\"><a href=\"#what-is-wdm\" aria-label=\"what is wdm permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is WDM?</h3>\n<p>WDM stands for <code class=\"language-text\">Microsoft Windows Driver Model</code>, and it is the architecture for device drivers on Windows 2000 and later.</p>\n<p>It is now a deprecated driver model.</p>\n<p>If you are creating a kernel driver today, <code class=\"language-text\">Windows Driver Foundation(WDF)</code> or universal Windows drivers are probably the mainstream choice.</p>\n<p>Reference: <a href=\"https://docs.microsoft.com/ja-jp/windows-hardware/drivers/kernel/introduction-to-wdm\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Introduction to WDM - Windows drivers | Microsoft Docs</a></p>\n<p>There are three types of WDM drivers:</p>\n<ul>\n<li>bus driver</li>\n<li>function driver</li>\n<li>filter drivers</li>\n</ul>\n<p>Reference: <a href=\"https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/bus-drivers\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Bus Drivers - Windows drivers | Microsoft Docs</a></p>\n<p>Reference: <a href=\"https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/function-drivers\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Function Drivers - Windows drivers | Microsoft Docs</a></p>\n<p>Reference: <a href=\"https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/filter-drivers\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Filter Drivers - Windows drivers | Microsoft Docs</a></p>\n<h3 id=\"simple-code\" style=\"position:relative;\"><a href=\"#simple-code\" aria-label=\"simple code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Simple code</h3>\n<p>After creating a WDM project in Visual Studio, add a C++ source file with any name and add the following code.</p>\n<div class=\"gatsby-highlight\" data-language=\"c++\"><pre class=\"language-c++\"><code class=\"language-c++\">#include &lt;ntddk.h&gt;\n\nvoid FirstDriverUnload(_In_ PDRIVER_OBJECT DriverObject)\n{\nUNREFERENCED_PARAMETER(DriverObject);\n}\n\nextern &quot;C&quot;\nNTSTATUS\nDriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath)\n{\nUNREFERENCED_PARAMETER(DriverObject);\nUNREFERENCED_PARAMETER(RegistryPath);\n\nreturn STATUS_SUCCESS;\n}</code></pre></div>\n<h3 id=\"driverentry\" style=\"position:relative;\"><a href=\"#driverentry\" aria-label=\"driverentry permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>DriverEntry</h3>\n<p><code class=\"language-text\">DriverEntry</code> is the entry point of the driver module.</p>\n<p><code class=\"language-text\">DriverEntry</code> is called by a system thread (kernel-mode system thread) at IRQL <code class=\"language-text\">IRQL_PASSIVE_LEVEL(0)</code>.</p>\n<h3 id=\"firstdriverunload\" style=\"position:relative;\"><a href=\"#firstdriverunload\" aria-label=\"firstdriverunload permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>FirstDriverUnload</h3>\n<p><code class=\"language-text\">FirstDriverUnload</code> defines the unload routine for the driver.</p>\n<p>There is no problem if you change the function name to something other than <code class=\"language-text\">FirstDriverUnload</code>.</p>\n<p>Although it is not yet defined in the sample code above, assigning it to <code class=\"language-text\">DriverObject</code>’s <code class=\"language-text\">DriverUnload</code> defines the function that is called when the driver module is unloaded.</p>\n<div class=\"gatsby-highlight\" data-language=\"c++\"><pre class=\"language-c++\"><code class=\"language-c++\">// アンロードルーチンを定義\nDriverObject-&gt;DriverUnload = FirstDriverUnload;</code></pre></div>\n<p>In the case of a kernel driver, resources such as memory need to be released when it is unloaded, so cleanup processing is defined in this unload routine.</p>\n<h3 id=\"system-threads-kernel-mode-system-threads\" style=\"position:relative;\"><a href=\"#system-threads-kernel-mode-system-threads\" aria-label=\"system threads kernel mode system threads permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>System threads (kernel-mode system threads)</h3>\n<p>The threads running in the System process (Process ID 4) are system threads, and they run in kernel mode.</p>\n<p>System threads execute kernel-mode code in <code class=\"language-text\">Ntoskrnl.exe</code> or in loaded device drivers.</p>\n<p>Reference: <a href=\"https://amzn.to/3ei56Eg\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Inside Windows 7th Edition, Part 1</a></p>\n<h3 id=\"build-the-driver\" style=\"position:relative;\"><a href=\"#build-the-driver\" aria-label=\"build the driver permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Build the driver</h3>\n<p>Once you have created the minimum code, build the kernel driver.</p>\n<p>For now, build it as a debug build by pressing [Ctrl+Shift+B].</p>\n<h3 id=\"set-the-target-platform\" style=\"position:relative;\"><a href=\"#set-the-target-platform\" aria-label=\"set the target platform permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Set the target platform</h3>\n<p>This time, because I am building a driver module for an x64 environment, I set [Active solution platform] to [x64] in the Visual Studio project properties.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 944px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/e5202bb713f0239c106e61aa9276e9d5/966a0/image-58.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 75.41666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAACu0lEQVQ4y41TS08TURSeH0DCig1pEDCgCa8N/gckboj/g3/QhQsXmCBuCW4AY4GakCBqQmq7wIUYQyAQGzt9t9PpPDswnU6ffJ5zSxF1gTf55t5z7z3f+c45d6Sjo68wDB2ZTAalUgmapkHX9S5orWllFItFcVYul6+hIpfLwbYs7O3tIRAIYGhoSEA6PPyCRCKJH/GfKCoqlJKKhJxCLl+EbljQdBPZXAH5AtsmDNOiYCYy2TzsioP9/Q9/Eh59+w45mSEyDWXNIgcHqXSOnG20r4BWB3A9n85L8Go+rmivQ2jzh0Y0GsXg4OBvwtPTU1gk3TRNeF4VFeeS0skLu9lsCifHcaAoCkxS6DgX6HQ6aLXb4iwWiwmikZERDA8PQzo5OYGqqqJOruvi4tKFnJCRTKZQI0U8DN3A+fk54vG4qCWTtVotcXZwcIC+vj709/djYGCgS6gQWbGowPd9UmYhm82iQvVhhfV6XQSybRuXFKy3x3fbRJxOp7G6uoqNjQ0sLi5eE1I6CVJlWbZQyx0vFAqQZVnYvZIYhoF8Pn8DDsyvwa/VhNpwONytIW/26laiLnNqqVRGOMly8tpOdUGKVLUsyFV6QjqVI0PErlvF+vo6KSRCll6jKAyNyPmtsVpOrd3uiCC8V6lUUKJuswAuAZeCfbhRPLa2tiEdHx+LzW6NuMNZkSJ3lgkYvO7Zf695Zl9u0ubmG0hnZ2eoVquCxPM8kTZDXL7lfJvg9ppn9u0SbkIKBoNYWXmF5eWXWFp6gbW11/843wUm5O6HQiFIY2MPMTo6Bp4DgXuYn39ykwZf5PkuWJRRq9lAeGcH0uTkNKamZjAxMUXE97Gw8BSNRlO8s/9Bve7DvnDhNq4Q2n4HaXb2ERjT0zMYH3+AubnH4neKRCKEz3cgglj0M3bff8Lb3Y8IPnuOX6ZgBqT8x/wCAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/e5202bb713f0239c106e61aa9276e9d5/8ac56/image-58.webp 240w,\n/static/e5202bb713f0239c106e61aa9276e9d5/d3be9/image-58.webp 480w,\n/static/e5202bb713f0239c106e61aa9276e9d5/59b61/image-58.webp 944w\"\n              sizes=\"(max-width: 944px) 100vw, 944px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/e5202bb713f0239c106e61aa9276e9d5/8ff5a/image-58.png 240w,\n/static/e5202bb713f0239c106e61aa9276e9d5/e85cb/image-58.png 480w,\n/static/e5202bb713f0239c106e61aa9276e9d5/966a0/image-58.png 944w\"\n            sizes=\"(max-width: 944px) 100vw, 944px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/e5202bb713f0239c106e61aa9276e9d5/966a0/image-58.png\"\n            alt=\"image-58.png\"\n            title=\"image-58.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>If you leave this at the default setting, a driver module for an x86 environment will be built, which causes the problem that it fails even if you try to start it on 64-bit Windows.</p>\n<h3 id=\"load-the-kernel-driver\" style=\"position:relative;\"><a href=\"#load-the-kernel-driver\" aria-label=\"load the kernel driver permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Load the kernel driver</h3>\n<p>Once the driver has been built, place the built driver module in the virtual machine as <code class=\"language-text\">Z:\\FirstDriverSample.sys</code>.</p>\n<p>Note: Any folder and driver name are fine here.</p>\n<p>Next, use the <code class=\"language-text\">sc</code> command to load the driver you created as the <code class=\"language-text\">001_sample</code> service.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\"><span class=\"token function\">sc</span> create 001_sample <span class=\"token function\">type</span>= kernel binPath= Z:\\FirstDriverSample<span class=\"token punctuation\">.</span>sys</code></pre></div>\n<p>If it succeeds, you will see a screen like the following, and the service you added is registered under <code class=\"language-text\">HKLM\\SYSTEM\\CurrentControlSet\\Services</code> in the registry.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/817cd8b424ed771b6a711b20326e9a20/0b533/image-22.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 80%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAARlAAAEZQAGA43XUAAAB0UlEQVQ4y6WT227aMBzGzVFI3PCa3WWhWqXuMRBQngEhTm+wabftzS4mrQu0FMg5ThwnDShfbUOAbhdNN0u/+PT35892/uTHwyNWax2rjSkwYFo2TNOC7XhwPQrHFTUNBD4c2xFjvupTAfMDBJQqvEMs8cTH1h/w/LzE0+JRiJlYLpciwFNtxhg45+BhuK8FcRz/xXa7xW63A+FxAk7nMA0dtu1DFrk4TVPhguIjRa4hjMdgrgbLMvH123f0bm/R7/fR6/UUWbvb7R7pdDpHZL/dbkPTNCVKQh4hcH7D9yk+X1+DEIJ6vY5yuYxKpaJqOfYek8lkL8gPgowFuLn5okQajQZqtRpKpZJCimYb/Em1WlWCs9ns5JBav8SlJmi1rtSkDMzj6pzpdHoSdI2f4vVCNJstNVksFnMLFQqFt4Is5OpRkiTG5WVTTcpj/rNgKP4reeQ45keH8r5kYB6y05zd4d5hFHFcXHz68N1ljMfjg6A8sqdB1ze4u7vHYDBQk6PRKDfD4RDr9frkMPKfYBgb/G9RmSLzMPTmIu0svLwkKh+zvMyLjJdiyqEU5P5CWF6Jl94ed8oCkOa1t6+I4zqgzgKLuXyY6K1gehb8Hoe4V6zTlknLrnNnAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/817cd8b424ed771b6a711b20326e9a20/8ac56/image-22.webp 240w,\n/static/817cd8b424ed771b6a711b20326e9a20/d3be9/image-22.webp 480w,\n/static/817cd8b424ed771b6a711b20326e9a20/b0a15/image-22.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/817cd8b424ed771b6a711b20326e9a20/8ff5a/image-22.png 240w,\n/static/817cd8b424ed771b6a711b20326e9a20/e85cb/image-22.png 480w,\n/static/817cd8b424ed771b6a711b20326e9a20/0b533/image-22.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/817cd8b424ed771b6a711b20326e9a20/0b533/image-22.png\"\n            alt=\"image-22.png\"\n            title=\"image-22.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Next, start the service you added.</p>\n<p>If you run the <code class=\"language-text\">sc start 001_sample</code> command, startup fails.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">> <span class=\"token function\">sc</span> <span class=\"token function\">start</span> 001_sample\n<span class=\"token namespace\">[SC]</span> StartService FAILED 1275:\nこのドライバーの読み込みはブロックされています</code></pre></div>\n<p>This is because 64-bit system drivers require a signature, while the driver I created myself does not have one.</p>\n<p>To work around this error, boot the virtual machine you use to verify the driver in test-signing mode.</p>\n<p>Run the following command and reboot.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">bcdedit <span class=\"token operator\">/</span><span class=\"token function\">set</span> testsigning on</code></pre></div>\n<p>Note: The system does not switch to test-signing mode until the OS is rebooted.</p>\n<p>Note: If Secure Boot is enabled, switching to test-signing mode will fail.</p>\n<p>To reboot, run the following command from a command prompt started with administrator privileges.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">shutdown <span class=\"token operator\">/</span>r <span class=\"token operator\">/</span>t 0</code></pre></div>\n<p>After rebooting, if you start the service again, the registered service starts.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">> <span class=\"token function\">sc</span> <span class=\"token function\">start</span> 001_sample\nSERVICE_NAME: sample\n        <span class=\"token function\">TYPE</span>               : 1  KERNEL_DRIVER\n        STATE              : 4  RUNNING\n                                <span class=\"token punctuation\">(</span>STOPPABLE<span class=\"token punctuation\">,</span> NOT_PAUSABLE<span class=\"token punctuation\">,</span> IGNORES_SHUTDOWN<span class=\"token punctuation\">)</span>\n        WIN32_EXIT_CODE    : 0  <span class=\"token punctuation\">(</span>0x0<span class=\"token punctuation\">)</span>\n        SERVICE_EXIT_CODE  : 0  <span class=\"token punctuation\">(</span>0x0<span class=\"token punctuation\">)</span>\n        CHECKPOINT         : 0x0\n        WAIT_HINT          : 0x0\n        PID                : 0\n        FLAGS              :</code></pre></div>\n<p>Next, stop the service.</p>\n<h3 id=\"the-service-cannot-be-stopped\" style=\"position:relative;\"><a href=\"#the-service-cannot-be-stopped\" aria-label=\"the service cannot be stopped permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The service cannot be stopped</h3>\n<p>Even if you try to stop the service with the driver module up to this point, stopping fails.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">> <span class=\"token function\">sc</span> stop 001_sample\n<span class=\"token namespace\">[SC]</span> ControlService FAILED 1052:s\n要求された制御はこのサービスに対して無効です。</code></pre></div>\n<p>The same happens if you use a tool such as ProcessHacker to stop the service.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 960px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/c18158a0b8a4ca06a2913b1f95005dbb/2bef9/image-62-1024x756.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 73.75%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAARlAAAEZQAGA43XUAAACN0lEQVQ4y31UibKiMBDk/39vtZ6CgCI3yH2K2K8niKuutamaIoFJz9E9aHESoW1b1HVNazAMA263m7J5njHxeb/f8brkvJr4jeOI6/WKoiyh/ex+EHgubMuGYRyQpimyS4YwDBFFMZIkwSWJkeU56qpGyUsSvOs6ZVVVIYkT5Pzu+QG0zeYPL6UKYL/f8xkhjmMEQUDAiM4xLMuCruvqXVEU6PteZScViK0VSHDNsk3WAOXkMVOJNk0TekZvmqUFASOHYUCfAd/WCijBNJ1ZSeoCeDgcVBayF1tL830fruuq8j57uNoCyB7q+l41tetaOCcHRV6gI0lNXSmAllm65zNOpyP7VyyALPOToGeGe8NQDEkmlmnhzMt+lCDKS5RVg7YbkF5yxOkFddvhOs3ox0ntIWCfJVtkV/qkANl81/ORkd3+sEPbL+8bybhpKa+lr1JRw0DzI9M3QIPsSbNFiwklI0vk0VGf4zjwW6cIEuCRgWsCe0mBOKueQO+kUHsqQwIe7aPSU8lLYZrBMG04roejc4Z1PPFswT772Jou/CT/3sOdbpKIXGVgsJ/CqOyFYds+KY16nkdSHEQPSf3N7AugyagC0LaNIkUui0Tko1hD4EBkQ7K+yebfHpomqpIS4Ug5joM8y5+6FEGX1FbE6VmE3VMRo2rRK/A8v5ZMYTePn4Jt22pSREYTLWW5wqoAi5xk/78lc65ttxvl7LHMdVJklmW2JaI4xdFyznlulYSaN1snSnx+Ae3/e/VyADnqAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/c18158a0b8a4ca06a2913b1f95005dbb/8ac56/image-62-1024x756.webp 240w,\n/static/c18158a0b8a4ca06a2913b1f95005dbb/d3be9/image-62-1024x756.webp 480w,\n/static/c18158a0b8a4ca06a2913b1f95005dbb/e46b2/image-62-1024x756.webp 960w,\n/static/c18158a0b8a4ca06a2913b1f95005dbb/a9a89/image-62-1024x756.webp 1024w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/c18158a0b8a4ca06a2913b1f95005dbb/8ff5a/image-62-1024x756.png 240w,\n/static/c18158a0b8a4ca06a2913b1f95005dbb/e85cb/image-62-1024x756.png 480w,\n/static/c18158a0b8a4ca06a2913b1f95005dbb/d9199/image-62-1024x756.png 960w,\n/static/c18158a0b8a4ca06a2913b1f95005dbb/2bef9/image-62-1024x756.png 1024w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/c18158a0b8a4ca06a2913b1f95005dbb/d9199/image-62-1024x756.png\"\n            alt=\"image-62-1024x756.png\"\n            title=\"image-62-1024x756.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>This is because the unload routine mentioned earlier has not been implemented in the device driver.</p>\n<p>Reference: <a href=\"https://proc-cpuinfo.fixstars.com/2017/06/windows-device-driver-1/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Checking the Basic Behavior of Windows Device Drivers (1) - Fixstars Tech Blog /proc/cpuinfo</a></p>\n<p>For that reason, I modified the code as follows.</p>\n<div class=\"gatsby-highlight\" data-language=\"c++\"><pre class=\"language-c++\"><code class=\"language-c++\">#include &lt;ntddk.h&gt;\n\nvoid FirstDriverUnload(_In_ PDRIVER_OBJECT DriverObject)\n{\nUNREFERENCED_PARAMETER(DriverObject);\n}\n\nextern &quot;C&quot;\nNTSTATUS\nDriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath)\n{\nUNREFERENCED_PARAMETER(RegistryPath);\n\n    // アンロードルーチンを定義\nDriverObject-&gt;DriverUnload = FirstDriverUnload;\n\nreturn STATUS_SUCCESS;\n}</code></pre></div>\n<p>After reloading the device driver with this change and starting the service, running the <code class=\"language-text\">sc stop 001_sample</code> command makes it possible to stop the service.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">> <span class=\"token function\">sc</span> stop 001_sample\nSERVICE_NAME: 001_sample\n        <span class=\"token function\">TYPE</span>               : 1  KERNEL_DRIVER\n        STATE              : 1  STOPPED\n        WIN32_EXIT_CODE    : 0  <span class=\"token punctuation\">(</span>0x0<span class=\"token punctuation\">)</span>\n        SERVICE_EXIT_CODE  : 0  <span class=\"token punctuation\">(</span>0x0<span class=\"token punctuation\">)</span>\n        CHECKPOINT         : 0x0\n        WAIT_HINT          続いて、サービスの確認を行います。</code></pre></div>\n<h3 id=\"reload-the-service\" style=\"position:relative;\"><a href=\"#reload-the-service\" aria-label=\"reload the service permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reload the service</h3>\n<p>To reload the service, you need to delete the service you previously registered once.</p>\n<p>The following commands let you delete and re-register the service, so save them as an appropriate batch file such as <code class=\"language-text\">reload.bat</code> and use that.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\"><span class=\"token function\">sc</span> stop 001_sample\n<span class=\"token function\">sc</span> delete 001_sample\n<span class=\"token function\">sc</span> create 001_sample <span class=\"token function\">type</span>= kernel binPath= Z:\\FirstDriverSample<span class=\"token punctuation\">.</span>sys\n<span class=\"token function\">sc</span> <span class=\"token function\">start</span> 001_sample</code></pre></div>\n<p>Next, check the registered kernel driver service.</p>\n<h3 id=\"check-the-service\" style=\"position:relative;\"><a href=\"#check-the-service\" aria-label=\"check the service permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Check the service</h3>\n<p>Before that, let me briefly touch on what a service is.</p>\n<p>In Windows, a service refers to a process started by the Service Control Manager (SCM).</p>\n<p>When you add a kernel driver, it is added as a service, which is confusing, but in many cases it seems that “Windows services” and “driver services” are distinguished.</p>\n<p>Inside Windows also explicitly states that a “service” is a user-mode process started by the SCM, and that device drivers are not treated as services.</p>\n<p>Reference: <a href=\"https://amzn.to/3ei56Eg\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Inside Windows 7th Edition, Part 1</a></p>\n<p>Reference: <a href=\"https://docs.microsoft.com/ja-jp/windows/win32/services/service-control-manager\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Service Control Manager - Win32 apps | Microsoft Docs</a></p>\n<p>As confirmed earlier, a kernel driver is registered as a subkey under <code class=\"language-text\">HKLM\\SYSTEM\\CurrentCntrolSet\\Services</code> and is started by the SCM.</p>\n<p>Here, the services registered under <code class=\"language-text\">HKLM\\SYSTEM\\CurrentCntrolSet\\Services</code> are distinguished between kernel drivers and Windows services, and when the value of each subkey’s [Type] is a low numeric value, it means the service is a kernel driver; a value of 0x10 or 0x20 means it is registered as a Windows service.</p>\n<p>You can verify the registered kernel driver service using tools such as ProcessHacker or Proexp.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 835px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/3ed7b1a1f81b29509725d9a3d2f23633/f0685/image-59.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 81.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAACpUlEQVQ4y3VUaY+bMBTM//9pVT+1ym4uwEA4w2EgHAmQ6bzHRlpVrSXL4Jh5czxnl2Qx2qZBVZWwtkFZlOi6Ox6PBx7TA+M4YZomPJcVM9/xeuEFWbb177EzgY8sTZAmEYwfIEkS1FUNW1vUZYWyZKG6Rt20sMUNj3FkgW2O44C+73UOX+vucPxEGIZkWMPzXOz3e+R5rkBVXSGKYrgmQJbfcDhd4Pkhfv3eww+uSLkXXCM0VNQPouSB3flyQhCEmOcZeZbBGEPG2QZIhkGU4Ox4cD2DKE5RsHCS5ShKsqZFlswHAglo3d6xc7wz2rZV/VVVwfd9FgjQ8OC6LOiGUW24Xq9orIXxPBRFwb0YcRxr4dPxiGsUoawb7FzXQUDvNsASR/4oB2+3GxYCtveBzwWGYVAVEpLsy/vz+fzaGzXQYgO8IKVEBWQ1Ydjwx7bt1OjKtvSH5t/v+uHI51GDmTQES9YCXjG4orIENA5DuCmgJGvoVRiEKl8Am65HToYi+XQ6aUHxWT3mGbEnotxMgmTxXRhn+sPGkB6azcMkTZWBAIo/4p/rujrFkoa9q4w5U57NaZECysdptkmuSduQgVSUUKR5BTBlqtbWyvByuei5Oy0QUAlUnmuqUw8DAkiK75QljO9DQumHrYnXddU9CUXCkPU9J96s0koonq+N/A5Fmlxk6CDD9t5jWV/vV2X9rzETVCU77HiRrYC8x67j0kejkt8M50WYvbb7+48p40nGytAPYziOo7SF4eFw0AIZfZV3aZv1P6y+j5UqlKG0wFtyQf/E9CROtqSZZtV09GvRW7P5NfNj8eyJqh3Q9pP2oYSjoZyNpz3WdZ1eKcO2Kdh30rDipVyns8ve5J+EH0bbyj51wgQ/Pgx+fjj4PDn4OBwJaPEHXr3AFDq1+HQAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/3ed7b1a1f81b29509725d9a3d2f23633/8ac56/image-59.webp 240w,\n/static/3ed7b1a1f81b29509725d9a3d2f23633/d3be9/image-59.webp 480w,\n/static/3ed7b1a1f81b29509725d9a3d2f23633/2321d/image-59.webp 835w\"\n              sizes=\"(max-width: 835px) 100vw, 835px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/3ed7b1a1f81b29509725d9a3d2f23633/8ff5a/image-59.png 240w,\n/static/3ed7b1a1f81b29509725d9a3d2f23633/e85cb/image-59.png 480w,\n/static/3ed7b1a1f81b29509725d9a3d2f23633/f0685/image-59.png 835w\"\n            sizes=\"(max-width: 835px) 100vw, 835px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/3ed7b1a1f81b29509725d9a3d2f23633/f0685/image-59.png\"\n            alt=\"image-59.png\"\n            title=\"image-59.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Next, let’s also confirm that the driver (<code class=\"language-text\">.sys</code> file) is loaded in the system.</p>\n<h3 id=\"confirm-that-the-driver-is-loaded-into-the-system\" style=\"position:relative;\"><a href=\"#confirm-that-the-driver-is-loaded-into-the-system\" aria-label=\"confirm that the driver is loaded into the system permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Confirm that the driver is loaded into the system</h3>\n<p>In Proexp, open [DLLs] from [Lower Pane View].</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 786px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/92071d128e5860eb218f94bc82b0ce0b/321ea/image-60.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 100.83333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAES0lEQVQ4y1VUiW7bRhDVN7dA0KBAPyABCjRtgRQBEuRoDiRwkBaJ4zOOY8l2bImiqJMURYqieIgiJUqUZR3269s1GrQLPOxyZ/dx5s3s5KJBiJ7rom65sPoDhIGPMAy/YTAYYJKmGI1GGKcTjMepnC8XSyxXa6yIxXJFLLleIef7MeIoQBRHcFwP3W4XvV6PF8cSk8kEcRzLH6WRiyzsYpGNcJmNkcYB4tDFLI2RpQmSJEGu73vYqQQ4aCSwbAdmuw1dN+D7PrIsk4STdAS7H+EvJcKH6gT7xgL77aXErj7HgXWNSgg5cgndP6zY2K4OoNv00OrAIKlNT/t9j2EPMCNxPxxip2TjoOoTHg7rIU7NEY6aA+T1BN2xoLtGzgtjuHYHLSdCw7DhWCbcngPHtuB7fYyTmIRTJIMAQ0tH5jmYBT1kfg9Tr8u9FmZ+l2Qr0tFDIaTnBShoFmp2AMN20fWHCJIMfjKFF0/gDUY4bzu487mA307O8EtJxT2tiZ8LCu5+OsHdvIKtcHIT8pKEuFrhkK5vnLnYKOjYpKafzRk+tac4MDPsNUf4SI33TBdlaq4Qn3UTv77+gjuPt3Hn6T7+PjZvCNfrFbLZHB0mJPJd2GYbkxHDnIwxm47lvJhnsNoG7v/2B36/dx8PHzzBg/uP8OMPP+HW97dx67vb+PPJixvCq6srDIcJDL0Dy7JQUTUmxeRejJQZHrF0pkxKr+vi4P0nnOzkUf5Sgl5sQTks4uveMcqc035yQyg0dHse7FYbQRjAtm0EQShLJmVBi7K5mF/Ac/oofsijvl+EunWK2l4J1d1zKJvHqG6fIWkFWK1lYfus/BTDZETiHur1OpPky2IW8P2Ahe7A80PUWz1UajZ0M4BpRzA6IVptD22LLyy6wHQ6Ra7VamG9Xkt3RXjlcpmFrcvwhYedTgfn5+cwDB2r5SVqNY32JiVqos71iHr/d+RqtZokEDoJPUWo8/kcs9lMvs3Ly8v/7ZVKJf5UhaqqctY0DS57wdXVNa6vWdgaDR3TxGqxwAUvirBteiUaghiDIODrsRBQGvFiFBJWGIW4V+a6qmmycQiyG0I+sw4viZHSI9UwoNLjJp/egAQWbUqzCSeKsOCZMiUSd2r8qVirhPD8W8hfnz+H+uoV3N1d2FtbKL98ieqbNzDfv4e1uYnG27dyT8zO9jYKjx7hmDh79gyKuPvuHZPoyTYniHM1Hna4mR0dIfz4EQ0ear54IedoZwc2ibSnT2G8fo0hv8skqzx+DFXg4UPUNjZk8i6o8ZI9MVelfn1q8G/IFYbTYLgaQ5oySc5wiDJlaNOLCxEy5ajwjkY0HLa7fl/Wquw1QsMaRRWii5HRIL4N6iKSMWNdCVu9WpXzkhlXFQXVSkVCJEbYRCP+puHp6SlOTk5k6VRpFN+i7hqNhkSxWET+KI98oYAKSYRNoERiRSlzXeS5Jkx63OaT/Qd/JJ8rm+28SgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/92071d128e5860eb218f94bc82b0ce0b/8ac56/image-60.webp 240w,\n/static/92071d128e5860eb218f94bc82b0ce0b/d3be9/image-60.webp 480w,\n/static/92071d128e5860eb218f94bc82b0ce0b/4cb1e/image-60.webp 786w\"\n              sizes=\"(max-width: 786px) 100vw, 786px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/92071d128e5860eb218f94bc82b0ce0b/8ff5a/image-60.png 240w,\n/static/92071d128e5860eb218f94bc82b0ce0b/e85cb/image-60.png 480w,\n/static/92071d128e5860eb218f94bc82b0ce0b/321ea/image-60.png 786w\"\n            sizes=\"(max-width: 786px) 100vw, 786px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/92071d128e5860eb218f94bc82b0ce0b/321ea/image-60.png\"\n            alt=\"image-60.png\"\n            title=\"image-60.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>As shown in the image, you can confirm that the driver module you created is loaded.</p>\n<h3 id=\"add-the-kdprint-macro\" style=\"position:relative;\"><a href=\"#add-the-kdprint-macro\" aria-label=\"add the kdprint macro permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Add the KdPrint macro</h3>\n<p>Next, add a print routine to the device driver.</p>\n<p>Before that, to make DebugView capture the output of <code class=\"language-text\">KdPrint</code> from the kernel driver, create a [Debug Print Filter] subkey under <code class=\"language-text\">HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager</code>, add a DWORD key named <code class=\"language-text\">DEFAULT</code>, and set its value to 1.</p>\n<p>Note: You need to reboot the OS for this setting to take effect.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 660px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/024db07d72761c7ed65f81cf9a321a17/1f083/image-64.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 124.58333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAYAAAAxFw7TAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC/klEQVQ4y6WVSXPaQBCF+f+nXFIunwIHV8q55Z5U5RTH8YKNMZLQjpBAAiEJtKD1pWcAb7FTAU9VFxoB33TPvH7T+m168N0pNN1AEETIixJ5nmO9XqMoCqRpiqZpwEZd1zzYYO/qqkazm2+/ay2XEaIogufN4XsaLFOCphlQVQ1hGNEiAVzPQ7haoaI/sph4MyS0KFsmJ0iUJCjqBiXNW2VZMTxoMTSpClU8gzTUKNMcu2HLKoSfv7ByHIDA48GARzGfo6YFh5eXULpdrGnhFiurqioC1CgTHc6oD2+2wLYunpH7/Qecj0fwO22kp6dYdDpwjo/htzdz9swiOjnZAOu6QlFSyrGK+USC7bjIsowzy7JEtFw+ZNvg+Xg5fwDmxQZomz2IogJ5OKTqYn4wbC9Zts1bUT8+v8hQhqXfQBBkCLRHrutRpmt+aLuT3QW28XT+DMj2MF9ptIc9KIoBQ9d5OQ0dWBiGfwN3JT+ZPwHWVFqJskiRBXcwDQOWNYZDp7qiU42i5aHAHNniBpqqwB47lDUJmzT2asn/BSzXSP1rSMIA1timLsl4x4QHA4sNUCSgN5sjpnJZ670LmAU9WKMR4jh+1OG7gLSH1sikQ7FgmiOSjkt9PjsQmGdIvUvIImlwMkFKJQeLBXWOcwAwp54m5SW5BGFswSVBsxGTq9i2vSeQzIE6D7muYdz+gO7RJwjtL9A7nxEO7uGQi+wJLMnPCGjfYvrtK/yzcywvLoD7e4Qkcns63bdkypCZw9rA1NcwS3IE5Dbl1kDn5Ht7Azduo0CXzslpNCTxCjmJuiLZHKDDrdskGkz1GubI4dafJinXIbevfYAV20PKsIg1uLaA5SrlLcd+8BaweWFfD37I7J/5LrtT6lTDhOxLN8aYkA4ZjH0fBOGrwNcybrH9YeUtFvQ5k0nUXbqkFPi+z/WZkNvsMvzX2F2xLYGMoN/v4+b2Fr3eHY+rqyt06RYTRZFCgiRJGA5lMl66GmSF7E3j72VZ5tetNbJ4vzMj/gOlaYYc0p/6eQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/024db07d72761c7ed65f81cf9a321a17/8ac56/image-64.webp 240w,\n/static/024db07d72761c7ed65f81cf9a321a17/d3be9/image-64.webp 480w,\n/static/024db07d72761c7ed65f81cf9a321a17/cc661/image-64.webp 660w\"\n              sizes=\"(max-width: 660px) 100vw, 660px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/024db07d72761c7ed65f81cf9a321a17/8ff5a/image-64.png 240w,\n/static/024db07d72761c7ed65f81cf9a321a17/e85cb/image-64.png 480w,\n/static/024db07d72761c7ed65f81cf9a321a17/1f083/image-64.png 660w\"\n            sizes=\"(max-width: 660px) 100vw, 660px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/024db07d72761c7ed65f81cf9a321a17/1f083/image-64.png\"\n            alt=\"image-64.png\"\n            title=\"image-64.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Next, rewrite the device driver code as follows, and after building, reload the device driver.</p>\n<div class=\"gatsby-highlight\" data-language=\"c++\"><pre class=\"language-c++\"><code class=\"language-c++\">#include &lt;ntddk.h&gt;\n\nvoid FirstDriverUnload(_In_ PDRIVER_OBJECT DriverObject)\n{\nUNREFERENCED_PARAMETER(DriverObject);\n\nKdPrint((&quot;This driver unloaded\\n&quot;));\n}\n\nextern &quot;C&quot;\nNTSTATUS\nDriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath)\n{\nUNREFERENCED_PARAMETER(RegistryPath);\n\nDriverObject-&gt;DriverUnload = FirstDriverUnload;\n\nOSVERSIONINFOEXW osVersionInfo;\nNTSTATUS status = STATUS_SUCCESS;\nosVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);\nstatus = RtlGetVersion((POSVERSIONINFOW)&amp;osVersionInfo);\n\nKdPrint((&quot;This is my first sample driver\\n&quot;));\nKdPrint((&quot;OS version is : %d.%d.%d\\n&quot;, osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, osVersionInfo.dwBuildNumber));\n\nreturn STATUS_SUCCESS;\n}</code></pre></div>\n<p>Here, I use the <code class=\"language-text\">KdPrint</code> macro to output the current OS version information.</p>\n<p>I use <code class=\"language-text\">RtlGetVersion</code> to obtain the OS version information.</p>\n<p>Reference: <a href=\"https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlgetversion\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">RtlGetVersion function (wdm.h) - Windows drivers | Microsoft Docs</a></p>\n<p>The information obtained is returned as an <code class=\"language-text\">OSVERSIONINFOEXW</code> structure.</p>\n<p>Reference: <a href=\"https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-osversioninfoa\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">OSVERSIONINFOA (winnt.h) - Win32 apps | Microsoft Docs</a></p>\n<div class=\"gatsby-highlight\" data-language=\"c++\"><pre class=\"language-c++\"><code class=\"language-c++\">typedef struct _OSVERSIONINFOA {\n  DWORD dwOSVersionInfoSize;\n  DWORD dwMajorVersion;\n  DWORD dwMinorVersion;\n  DWORD dwBuildNumber;\n  DWORD dwPlatformId;\n  CHAR  szCSDVersion[128];\n} OSVERSIONINFOA, *POSVERSIONINFOA, *LPOSVERSIONINFOA;</code></pre></div>\n<h3 id=\"configure-debugview\" style=\"position:relative;\"><a href=\"#configure-debugview\" aria-label=\"configure debugview permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Configure DebugView</h3>\n<p>Use Sysinternals DebugView to capture the output of the KdPrint macro.</p>\n<p>First, start the application and enable [Capture Kernel] and [Enable Verbose Output] from [Capture].</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 453px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/f15f75aa75f9687bbf2686d7e50e4671/2108e/image-66.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 110.00000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAWCAYAAADAQbwGAAAACXBIWXMAAAsTAAALEwEAmpwYAAAD2ElEQVQ4y22VSW8bRxCF+TtzSW52HCXRVYl98A+wlZNgJIoMGIkudmxAt0DQvu8rF4mc4T5DcjgiRZHapZf6OiaDBGmgwJ6eqlevXlUPE0s7e6pWqqqFoeLmmRpRU/l8XoE9h2blclmNRkONekM3NzdiPTw86PHx8X8tUaqECqqhCsWypnd8/blb1GmxppKXUxxFqpZLymZOVDfwUr6gq14PRD3e3ztgrL/YJ1LJTc3MTGlmfkaTqa72g1iZalWpUkm5Wl3x7a0al5dq3T+oaQyj62t5xjiIz5ROpRTWalpbW9PBwYHa7bYShYKnZrOu0uTvqo79rODNG+VevZL/+rXyo6P2O6rQzs4mJnT29q1is2hsTNHysgKT58YSnJ+fO+t2u0q0L7q6a7VV/PIrzf70m/x3k/LHfzEbV/7XCcXvP6j18aPanz6p9eEPxVNTKrx4ocbLlyobo0qxqJqxBJCVaMSxrhp1+d8Na3w91lExVDKTViqbU8bzVLDyi0Eov1JR1GqpY0HhxoaiH5/Lq9eVTiblmR8NdBqGQaDOZU+pkR+08sU3yg19L//JE/lPv3ZWGvrWbMj2TxUMDysaGdHZs2dqWQWnluRwb09HR0cqGlPXZQB7BniQTCnl2Zjki/IzGXnptPKnpzpvNtX5bG3rettYVQsFNawpuVxOSWOYzWb/YRiZE/XvW6Zet6OoGcmzOaxYqVVLdn5xoY6JjV1gva7Kxoy5zOaySlvik5MTN68OsGqBtHtra0slO/R935WQy3kiWWCgDHb4edCx2HTnXcYqOTw8dKDg/F2yOQCYspnaMLFPrUzaTxlLS0vurGAl7u/vu+fNzU3t7Oxod3fX3Sj8/9UUxCTb9va2FhcXHXUWzuvr6y4YlsfHx5qfn3eV4Ms5sewZapI6hhXT48J0gjbOBPOCjDgRxBkJ8KERSIDuPbuGnU7HGXsHSGmUSAkLCwtOPxZaLttt4FpREqVOT09rdXV1oBtJkYqy2Q8Ygk4QYNDnBaVTDsH9BlAmPuwBohF91gMNYXJpl5/yYNEHJBESwJw9GtKUvob4kZRzwEv2MXEMYXZ1deU06gOSCe0Ag1HdhpmglZUVB8YZRiIqgPFgbACEIVoCyJDyAsaA40wVBJIEcMrrN4aZxNg7QALQkGDGpt8UEszNzTlGAKEfCSmRd8TxlUFDSJFo0OVr+6YBRIk48gIQgjmnKQjf145zgBin/mCzd02hRBiSfXZ21jmzcNqz+81wIz5ADDZ+AJOcRnCDSDpoCjPIurVPPc3hl3V3d+f+lLB7+//gHOOZX97/1wD8C7ehKnzFNI1QAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/f15f75aa75f9687bbf2686d7e50e4671/8ac56/image-66.webp 240w,\n/static/f15f75aa75f9687bbf2686d7e50e4671/2430e/image-66.webp 453w\"\n              sizes=\"(max-width: 453px) 100vw, 453px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/f15f75aa75f9687bbf2686d7e50e4671/8ff5a/image-66.png 240w,\n/static/f15f75aa75f9687bbf2686d7e50e4671/2108e/image-66.png 453w\"\n            sizes=\"(max-width: 453px) 100vw, 453px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/f15f75aa75f9687bbf2686d7e50e4671/2108e/image-66.png\"\n            alt=\"image-66.png\"\n            title=\"image-66.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Now, if you start the service while capture is running after pressing the capture button on the toolbar, you can see the OS version information in DebugView.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 960px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/8a20c71a43fccf4a87343dbe533bf956/2bef9/image-67-1024x642.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 62.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAARlAAAEZQAGA43XUAAABy0lEQVQ4y52S20pCURCG1/aUhwRTEcsrL3yNyCwqoh6gCKLzgYiIniOIbrqJ7sIwJeoiqF4iEIWCgih129EySc2/NdMuysgOC35mZi/WN/+stcWlqkLNpJDJpJHNZpHL3SKXz8uYQ6FQ+LPE2ekp0qlzJBIJpC4uoKqvYMrzEvz4mOf4W4nDeBzq9TXiySSSR0c4PjnBze0dHuTmf5bIbG/j7uAAV7u7SO/s4H5vD/ubMaysrmF9PYzIZhTR2BYi0RgrvBGpKYFKBSiVgHIZz09P3GV0bBxCCNjr66HX6Ti3mM0w19VxXlMEqGgqEliuweERCEVBIBCAw+GAxWKB1+uFyWSCIr/rZBOFpGh6rxUNKF2SShpwfHKauzU1NjKMRDC9Xv9Lh1XAufkFmOR4Ho+HndntdpjlyDabDUajEQaDgWO1vnU4MzvHmy6Xi0cmEDkkp+SSRtZpI1KkBqRvgRNT03yHDRJGnekQuXW73XyQagJT/uUaPgLfHmVoZJQ3/X4/nE4nO/P5fDw65VarlWWTfwHVbw0ofgYWiwzs6x9gII374yPUepSy/BdpLS4to7mlFd09vejs6kaovQMtwRCCoTYEWz+oupZ6AewCe2nx8DgEAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/8a20c71a43fccf4a87343dbe533bf956/8ac56/image-67-1024x642.webp 240w,\n/static/8a20c71a43fccf4a87343dbe533bf956/d3be9/image-67-1024x642.webp 480w,\n/static/8a20c71a43fccf4a87343dbe533bf956/e46b2/image-67-1024x642.webp 960w,\n/static/8a20c71a43fccf4a87343dbe533bf956/a9a89/image-67-1024x642.webp 1024w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/8a20c71a43fccf4a87343dbe533bf956/8ff5a/image-67-1024x642.png 240w,\n/static/8a20c71a43fccf4a87343dbe533bf956/e85cb/image-67-1024x642.png 480w,\n/static/8a20c71a43fccf4a87343dbe533bf956/d9199/image-67-1024x642.png 960w,\n/static/8a20c71a43fccf4a87343dbe533bf956/2bef9/image-67-1024x642.png 1024w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/8a20c71a43fccf4a87343dbe533bf956/d9199/image-67-1024x642.png\"\n            alt=\"image-67-1024x642.png\"\n            title=\"image-67-1024x642.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Finally, I will try kernel-debugging the custom driver with WinDbg (the main topic at last).</p>\n<h2 id=\"analyze-the-custom-driver-with-kernel-debugging\" style=\"position:relative;\"><a href=\"#analyze-the-custom-driver-with-kernel-debugging\" aria-label=\"analyze the custom driver with kernel debugging permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Analyze the custom driver with kernel debugging</h2>\n<p>First, enable kernel debugging.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">bcdedit <span class=\"token operator\">/</span>debug on\nbcdedit <span class=\"token operator\">/</span>dbgsettings serial debugport:1 baudrate:115200\nshutdown <span class=\"token operator\">/</span>r <span class=\"token operator\">/</span>t 0</code></pre></div>\n<p>I summarized the detailed setup method in the following article.</p>\n<p>Reference: <a href=\"/windows-windbg-004-kernel-debug\">A First Step Toward Kernel Debugging a Windows 10 Environment with WinDbg</a></p>\n<p>Once the kernel debugging connection succeeds, add the pdb file for the driver you built this time to Sympath.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\"><span class=\"token punctuation\">.</span>sympath+ C:\\Users\\Tadpole01\\source\\repos\\Try2WinDbg\\drivers\\FirstDriverSample\\x64\\Debug</code></pre></div>\n<p>If you look at the module list, you can confirm that <code class=\"language-text\">FirstDriverSample</code> exists.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">kd> lm\n<span class=\"token function\">start</span>             <span class=\"token keyword\">end</span>                 module name\nfffff800`64520000 fffff800`64527000   FirstDriverSample   <span class=\"token punctuation\">(</span>deferred<span class=\"token punctuation\">)</span>             \nfffff801`74000000 fffff801`747cc000   nt         <span class=\"token punctuation\">(</span>pdb symbols<span class=\"token punctuation\">)</span>          C:\\ProgramData\\Dbg\\sym\\ntkrnlmp<span class=\"token punctuation\">.</span>pdb\\F7971FB6AA7E450CBCA7054A98D659421\\ntkrnlmp<span class=\"token punctuation\">.</span>pdb\nUnloaded modules:\nfffff800`62c80000 fffff800`62c8f000   dump_storport<span class=\"token punctuation\">.</span>sys\nfffff800`62cc0000 fffff800`62ce5000   dump_storahci<span class=\"token punctuation\">.</span>sys\nfffff800`62d10000 fffff800`62d2c000   dump_dumpfve<span class=\"token punctuation\">.</span>sys\nfffff800`63400000 fffff800`63413000   dam<span class=\"token punctuation\">.</span>sys \nfffff800`61ec0000 fffff800`61ed0000   WdBoot<span class=\"token punctuation\">.</span>sys\nfffff800`62ba0000 fffff800`62bae000   hwpolicy<span class=\"token punctuation\">.</span>sys</code></pre></div>\n<p>Because the symbols are loaded, you can also confirm the function names.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">kd> x <span class=\"token operator\">/</span>D <span class=\"token operator\">/</span>f FirstDriverSample!d*\nfffff800`64521020 FirstDriverSample!DriverEntry <span class=\"token punctuation\">(</span>struct _DRIVER_OBJECT <span class=\"token operator\">*</span><span class=\"token punctuation\">,</span> struct _UNICODE_STRING <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span>\nfffff800`645210f7 FirstDriverSample!DbgPrint <span class=\"token punctuation\">(</span>DbgPrint<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Use the <code class=\"language-text\">uf</code> command to look at the disassembly result of the entry function.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\">kd> uf FirstDriverSample!DriverEntry\nFirstDriverSample!DriverEntry <span class=\"token namespace\">[C:\\Users\\Tadpole01\\source\\repos\\Try2WinDbg\\drivers\\FirstDriverSample\\FirstDriver.cpp @ 13]</span>:\n   13 fffff800`64521020 4889542410      mov     qword ptr <span class=\"token namespace\">[rsp+10h]</span><span class=\"token punctuation\">,</span>rdx\n   13 fffff800`64521025 48894c2408      mov     qword ptr <span class=\"token namespace\">[rsp+8]</span><span class=\"token punctuation\">,</span>rcx\n   13 fffff800`6452102a 4881ec68010000  sub     rsp<span class=\"token punctuation\">,</span>168h\n   13 fffff800`64521031 488b05c81f0000  mov     rax<span class=\"token punctuation\">,</span>qword ptr <span class=\"token namespace\">[FirstDriverSample!__security_cookie (fffff800`64523000)]</span>\n   13 fffff800`64521038 4833c4          xor     rax<span class=\"token punctuation\">,</span>rsp\n   13 fffff800`6452103b 4889842450010000 mov     qword ptr <span class=\"token namespace\">[rsp+150h]</span><span class=\"token punctuation\">,</span>rax\n   16 fffff800`64521043 488b842470010000 mov     rax<span class=\"token punctuation\">,</span>qword ptr <span class=\"token namespace\">[rsp+170h]</span>\n   16 fffff800`6452104b 488d0daeffffff  lea     rcx<span class=\"token punctuation\">,</span><span class=\"token namespace\">[FirstDriverSample!FirstDriverUnload (fffff800`64521000)]</span>\n   16 fffff800`64521052 48894868        mov     qword ptr <span class=\"token namespace\">[rax+68h]</span><span class=\"token punctuation\">,</span>rcx\n   19 fffff800`64521056 c744242000000000 mov     dword ptr <span class=\"token namespace\">[rsp+20h]</span><span class=\"token punctuation\">,</span>0\n   20 fffff800`6452105e c74424301c010000 mov     dword ptr <span class=\"token namespace\">[rsp+30h]</span><span class=\"token punctuation\">,</span>11Ch\n   21 fffff800`64521066 488d4c2430      lea     rcx<span class=\"token punctuation\">,</span><span class=\"token namespace\">[rsp+30h]</span>\n   21 fffff800`6452106b ff158f0f0000    call    qword ptr <span class=\"token namespace\">[FirstDriverSample!_imp_RtlGetVersion (fffff800`64522000)]</span>\n   21 fffff800`64521071 89442420        mov     dword ptr <span class=\"token namespace\">[rsp+20h]</span><span class=\"token punctuation\">,</span>eax\n   23 fffff800`64521075 488d0d74010000  lea     rcx<span class=\"token punctuation\">,</span><span class=\"token punctuation\">[</span>FirstDriverSample! ?? ::FNODOBFM::`string<span class=\"token string\">' (fffff800`645211f0)]\n   23 fffff800`6452107c e876000000      call    FirstDriverSample!DbgPrint (fffff800`645210f7)\n   24 fffff800`64521081 448b4c243c      mov     r9d,dword ptr [rsp+3Ch]\n   24 fffff800`64521086 448b442438      mov     r8d,dword ptr [rsp+38h]\n   24 fffff800`6452108b 8b542434        mov     edx,dword ptr [rsp+34h]\n   24 fffff800`6452108f 488d0d7a010000  lea     rcx,[FirstDriverSample! ?? ::FNODOBFM::`string'</span> <span class=\"token punctuation\">(</span>fffff800`64521210<span class=\"token punctuation\">)</span><span class=\"token punctuation\">]</span>\n   24 fffff800`64521096 e85c000000      call    FirstDriverSample!DbgPrint <span class=\"token punctuation\">(</span>fffff800`645210f7<span class=\"token punctuation\">)</span>\n   26 fffff800`6452109b 33c0            xor     eax<span class=\"token punctuation\">,</span>eax\n   27 fffff800`6452109d 488b8c2450010000 mov     rcx<span class=\"token punctuation\">,</span>qword ptr <span class=\"token namespace\">[rsp+150h]</span>\n   27 fffff800`645210a5 4833cc          xor     rcx<span class=\"token punctuation\">,</span>rsp\n   27 fffff800`645210a8 e823000000      call    FirstDriverSample!__security_check_cookie <span class=\"token punctuation\">(</span>fffff800`645210d0<span class=\"token punctuation\">)</span>\n   27 fffff800`645210ad 4881c468010000  add     rsp<span class=\"token punctuation\">,</span>168h\n   27 fffff800`645210b4 c3              ret</code></pre></div>\n<p>This time, it is not a kernel driver with much behavior, so I will stop here for now.</p>\n<p>From next time onward, I plan to perform live debugging against a custom driver.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>I started developing a kernel driver in order to verify kernel-mode debugging.</p>\n<p>WinDbg-related articles are collected here.</p>\n<p>Reference: <a href=\"/windows-windbg-001-index\">Debugging and Troubleshooting Techniques with WinDbg</a></p>","fields":{"slug":"/windows-windriver-001-tutorial-en","tagSlugs":["/tag/win-dbg-en/","/tag/kernel-en/","/tag/kernel-driver-en/","/tag/reversing-en/","/tag/english/"]},"frontmatter":{"date":"2021-12-22","description":"","tags":["WinDbg (en)","Kernel (en)","KernelDriver (en)","Reversing (en)","English"],"title":"Building a Custom Windows Kernel Driver and Analyzing It with WinDbg","socialImage":{"publicURL":"/static/1d61c36eea0cac0d4e6ab1a46fc920c4/windows-windriver-001-tutorial.png"}}}},"pageContext":{"slug":"/windows-windriver-001-tutorial-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}