{"componentChunkName":"component---src-templates-post-template-js","path":"/windows-createfile-en","result":{"data":{"markdownRemark":{"id":"70a7bef7-aa3b-5e3c-8c3f-38032c5adbc1","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/windows-createfile\">original page</a>.</p>\n</blockquote>\n<p>I wanted a tool that could obtain file handles with arbitrary settings for testing, so I created a simple one.</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><a href=\"#source-code\">Source Code</a></li>\n<li><a href=\"#operation-check\">Operation Check</a></li>\n<li><a href=\"#summary\">Summary</a></li>\n</ul>\n<h2 id=\"source-code\" style=\"position:relative;\"><a href=\"#source-code\" aria-label=\"source 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>Source Code</h2>\n<div class=\"gatsby-highlight\" data-language=\"c++\"><pre class=\"language-c++\"><code class=\"language-c++\">#include &lt;windows.h&gt;\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n\n# define FILE_SHARE_NONE 0x00000000\n\nint isEmpty(std::wstring text) {\n\n    if (text.empty()) {\n        std::wcerr &lt;&lt; L&quot;Input is empty. Using default path.&quot; &lt;&lt; std::endl;\n        return 1;\n    }\n\n    return 0;\n}\n\nstd::wstring handleInput(std::wstring text) {\n    while (!text.empty() &amp;&amp; (text.back() == L&#39;\\n&#39; || text.back() == L&#39;\\r&#39; || text.back() == L&#39; &#39;))\n        text.pop_back();\n\n    return text;\n}\n\nHANDLE createFileHandle(const std::wstring&amp; path, std::wstring&amp; desiredAccess, std::wstring&amp; shareMode) {\n    \n    DWORD dwDesiredAccess = 0;\n    DWORD dwShareMode = 0;\n\n    if (desiredAccess == L&quot;1&quot;) {\n        dwDesiredAccess = GENERIC_READ;\n    }\n    else if (desiredAccess == L&quot;2&quot;) {\n        dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;\n    }\n    else {\n        std::wcerr &lt;&lt; L&quot;Invalid desired access input.&quot; &lt;&lt; std::endl;\n        return NULL;\n}\n\n    if (shareMode == L&quot;1&quot;) {\n        dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;\n    }\n    else if (shareMode == L&quot;2&quot;) {\n        dwShareMode = FILE_SHARE_READ;\n    }\n    else if (shareMode == L&quot;3&quot;) {\n        dwShareMode = FILE_SHARE_NONE;\n    }\n    else {\n        std::wcerr &lt;&lt; L&quot;Invalid share mode input.&quot; &lt;&lt; std::endl;\n        return NULL;\n    }\n\n    HANDLE hFile = CreateFileW(\npath.c_str(), // lpFileName\ndwDesiredAccess, // dwDesiredAccess\ndwShareMode, // dwShareMode\nNULL, // lpSecurityAttributes\n        OPEN_ALWAYS, // dwCreationDisposition\nFILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes\nNULL // hTemplateFile\n    );\n\n    if (hFile == INVALID_HANDLE_VALUE) {\n        std::wcerr &lt;&lt; L&quot;Failed to open file. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;\n        return NULL;\n    }\n    std::wcout &lt;&lt; L&quot;File opened successfully.&quot; &lt;&lt; std::endl;\n\n    return hFile;\n\n}\n\nint wmain()\n{\n// Input target file path\n    std::wcout &lt;&lt; L&quot;Input target file path(Default C:\\\\Windows\\\\Temp\\\\testfile.txt): &quot;;\n    std::wstring path;\n    std::getline(std::wcin, path);\n\npath = handleInput(path);\n    if (isEmpty(path)) {\npath = L&quot;C:\\\\Windows\\\\Temp\\\\testfile.txt&quot;;\n    }\n\n// Input desired access\n    std::wcout &lt;&lt; L&quot;\\nWhat is desired access?\\n1:\\&quot;GENERIC_READ\\&quot;\\n2:\\&quot;GENERIC_READ | GENERIC_WRITE\\&quot; : &quot;;\n    std::wstring desiredAccess;\n    std::getline(std::wcin, desiredAccess);\n\ndesiredAccess = handleInput(desiredAccess);\n    if (isEmpty(desiredAccess))\nreturn 1;\n \n// Input share mode\n    std::wcout &lt;&lt; L&quot;\\nWhat is share mode?\\n1:\\&quot;FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE\\&quot;\\n2:\\&quot;FILE_SHARE_READ\\&quot;\\n3:\\&quot;FILE_SHARE_NONE\\&quot; : &quot;;\n    std::wstring shareMode;\n    std::getline(std::wcin, shareMode);\n\nshareMode = handleInput(shareMode);\nif (isEmpty(shareMode))\nreturn 1;\n    \nHANDLE hFile = createFileHandle(path, desiredAccess, shareMode);\n\n    std::wcout &lt;&lt; L&quot;\\nPlease Enter key to close.&quot;;\n    std::wstring tmp;\n    std::getline(std::wcin, tmp);\n\n    CloseHandle(hFile);\n\n    return 0;\n}</code></pre></div>\n<h2 id=\"operation-check\" style=\"position:relative;\"><a href=\"#operation-check\" aria-label=\"operation check 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>Operation Check</h2>\n<p>When you run the tool built from the code above, you can execute CreateFileW by specifying the dwDesiredAccess and dwShareMode options as shown below.</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/6e5eb5fe499037f3576bae6b9e45944a/12bff/image-20251119181145247.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: 79.58333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsUlEQVQ4y3WTyY7CQAxE+8i+7zuENYBQuIDE/3+XZ55FoQ6TOZSMg9su2+WwTzM7Xy52Pp8t/cXpdLL1eu1YrVbW6XSs2+26nc1mlmWZ3W43jyc2TdMcQrLZ2Hw+t+126zZJEg/EJ+FisbDRaGSTycTG47ENBgNHo9GwarX6B2G32zmbw+HgFha9Xs/q9bo/qlQqVi6XHfwWeFyr1T5WCDC5Xq/OavNmC6vpdGrtdjvH5DtZEZwhCZgJiZfLpTOEKRgOh+7TLkVoHZ/EYphr+Xg8+pCfz6e9Xi/D7/f7zo5FNJtNh3yx/pchLcKE6lgYsIRSqeTQ7OTH3zTbGAH6kgYgKdtVAVrlO4Xka+P6DRQf4s0BaXC/37t0kBEzRmMs7vLWrMA3xiSZhbh/hspSqCiW+CyO0QCWFIudmbZaLX/vLcd6QnskEgsqa9MsQrosgvKEb4HCSi1I6ALbFxuKg+8OQ+wQQBLmxc0+Hg9PTGssgJaxJGYsUgh+jqGuQDOkZbR5v9/9epAR9ws7PRQ72U9CggRmJElIjyTSGRZdhvBpmWCB4ZOYy4iL4McPi6D/fgBOm5HBlET05wAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/6e5eb5fe499037f3576bae6b9e45944a/8ac56/image-20251119181145247.webp 240w,\n/static/6e5eb5fe499037f3576bae6b9e45944a/d3be9/image-20251119181145247.webp 480w,\n/static/6e5eb5fe499037f3576bae6b9e45944a/e46b2/image-20251119181145247.webp 960w,\n/static/6e5eb5fe499037f3576bae6b9e45944a/6ed01/image-20251119181145247.webp 1032w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/6e5eb5fe499037f3576bae6b9e45944a/8ff5a/image-20251119181145247.png 240w,\n/static/6e5eb5fe499037f3576bae6b9e45944a/e85cb/image-20251119181145247.png 480w,\n/static/6e5eb5fe499037f3576bae6b9e45944a/d9199/image-20251119181145247.png 960w,\n/static/6e5eb5fe499037f3576bae6b9e45944a/12bff/image-20251119181145247.png 1032w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/6e5eb5fe499037f3576bae6b9e45944a/d9199/image-20251119181145247.png\"\n            alt=\"image-20251119181145247\"\n            title=\"image-20251119181145247\"\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 makes it easy to verify behaviors such as CreateFile failing with SHARING VIOLATION when you execute CreateFile with <code class=\"language-text\">FILE_SHARE_NONE</code> against a file whose handle has already been obtained with <code class=\"language-text\">FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE</code>.</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/193ec0c16db1c4732569faf8a8eb922a/161ec/image-20251119181320435.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: 12.083333333333332%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAYAAABYBvyLAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAaUlEQVQI122MywkDIQBEtwfxFn9rW57szAJEUBAEwYOFveAScgg5PJgPM5e7b6y1GGPQWn+0RSn1eOfctz9475FSEmNkrcUYgzkne29yzlxKvZ7xL+fsX35OhRCEEKi1UkqhtUbvnZQSb/TsSPzec+U4AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/193ec0c16db1c4732569faf8a8eb922a/8ac56/image-20251119181320435.webp 240w,\n/static/193ec0c16db1c4732569faf8a8eb922a/d3be9/image-20251119181320435.webp 480w,\n/static/193ec0c16db1c4732569faf8a8eb922a/e46b2/image-20251119181320435.webp 960w,\n/static/193ec0c16db1c4732569faf8a8eb922a/f992d/image-20251119181320435.webp 1440w,\n/static/193ec0c16db1c4732569faf8a8eb922a/78de1/image-20251119181320435.webp 1840w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/193ec0c16db1c4732569faf8a8eb922a/8ff5a/image-20251119181320435.png 240w,\n/static/193ec0c16db1c4732569faf8a8eb922a/e85cb/image-20251119181320435.png 480w,\n/static/193ec0c16db1c4732569faf8a8eb922a/d9199/image-20251119181320435.png 960w,\n/static/193ec0c16db1c4732569faf8a8eb922a/07a9c/image-20251119181320435.png 1440w,\n/static/193ec0c16db1c4732569faf8a8eb922a/161ec/image-20251119181320435.png 1840w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/193ec0c16db1c4732569faf8a8eb922a/d9199/image-20251119181320435.png\"\n            alt=\"image-20251119181320435\"\n            title=\"image-20251119181320435\"\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><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 625px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/2bbb6325af999ab16c56e12b49da7c16/80d71/image-20251119181540699.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: 24.166666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAABGElEQVQY023QS0/CQBSG4f7/hRpduDGRuDUmJgRRg9yLMQa5tYDcShFruQRaWtpqJK/ToujCxZNvzpw5izPSxWWCu0wxUpAfyeZKlKtNmu0BaqtHQ+1SUzrUlW3WGp1d/R8pn9hDvj6gmBREFq72ecoec397hHxzSPvhhGE1xqByiiZSr51FGda/YmjfJMdI8THPE8xy+NMcnplh1o+zNtPoyjnWKAlWkc2yIHopFlqcYJqGVQlsORL2Phd5NoLUUHtiPQ2lOaCvGZhTm9azzmD4Rrujo7/MMEyLt4lNpzemXGmJlbuoYqauhF/SZzSeY0wsXs0lkuuscBw74q1dgsDH89zo7Hlr3kX9I7x33dXWnznf93ZvvgDbQ2KQop/RdwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/2bbb6325af999ab16c56e12b49da7c16/8ac56/image-20251119181540699.webp 240w,\n/static/2bbb6325af999ab16c56e12b49da7c16/d3be9/image-20251119181540699.webp 480w,\n/static/2bbb6325af999ab16c56e12b49da7c16/487e2/image-20251119181540699.webp 625w\"\n              sizes=\"(max-width: 625px) 100vw, 625px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/2bbb6325af999ab16c56e12b49da7c16/8ff5a/image-20251119181540699.png 240w,\n/static/2bbb6325af999ab16c56e12b49da7c16/e85cb/image-20251119181540699.png 480w,\n/static/2bbb6325af999ab16c56e12b49da7c16/80d71/image-20251119181540699.png 625w\"\n            sizes=\"(max-width: 625px) 100vw, 625px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/2bbb6325af999ab16c56e12b49da7c16/80d71/image-20251119181540699.png\"\n            alt=\"image-20251119181540699\"\n            title=\"image-20251119181540699\"\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<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>GitHub Copilot’s development assistance is so intelligent that writing code has become incredibly easy.</p>\n<p>I also wrote this article in 10 minutes.</p>","fields":{"slug":"/windows-createfile-en","tagSlugs":["/tag/windows/","/tag/english/"]},"frontmatter":{"date":"2025-11-19","description":"Notes on creating a tool to obtain file handles with arbitrary flags on Windows","tags":["Windows","English"],"title":"Notes on Creating a Tool to Obtain File Handles with Arbitrary Flags on Windows","socialImage":{"publicURL":"/static/faa4521ccee6cf02e5c0e5b7606edc67/windows-createfile.png"}}}},"pageContext":{"slug":"/windows-createfile-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}