{"id":950,"date":"2012-11-19T20:12:31","date_gmt":"2012-11-19T12:12:31","guid":{"rendered":"http:\/\/blog.dynox.cn\/?p=950"},"modified":"2017-10-18T23:51:33","modified_gmt":"2017-10-18T15:51:33","slug":"ioctl-methods","status":"publish","type":"post","link":"https:\/\/blog.dynox.cn\/?p=950","title":{"rendered":"Windows IOCTL Methods"},"content":{"rendered":"<div class=\"gruber-markdown\"><p><span style=\"font-size: small;\">\u5728\u5b66\u4e60Windows\u5185\u6838\u5f00\u53d1\u4e4b\u521d\uff0c\u6bcf\u4e2a\u65b0\u624b\u90fd\u4f1a\u5728DeviceIoControl\u4e0a\u7ea0\u7ed3\u4e00\u9635\uff0c\u7136\u540e\u5c31\u9009\u62e9\u4e00\u4e2a\u6700\u5f97\u5fc3\u5e94\u624b\u7684\u3002\u62ff\u5728\u624b\u4e2d\uff0c\u53bb\u8d70\u904d\u5168\u5929\u4e0b\u3002\u7b49<span style=\"font-size: small;\">\u8d70\u4e86\u4e00\u5708\u4e4b\u540e\u5c31\u4f1a\u611f\u6168\u5929\u4e0b\u7adf\u5982\u6b64\u7b80\u5355\uff01<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u6216\u8bb8\uff0c\u5929\u4e0b\u5c31\u662f\u8fd9\u822c\u7b80\u5355\uff0c\u4f46DeviceIoControl\u5374\u7a0d\u6709\u4e9b\u590d\u6742\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">DeviceIoControl\u7684Buffer\u4f20\u8f93\u65b9\u5f0f\u6709\u4e09\u79cd\uff1aMETHOD_BUFFERED\uff0cMETHOD_IN_DIRECT &amp; METHOD_OUT_DIRECT\uff0cMETHOD_NEITHER\u3002METHOD_IN_DIRECT\u548cMETHOD_OUT_DIRECT\u7b97\u4f5c\u662f\u540c\u4e00\u7c7b\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">DeviceIoControl\u662f\u7528\u6237\u5c42\u7a0b\u5e8f\u548c\u5185\u6838\u9a71\u52a8\u7684\u4e00\u79cd\u901a\u4fe1\u65b9\u5f0f\u3002\u8fd9\u4e2a\u901a\u4fe1\u53ef\u5b8c\u5168\u770b\u6210\u662f\u4e24\u4eba\u4e4b\u95f4\u7684\u79c1\u5bc6\uff0c\u4e8c\u8005\u60f3\u600e\u4e48\u6837\u5c31\u600e\u4e48\u6837\uff0c\u53ea\u8981\u53cc\u65b9\u90fd\u77e5\u6653\u5e76\u5e94\u7528\u540c\u4e00\u4e2a\u89c4\u5219\u5373\u53ef\u3002\u4f46\u5982\u679c\u8fd8\u6709\u4e2a\u7b2c\u4e09\u8005\u7684\u8bdd\uff0c\u6c9f\u901a\u8d77\u6765\u5c31\u6ca1\u6709\u8fd9\u4e48\u65b9\u4fbf\u4e86\uff0c\u6240\u4ee5<\/span><span style=\"font-size: small;\">Windows\u5185\u6838\u8981\u5b9a\u4e49\u4e00\u4e2a\u89c4\u5219\u3002\u53ef\u4ee5\u8bf4\uff0c\u6839\u672c\u4e0a\u662f\u56e0\u4e3aWindows\u5185\u6838\u8fd9\u4e2a\u7b2c\u4e09\u8005\u7684\u5b58\u5728\uff0c\u4e5f\u6b63\u662f\u6839\u636eWindows\u5185\u6838<\/span><span style=\"font-size: small;\">\u5728\u5176\u4e2d\u7684\u53c2\u4e0e\u5ea6\u7684\u4e0d\u540c\u624d\u884d\u751f\u51fa\u4e86\u8fd9\u4e09\u79cd\u4e0d\u540c\u7684ioctl\u65b9\u6cd5\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"><strong>\u7528\u6237\u5c42API\u5b9a\u4e49\uff1a <\/strong><\/span><\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">BOOL WINAPI DeviceIoControl(\n__in\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 HANDLE hDevice,\n__in\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DWORD dwIoControlCode,\n__in_opt\u00a0\u00a0\u00a0\u00a0 LPVOID lpInBuffer,\n__in\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DWORD nInBufferSize,\n__out_opt\u00a0\u00a0\u00a0 LPVOID lpOutBuffer,\n__in\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DWORD nOutBufferSize,\n__out_opt\u00a0\u00a0\u00a0 LPDWORD lpBytesReturned,\n__inout_opt\u00a0 LPOVERLAPPED lpOverlapped\n);<\/code><\/pre>\n<p><span style=\"font-size: small;\"><span style=\"font-family: Tahoma;\"><strong>\u5185\u6838\u5c42Irp Stack\u4e2d\u76f8\u5173DeviceIoControl\u7684\u53c2\u6570<\/strong>\uff08\u53d6\u81eaIRP\u53caIO_STACK_LOCATION\u7ed3\u6784\uff09\uff1a<\/span><\/span><\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">typedef struct DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT) _IRP {\nCSHORT Type;\nUSHORT Size;\nPMDL MdlAddress;\n\nunion {\nstruct _IRP *MasterIrp;\nLONG IrpCount;\nPVOID SystemBuffer;\n} AssociatedIrp;\n\n\u2026\u2026\nPVOID UserBuffer;\n\u2026\u2026\n} IRP, *PIRP;\n\ntypedef struct _IO_STACK_LOCATION {\nUCHAR MajorFunction;\nUCHAR MinorFunction;\n\u2026\u2026\nunion {\nstruct {\n\u2026\u2026\n} Create;\n\u2026\u2026\nstruct {\nULONG OutputBufferLength;\nULONG POINTER_ALIGNMENT InputBufferLength;\nULONG POINTER_ALIGNMENT IoControlCode;\nPVOID Type3InputBuffer;\n} DeviceIoControl;\n\u2026\u2026\n} Parameters;\n\u2026\u2026\n} IO_STACK_LOCATION, *PIO_STACK_LOCATION;<\/code><\/pre>\n<p><span style=\"font-size: small;\">\u7528\u6237\u5c42DeviceIoControl()\u53c2\u6570\u53caIrp\u6210\u5458\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u9488\u5bf9\u4e0d\u540c\u7684ioctl method\u4e0d\u540c\u7684\u5bf9\u5e94\uff0c\u8be6\u89c1\u4e0b\u9762\u7684\u8868\u683c\uff1a<\/span><\/p>\n<p><a href=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2012\/11\/IOCTL-MTHODs.jpg\" class=\"highslide-image\" onclick=\"return hs.expand(this);\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"IOCTL-MTHODs\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2012\/11\/IOCTL-MTHODs_thumb.jpg\" alt=\"IOCTL-MTHODs\" width=\"530\" height=\"150\" border=\"0\" \/><\/a><\/p>\n<p><span style=\"font-size: small;\"><strong>METHOD_BUFFERED:<\/strong><\/span><\/p>\n<p><span style=\"font-size: small;\">\u8fd9\u79cd\u65b9\u5f0f\u662f\u6700\u7b80\u5355\uff0c\u4e5f\u662f\u6700\u5e38\u7528\u7684\u3002\u5185\u6838\u9a71\u52a8\u4e0d\u7528\u4efb\u4f55\u5904\u7406\u5373\u53ef\u64cd\u4f5c\u4ece\u5e94\u7528\u5c42\u53d1\u8fc7\u6765\u7684\u6570\u636e\u3002\u5b9e\u9645\u4e0a\u7528\u6237\u4f7f\u7528\u4e0a\u7684\u7b80\u5355\uff0c\u6b63\u662f\u56e0\u4e3a\u5185\u6838\u505a\u4e86\u76f8\u5173\u7684\u5904\u7406\u800c\u7701\u53bb\u4e86\u7528\u6237\u81ea\u5df1\u5904\u7406\u7684\u7e41\u7410\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5728DeviceIoContro()\u9636\u6bb5\uff1aNtDeviceIoControlFile()\u5728\u6784\u9020Irp\u65f6\uff0c\u4f1a\u4e8enInBufferSize\u53canOutBufferSize\u4e2d\u9009\u62e9\u5927\u8005\u4ee5\u5206\u914dirp-&gt;AssociatedIrp.SystemBuffer\uff0c\u5e76\u5c06lpInBuffer\u4e2d\u7684\u6570\u636e\u590d\u5236\u81f3\u65b0\u5206\u914d\u7684SystemBuffer\u4e2d\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679c\u6709SystemBuffer\u7684\u5206\u914d\uff0cIrp-&gt;Flags\u5219\u4f1a\u88ab\u8bbe\u7f6e\u6807\u5fd7\u4f4d\uff1a\u00a0\u00a0\u00a0 IRP_BUFFERED_IO | IRP_DEALLOCATE_BUFFER\u3002<\/span><span style=\"font-size: small;\">\u5982\u679clpOutBuffer\u4e0d\u4e3a\u7a7a\u7684\u8bdd\uff0cIrp-&gt;Flags\u4e5f\u4f1a\u88ab\u7f6e\u4e0a\u6807\u5fd7\uff1a IRP_INPUT_OPERATION\u3002\u8fd9\u4e09\u4e2a\u6807\u5fd7\u5728Irp\u5b8c\u6210\u65f6\u4f1a\u7528\u5230\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5728\u6b64\u8981\u6ce8\u610f\u4e00\u70b9\uff0c\u9488\u5bf9METHOD_BUFFERED\uff0c\u5373\u4f7flpInputBuffer\u548clpOutputBuffer\u4e3a\u540c\u4e00\u4e2aBuffer\uff0c\u8c03\u7528DeviceIoControl()\u65f6\u4e5f\u5fc5\u987b\u5168\u90e8\u6307\u5b9a\uff0c\u5426\u5219\u4e0d\u80fd\u6b63\u786e\u5c06\u8bf7\u6c42\u6570\u636e\u53d1\u9001\u81f3\u5185\u6838\u5c42\u6216\u8005\u7528\u6237\u5c42\u63a5\u6536\u4e0d\u5230\u8fd4\u56de\u7684\u6570\u636e\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5728IRP_MJ_DEVICE_CONTROL\u5b8c\u6210\u9636\u6bb5\uff1aIopCompleteRequest()\u4f1a\u6839\u636eIrp-&gt;Flags\u6807\u5fd7\u6765\u5b8c\u6210\u540e\u7eed\u7684\u5904\u7406\uff0c\u9488\u5bf9IRP_BUFFERED_IO\uff0c\u5373METHOD_BUFFERED\uff1a<\/span><\/p>\n<ul>\n    <li><span style=\"font-size: small;\">\u5982\u679c\u8bbe\u7f6e\u4e86IRP_INPUT_OPERATION\u6807\u5fd7\uff0c\u4f1a\u5c06\u8fd4\u56de\u6570\u636e\u4eceSystemBuffer\u4e2d\u8f6c\u79fb\u81f3\u7528\u6237\u4f20\u9012\u8fc7\u6765\u7684lpOutBuffer<\/span><\/li>\n    <li><span style=\"font-size: small;\">\u5982\u679c\u8bbe\u7f6e\u4e86IRP_DEALLOCATE_BUFFER\u6807\u5fd7\uff0c\u5219\u91ca\u653eSystemBuffer\u5185\u5b58\u81f3PagedPool<\/span><\/li>\n<\/ul>\n<p><span style=\"font-size: small;\"><strong>METHOD_IN_DIRECT &amp; METHOD_OUT_DIRECT:<\/strong><\/span><\/p>\n<p><span style=\"font-size: small;\">\u5728DeviceIoContro()\u9636\u6bb5\uff1a\u5982\u679clpInBuffer\u5b58\u5728\u7684\u8bdd\uff08\u5e76\u4e14\u6ee1\u8db3nInBufferSize &gt; 0\uff09\uff0cI\/O Manager\u4f1a\u4ee5METHOD_BUFFERED\u7684\u65b9\u5f0f\u6765\u5904\u7406lpInBuffer\uff0c\u5373\u4f1a\u5206\u914dSystemBuffer\u5e76\u5c06\u8f93\u5165\u6570\u636e\u4ecelpInBuffer\u8f6c\u79fb\u81f3SystemBuffer\u3002\u5982\u679clpOutBuffer\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efaIrp-&gt;MdlAddress\u5e76\u5c06lpOutBuffer\u6240\u7528\u9875\u9762\u5168\u90e8\u9501\u5b9a\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5728IRP_MJ_DEVICE_CONTROL\u5b8c\u6210\u9636\u6bb5\uff1aIopCompleteRequest()\u4f1a\u89e3\u9501irp-&gt;MdlAddress\u4e2d\u6240\u5360\u7528\u7684\u9875\u9762\uff0c\u5e76\u91ca\u653eirp-&gt;MdlAddress\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86lpInBuffer\u7684\u8bdd\uff0c\u7cfb\u7edf\u8fd8\u662f\u4f1a\u53e6\u5916\u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff0c\u5e76\u9020\u6210\u5185\u5b58\u7684\u6d6a\u8d39\u3002\u597d\u5728\u7528\u6237\u5b8c\u5168\u53ef\u4ee5\u4e0d\u7528lpInBuffer\uff0c\u800c\u662f\u5c06lpOutBuffer\u540c\u65f6\u5b58\u653e\u8f93\u5165\u6570\u636eBuffer\u3002\u53e6\u5916METHOD_IN_DIRECT\u53caMETHOD_OUT_DIRECT\u7684\u4f7f\u7528\u4e5f\u5b8c\u5168\u7531\u7528\u6237\u51b3\u5b9a\uff0c\u7cfb\u7edf\u53ea\u662f\u4e2a\u4f20\u9012\u8005\uff0c\u5e76\u4e0d\u53c2\u4e0elpInBuffer\u53calpOutBuffer\u4e2d\u6570\u636e\u7684\u5904\u7406\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\"><strong>METHOD_NEITHER:<\/strong><\/span><\/p>\n<p><span style=\"font-size: small;\">\u8fd9\u79cd\u65b9\u5f0f\u4e0b\uff0c\u7cfb\u7edf\u53c2\u4e0e\u5ea6\u6700\u5c0f\uff0c\u4e00\u822c\u7528\u4e8e\u80fd\u591f\u5feb\u901f\u5904\u7406\u7684\u64cd\u4f5c\u3002<\/span><span style=\"font-size: small;\"><span style=\"font-size: small;\">I\/O Manager\u6240\u505a\u7684\u5de5\u4f5c\u53ea\u662f\u5c06\u8f93\u5165\u53c2\u6570\u4f20\u9012\u81f3\u5185\u6838\uff0c\u5176\u5b83\u7684\u4ec0\u4e48\u4e8b\u4e5f\u4e0d\u505a\u3002\u5982\u679c\u5185\u6838\u9a71\u52a8\u8981\u5c06Irp\u6302\u8d77\u6765\u7684\u8bdd\uff0c\u5b83\u8981\u5c06irpSp-&gt;Parameters.DeviceIoControl.Type3InputBuffer\u53caIrp-&gt;UserBuffer\u6240\u7528\u9875\u9762\u9501\u5b9a\u8d77\u6765\uff0c\u5e76\u5728\u5b8c\u6210\u65f6\u91ca\u653e\u6240\u5360\u8d44\u6e90\uff08\u5982\u679c\u5c06lpOutBuffer\/Irp-&gt;UserBuffer\u9501\u5b9a\u81f3irp-&gt;MdlAddress\u7684\u8bdd\uff0cIopCompleteRequest()\u5728\u8c03\u7528\u65f6\u4f1a\u505a\u91ca\u653e\u5de5\u4f5c\uff09\u3002\n<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">\u603b\u7ed3\u8d77\u6765\uff0cMETHOD_BUFFERED\u6700\u7701\u4e8b\uff0c\u4f46\u4f1a\u591a\u6d88\u8017\u7cfb\u7edf\u5185\u5b58\uff1bMETHOD_NEITHER\u6700\u76f4\u63a5\uff0c\u867d\u7136\u770b\u8d77\u6765\u5e76\u4e0d\u201c\u76f4\/DIRECT\u201d\u00a0 :-) METHOD_IN_DIRECT\u6216METHOD_OUT_DIRECT\u6700\u9002\u5408\u5927\u5185\u5b58\u533a\u7684\u64cd\u4f5c\uff0c\u5373\u4f7f\u9700\u8981\u5c06\u8bb2\u6c42\u6302\u8d77\u6765\uff0c\u5185\u6838\u9a71\u52a8\u4e5f\u4e0d\u7528\u505a\u989d\u5916\u7684\u5de5\u4f5c\u3002\u4f46\u5728\u5185\u5b58\u865a\u62df\u5730\u5740\u7a7a\u95f4\u7d27\u7f3a\u65f6\uff0c\u7279\u522b\u662f\u572832\u4f4d\u7cfb\u7edf\u4e0a\uff0c\u5bf9\u4e8e\u7279\u5927\u7684\u5185\u5b58\u533a\u64cd\u4f5c\uff0c\u5b9c\u91c7\u7528METHOD_NEITHER\u65b9\u5f0f\u4ee5\u8282\u7701\u5730\u5740\u8d44\u6e90\u3002<\/span><\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b66\u4e60Windows\u5185\u6838\u5f00\u53d1\u4e4b\u521d\uff0c\u6bcf\u4e2a\u65b0\u624b\u90fd\u4f1a\u5728DeviceIoControl\u4e0a\u7ea0\u7ed3\u4e00\u9635\uff0c\u7136\u540e\u5c31\u9009\u62e9\u4e00\u4e2a\u6700\u5f97\u5fc3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[9],"tags":[495,447,394],"views":4057,"_links":{"self":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/950"}],"collection":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=950"}],"version-history":[{"count":5,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/950\/revisions"}],"predecessor-version":[{"id":1620,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/950\/revisions\/1620"}],"wp:attachment":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}