{"componentChunkName":"component---src-templates-post-template-js","path":"/rust-winapi-en","result":{"data":{"markdownRemark":{"id":"258bd9e0-4f65-5d3a-bcd1-544cf4ef921c","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/rust-winapi\">original page</a>.</p>\n</blockquote>\n<p>Rust has been a hot topic for a while, and although I had been meaning to try it someday, I kept putting it off. Recently, I learned that the official Rust crate for Windows looks quite promising, so I decided to try creating and debugging programs that call various Windows APIs from Rust.</p>\n<p>Reference: <a href=\"https://github.com/microsoft/windows-rs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">microsoft/windows-rs: Rust for Windows</a></p>\n<p>The following is a crate that can call C-style Windows APIs.</p>\n<p>Reference: <a href=\"https://crates.io/crates/windows-sys\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">windows-sys - crates.io: Rust Package Registry</a></p>\n<p>This one also seems to be a crate that can call APIs such as COM.</p>\n<p>Reference: <a href=\"https://crates.io/crates/windows\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">windows - crates.io: Rust Package Registry</a></p>\n<p>I would like to try various implementations using crates like these.</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=\"#notes-on-rust\">Notes on Rust</a></p>\n<ul>\n<li><a href=\"#installing-rust-and-setting-up-the-development-environment\">Installing Rust and Setting Up the Development Environment</a></li>\n<li><a href=\"#creating-a-rust-project\">Creating a Rust Project</a></li>\n<li><a href=\"#changing-build-options\">Changing Build Options</a></li>\n<li><a href=\"#specifying-the-build-target\">Specifying the Build Target</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#using-the-messageboxw-api-from-rust\">Using the MessageBoxW API from Rust</a></p>\n<ul>\n<li><a href=\"#adding-the-windows-crate-to-use-the-messageboxw-api\">Adding the windows Crate to Use the MessageBoxW API</a></li>\n<li><a href=\"#creating-messageboxwrs-and-loading-the-module\">Creating MessageBoxW.rs and Loading the Module</a></li>\n<li><a href=\"#defining-the-function\">Defining the Function</a></li>\n<li><a href=\"#calling-the-messageboxw-api\">Calling the MessageBoxW API</a></li>\n<li><a href=\"#easily-converting-string-literals-to-utf-16-wide-strings\">Easily Converting String Literals to UTF-16 Wide Strings</a></li>\n<li><a href=\"#running-the-program\">Running the Program</a></li>\n</ul>\n</li>\n<li><a href=\"#summary\">Summary</a></li>\n</ul>\n<h2 id=\"notes-on-rust\" style=\"position:relative;\"><a href=\"#notes-on-rust\" aria-label=\"notes on rust 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>Notes on Rust</h2>\n<p>Before implementing the program, I will first briefly summarize what I looked up about what kind of language Rust is in the first place.</p>\n<p>Rust is generally said to have the following advantages.</p>\n<ul>\n<li>Fast execution speed</li>\n<li>Guaranteed safety</li>\n<li>Compatibility ensured through editions</li>\n<li>Rich features and tools</li>\n</ul>\n<p>In particular, because Rust does not use garbage collection and is compiled directly into machine code, it is said to deliver performance second only to C/C++.</p>\n<p>Also, Rust’s focus on zero-cost abstractions, as in C++, is apparently one reason it can achieve high performance.</p>\n<p>Reference: <a href=\"https://doc.rust-lang.org/book/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">The Rust Programming Language - The Rust Programming Language</a></p>\n<h3 id=\"installing-rust-and-setting-up-the-development-environment\" style=\"position:relative;\"><a href=\"#installing-rust-and-setting-up-the-development-environment\" aria-label=\"installing rust and setting up the development environment 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>Installing Rust and Setting Up the Development Environment</h3>\n<p>This time I want to implement a program in Rust that calls Windows APIs, so I will set up the development environment on Windows rather than Linux.</p>\n<p>Basically, I followed the steps in the official documentation below.</p>\n<p>Reference: <a href=\"https://learn.microsoft.com/ja-jp/windows/dev-environment/rust/setup\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Set up a development environment for Rust on Windows | Microsoft Learn</a></p>\n<p>To set up the Rust environment, I first installed Visual Studio so that I could install the build tools.</p>\n<p>At that time, I installed [.Net desktop development] and [Desktop development with C++] together.</p>\n<p>Next, I installed Rust on Windows using the installer downloaded from the following page.</p>\n<p>Reference: <a href=\"https://www.rust-lang.org/tools/install\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Install Rust - Rust Programming Language</a></p>\n<p>This completes the minimum required Rust setup, but because I wanted to use VSCode as the IDE, I also installed the rust-analyzer and CodeLLDB extensions.</p>\n<p>Reference: <a href=\"https://learn.microsoft.com/ja-jp/windows/dev-environment/rust/setup#install-visual-studio-code\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Install Visual Studio Code | Microsoft Learn</a></p>\n<h3 id=\"creating-a-rust-project\" style=\"position:relative;\"><a href=\"#creating-a-rust-project\" aria-label=\"creating a rust project 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>Creating a Rust Project</h3>\n<p>Once the basic setup is complete, you can create a HelloWorld project by running the following command.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">cargo new HelloWorld</code></pre></div>\n<p>The created project includes <code class=\"language-text\">main.rs</code>, <code class=\"language-text\">Cargo.toml</code>, and <code class=\"language-text\">.gitignore</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: 705px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/56739f6534437c3354fbaa550a2aa51a/d2cbc/image-20250403200002716.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: 34.166666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABUklEQVQoz42Qy47UMBQFs4Qd0nQSk5dfsZ3E6U5PaDTADKz4AMQGwYb//4rCMcOaWRxdKy7rVk7Rti3ee0IIVFVFWZbUdZ0jpSLEBXv2KG8IU2Do+8ScMpv5qszz35tiHEfWGOnaDqstznm01rjRMQwDcV253G8sceZgjXFYF5KEwyR+8ef0Zsxs13UUMh18DKjFsNxWRmezhbY6AS3TNBGXmEzveXh4x+PHyPXi8vdjweRj/kOTJJRSFEI09M7QWEXVNlRCUNUC0be0aeNhq1Kk0mzXyNPjxr5Hzn7m/Toxj4Zt8myzZ3YTxevbd8SXH+ivvxGff/Jq/5bz5ukXddgxSqKNxY9d6nTIvR4mKk2Tpn6OkjLdSYpSz3TxyvjpA3K/cVITd4NHpG5aaf7Cye7o6CUpuqbBhhF9MdiLpevfIk53tL2gTx0eWw9QPhv8L38AqYPENLvBJHAAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/56739f6534437c3354fbaa550a2aa51a/8ac56/image-20250403200002716.webp 240w,\n/static/56739f6534437c3354fbaa550a2aa51a/d3be9/image-20250403200002716.webp 480w,\n/static/56739f6534437c3354fbaa550a2aa51a/af945/image-20250403200002716.webp 705w\"\n              sizes=\"(max-width: 705px) 100vw, 705px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/56739f6534437c3354fbaa550a2aa51a/8ff5a/image-20250403200002716.png 240w,\n/static/56739f6534437c3354fbaa550a2aa51a/e85cb/image-20250403200002716.png 480w,\n/static/56739f6534437c3354fbaa550a2aa51a/d2cbc/image-20250403200002716.png 705w\"\n            sizes=\"(max-width: 705px) 100vw, 705px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/56739f6534437c3354fbaa550a2aa51a/d2cbc/image-20250403200002716.png\"\n            alt=\"image-20250403200002716\"\n            title=\"image-20250403200002716\"\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>The <code class=\"language-text\">Cargo.toml</code> file is a manifest file written in TOML format.</p>\n<p>The project created by <code class=\"language-text\">cargo new</code> contains the following default manifest.</p>\n<div class=\"gatsby-highlight\" data-language=\"toml\"><pre class=\"language-toml\"><code class=\"language-toml\"><span class=\"token punctuation\">[</span><span class=\"token table class-name\">package</span><span class=\"token punctuation\">]</span>\n<span class=\"token key property\">name</span> <span class=\"token punctuation\">=</span> <span class=\"token string\">\"HelloWorld\"</span>\n<span class=\"token key property\">version</span> <span class=\"token punctuation\">=</span> <span class=\"token string\">\"0.1.0\"</span>\n<span class=\"token key property\">edition</span> <span class=\"token punctuation\">=</span> <span class=\"token string\">\"2024\"</span>\n\n<span class=\"token punctuation\">[</span><span class=\"token table class-name\">dependencies</span><span class=\"token punctuation\">]</span></code></pre></div>\n<p>Reference: <a href=\"https://doc.rust-lang.org/cargo/reference/manifest.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">The Manifest Format - The Cargo Book</a></p>\n<p>The <code class=\"language-text\">[package]</code> section is required in Cargo, and it contains the package name and version.</p>\n<p>Also, <code class=\"language-text\">edition</code> contains the value that specifies which edition the package will be built with.</p>\n<p>In Rust, this <code class=\"language-text\">edition</code> mechanism is (apparently) how backward compatibility is maintained.</p>\n<p>Reference: <a href=\"https://gihyo.jp/article/2022/10/tfc014-rust\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Let’s Understand Rust 2021 on the Road to Rust 2024 ~ From a TechFeed Conference 2022 Talk | gihyo.jp</a></p>\n<p>You can build the project you created by running the following command in the same directory as <code class=\"language-text\">Cargo.toml</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">cargo build</code></pre></div>\n<p>By default, an EXE file was generated directly under <code class=\"language-text\">target\\debug\\</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: 882px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9ca0e35096330049b6c36824399ef5b5/90712/image-20250403203022461.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: 19.583333333333332%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA4ElEQVQY0yWQW5KCQAxF2cTAAKK2zfTYjQRo5CGWs/9dnYn4cSqV172pJNbWOOV4qKjf8XikrrXmHNZazmeDDCPmYjHGUFUFeZ4qOVmWkqZfyidmWUZiLgYjHhcFLzeWeSbGEekEEaFpbrRt96lJx/3uFK/9qMY/anhRI7tTlgeSb3UqxivFn1CPLXddnOZlF4gxMk0T67LQDz3b88njsTDPPa/XzLYNKi6sa6TvA6dTSTItD+IQWbeNIQ6EEPSqhvAmeLwP+Kvfc+d+9Q2lXlrSSUUcDzpbcGtLnSv0ZTn/F1F2I5W6ftoAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/9ca0e35096330049b6c36824399ef5b5/8ac56/image-20250403203022461.webp 240w,\n/static/9ca0e35096330049b6c36824399ef5b5/d3be9/image-20250403203022461.webp 480w,\n/static/9ca0e35096330049b6c36824399ef5b5/9d646/image-20250403203022461.webp 882w\"\n              sizes=\"(max-width: 882px) 100vw, 882px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/9ca0e35096330049b6c36824399ef5b5/8ff5a/image-20250403203022461.png 240w,\n/static/9ca0e35096330049b6c36824399ef5b5/e85cb/image-20250403203022461.png 480w,\n/static/9ca0e35096330049b6c36824399ef5b5/90712/image-20250403203022461.png 882w\"\n            sizes=\"(max-width: 882px) 100vw, 882px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/9ca0e35096330049b6c36824399ef5b5/90712/image-20250403203022461.png\"\n            alt=\"image-20250403203022461\"\n            title=\"image-20250403203022461\"\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>Even without any special configuration, a PDB file is generated in the same folder, so debugging seems straightforward.</p>\n<h3 id=\"changing-build-options\" style=\"position:relative;\"><a href=\"#changing-build-options\" aria-label=\"changing build options 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>Changing Build Options</h3>\n<p>By the way, in <code class=\"language-text\">cargo</code>, if you do not specify any options, the <code class=\"language-text\">dev</code> profile seems to be selected by default, which appears to be why the build output is created under the <code class=\"language-text\">debug</code> folder.</p>\n<p>For example, if you use the following option, the files will be built in the <code class=\"language-text\">release</code> folder.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">cargo build --release</code></pre></div>\n<p>Reference: <a href=\"https://doc.rust-jp.rs/book-ja/ch14-01-release-profiles.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Customizing Builds with Release Profiles - The Rust Programming Language (Japanese Edition)</a></p>\n<p>At this point, the build options for each profile can be written in the <code class=\"language-text\">Cargo.toml</code> file.</p>\n<div class=\"gatsby-highlight\" data-language=\"toml\"><pre class=\"language-toml\"><code class=\"language-toml\"><span class=\"token punctuation\">[</span><span class=\"token table class-name\">profile.dev</span><span class=\"token punctuation\">]</span>\n<span class=\"token key property\">opt-level</span> <span class=\"token punctuation\">=</span> <span class=\"token number\">0</span>\n\n<span class=\"token punctuation\">[</span><span class=\"token table class-name\">profile.release</span><span class=\"token punctuation\">]</span>\n<span class=\"token key property\">opt-level</span> <span class=\"token punctuation\">=</span> <span class=\"token number\">3</span>\n<span class=\"token key property\">debug</span> <span class=\"token punctuation\">=</span> <span class=\"token boolean\">false</span></code></pre></div>\n<p>The following article was easy to understand for the available options.</p>\n<p>Reference: <a href=\"https://qiita.com/kyamamoto9120/items/28bcb2d724ec836f5033\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">About the Compilation Options for cargo build #Rust - Qiita</a></p>\n<h3 id=\"specifying-the-build-target\" style=\"position:relative;\"><a href=\"#specifying-the-build-target\" aria-label=\"specifying the build target 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>Specifying the Build Target</h3>\n<p>With the default settings immediately after creating a project, running <code class=\"language-text\">cargo build</code> builds <code class=\"language-text\">src/main.rs</code>.</p>\n<p>However, if, as in this case, you want to quickly test a program implemented as a single file, it seems you can create a <code class=\"language-text\">bin</code> folder directly under <code class=\"language-text\">src</code> and build the files placed there.</p>\n<p>For example, if you create <code class=\"language-text\">new_program.rs</code> as shown below, you can build <code class=\"language-text\">new_program.exe</code> by running the <code class=\"language-text\">cargo build --bin new_program</code> command.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 339px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/84cc84615ee105def5b7bc3cf11895ec/16caa/image-20250403212030927.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: 92.08333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsTAAALEwEAmpwYAAACj0lEQVQ4y4VUWXbbMAz0GdpIFBdxkajdkmM5SZM0bpaeob3/TaYg5bp28tx8zKMp0MMBMcDKOYe6afDr92/snx6RJAkYY2fIsgxpmkJJRWf2WE8jXp5fMK7XmOcdvC/x+vaGm92MFeccUkg0RGqdBZcCIpfggi+Q/IzYWgtjDJx1yHNNyOkiCVvQXiussoxBU6DwBYqbFt3+Gv3rFsPPHa3XKOcG6VUSyQJpULogWVaWxrgqDaRVi8Kokm6RimBUVBWURgh+VHcJIS7UktVCKAS9g4clldVtvxyi209TfU96umdJitzbRWH4ENSFVSkFXVkKGmQ8+1QVyw6XpSxmJpRYCAUpDKSFsbCjh9918VCaLO9zhiT9oDpko5yG1PKfwvDIZdujn64hnYIdSqhCw3QFdONg2iIifPuQ8kGhjFWOBVGR0JYV2mGE6YuostjUqO/XaL5PqB9G+JsebqwXkgOOhDaPpCtO7yCCdchbu/tHClDqlLapDb2nAbu6Qvrl6xFhn5FVTsGoGRRlpQylzHQJrgsIW6He3EG2DdzdBnYeCRMyUyBVFix3C+h3eoK4lwbCVxClxyp1PdlGwvkamtzfDxOGntLzDbUhpcPl56D/p5mIWDHbkEJHhaAbDKXqO1T9RKlXSLg+U/M/sANWGbELlcO1ayIlY++3aF9m+IcN2h8zVbik9hJRxUV1jEO3FfK6DEUJPuSx4RWV3ZBFNBk7QDmqXC6iz7JgkwsI8diuf1sveGq7JWVDi4KGQbffxBb0twOq++HcxO8QRxuZPXYYDYhjpxTkwdxomhoaug4qbTRx6NHPBkSIcbUMk+O0mb89UTE6+LsO/fM2GjoobJ8mKPJmMO8lwqiQRASFfwBNYuQZIqLtvAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/84cc84615ee105def5b7bc3cf11895ec/8ac56/image-20250403212030927.webp 240w,\n/static/84cc84615ee105def5b7bc3cf11895ec/d6e88/image-20250403212030927.webp 339w\"\n              sizes=\"(max-width: 339px) 100vw, 339px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/84cc84615ee105def5b7bc3cf11895ec/8ff5a/image-20250403212030927.png 240w,\n/static/84cc84615ee105def5b7bc3cf11895ec/16caa/image-20250403212030927.png 339w\"\n            sizes=\"(max-width: 339px) 100vw, 339px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/84cc84615ee105def5b7bc3cf11895ec/16caa/image-20250403212030927.png\"\n            alt=\"image-20250403212030927\"\n            title=\"image-20250403212030927\"\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=\"using-the-messageboxw-api-from-rust\" style=\"position:relative;\"><a href=\"#using-the-messageboxw-api-from-rust\" aria-label=\"using the messageboxw api from rust 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>Using the MessageBoxW API from Rust</h2>\n<p>This time, I will try creating a program that calls the <code class=\"language-text\">MessageBoxW</code> API from Rust using the windows crate.</p>\n<p>Reference: <a href=\"https://learn.microsoft.com/ja-jp/windows/dev-environment/rust/rust-for-windows\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Rust for Windows and the windows crate | Microsoft Learn</a></p>\n<p>By using the <a href=\"https://crates.io/crates/windows\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">windows crate</a> published on crates.io, you can easily use Windows APIs such as <code class=\"language-text\">MessageBoxW</code> from a Rust program.</p>\n<p>A crate is something like a library in other programming languages, and even with external crates published on crates.io, such as the windows crate, it seems that if you add their definitions to <code class=\"language-text\">[dependencies]</code> in the <code class=\"language-text\">Cargo.toml</code> file, the <code class=\"language-text\">cargo</code> command will automatically resolve the dependencies when building.</p>\n<h3 id=\"adding-the-windows-crate-to-use-the-messageboxw-api\" style=\"position:relative;\"><a href=\"#adding-the-windows-crate-to-use-the-messageboxw-api\" aria-label=\"adding the windows crate to use the messageboxw api 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>Adding the windows Crate to Use the MessageBoxW API</h3>\n<p>First, I added the following line to the <code class=\"language-text\">Cargo.toml</code> file to use the <code class=\"language-text\">MessageBoxW</code> API.</p>\n<p>In the following entry, I specify version 0.60.0 of the <code class=\"language-text\">windows</code> crate published on crates.io as a dependency, and I specify <code class=\"language-text\">Win32_UI_WindowsAndMessaging</code> with the <code class=\"language-text\">features</code> option.</p>\n<div class=\"gatsby-highlight\" data-language=\"toml\"><pre class=\"language-toml\"><code class=\"language-toml\"><span class=\"token punctuation\">[</span><span class=\"token table class-name\">dependencies</span><span class=\"token punctuation\">]</span>\n<span class=\"token key property\">windows</span> <span class=\"token punctuation\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token key property\">version</span> <span class=\"token punctuation\">=</span> <span class=\"token string\">\"0.60.0\"</span><span class=\"token punctuation\">,</span> <span class=\"token key property\">features</span> <span class=\"token punctuation\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">\"Win32_UI_WindowsAndMessaging\"</span><span class=\"token punctuation\">]</span> <span class=\"token punctuation\">}</span></code></pre></div>\n<p>It seems that the <code class=\"language-text\">features</code> option can be used to specify that particular functionality should be enabled at compile time.</p>\n<p>In this case, it enables the <code class=\"language-text\">Win32_UI_WindowsAndMessaging</code> feature, which includes <code class=\"language-text\">MessageBoxW</code>.</p>\n<p>Reference: <a href=\"https://qiita.com/osanshouo/items/43271813b5d62e89d598\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">How to Use Feature Flags in Rust #Rust - Qiita</a></p>\n<p>Reference: <a href=\"https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/UI/WindowsAndMessaging/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">windows::Win32::UI::WindowsAndMessaging - Rust</a></p>\n<p>Reference: <a href=\"https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/UI/WindowsAndMessaging/fn.MessageBoxExW.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">MessageBoxExW in windows::Win32::UI::WindowsAndMessaging - Rust</a></p>\n<h3 id=\"creating-messageboxwrs-and-loading-the-module\" style=\"position:relative;\"><a href=\"#creating-messageboxwrs-and-loading-the-module\" aria-label=\"creating messageboxwrs and loading the module 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>Creating MessageBoxW.rs and Loading the Module</h3>\n<p>Next, I created a <code class=\"language-text\">MessageBoxW.rs</code> file directly under <code class=\"language-text\">src/bin/</code> and wrote the following code.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">use</span> <span class=\"token namespace\">windows<span class=\"token punctuation\">::</span></span><span class=\"token punctuation\">{</span>\n    <span class=\"token namespace\">core<span class=\"token punctuation\">::</span></span><span class=\"token constant\">PCWSTR</span><span class=\"token punctuation\">,</span>\n    <span class=\"token class-name\">Win32</span><span class=\"token punctuation\">::</span><span class=\"token constant\">UI</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">WindowsAndMessaging</span><span class=\"token punctuation\">::</span><span class=\"token punctuation\">{</span><span class=\"token class-name\">MessageBoxW</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">MB_OK</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">MB_ICONINFORMATION</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">to_wide_null</span><span class=\"token punctuation\">(</span>s<span class=\"token punctuation\">:</span> <span class=\"token operator\">&amp;</span><span class=\"token keyword\">str</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">-></span> <span class=\"token class-name\">Vec</span><span class=\"token operator\">&lt;</span><span class=\"token keyword\">u16</span><span class=\"token operator\">></span> <span class=\"token punctuation\">{</span>\n    s<span class=\"token punctuation\">.</span><span class=\"token function\">encode_utf16</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">chain</span><span class=\"token punctuation\">(</span><span class=\"token namespace\">std<span class=\"token punctuation\">::</span>iter<span class=\"token punctuation\">::</span></span><span class=\"token function\">once</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">collect</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> caption <span class=\"token operator\">=</span> <span class=\"token function\">to_wide_null</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Title\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">let</span> message <span class=\"token operator\">=</span> <span class=\"token function\">to_wide_null</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"This is my first WinAPI with Rust.\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">unsafe</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token class-name\">MessageBoxW</span><span class=\"token punctuation\">(</span>\n            <span class=\"token class-name\">None</span><span class=\"token punctuation\">,</span>\n            <span class=\"token constant\">PCWSTR</span><span class=\"token punctuation\">(</span>message<span class=\"token punctuation\">.</span><span class=\"token function\">as_ptr</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            <span class=\"token constant\">PCWSTR</span><span class=\"token punctuation\">(</span>caption<span class=\"token punctuation\">.</span><span class=\"token function\">as_ptr</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            <span class=\"token constant\">MB_OK</span> <span class=\"token operator\">|</span> <span class=\"token constant\">MB_ICONINFORMATION</span><span class=\"token punctuation\">,</span>\n        <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>First, at the top of the file, <code class=\"language-text\">use</code> declares that the required components will be imported.</p>\n<p>In this example, it instructs Rust to import <code class=\"language-text\">core::PCWSTR</code> and <code class=\"language-text\">Win32::UI::WindowsAndMessaging::{MessageBoxW, MB_OK, MB_ICONINFORMATION}</code> from the windows crate.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">use</span> <span class=\"token namespace\">windows<span class=\"token punctuation\">::</span></span><span class=\"token punctuation\">{</span>\n    <span class=\"token namespace\">core<span class=\"token punctuation\">::</span></span><span class=\"token constant\">PCWSTR</span><span class=\"token punctuation\">,</span>\n    <span class=\"token class-name\">Win32</span><span class=\"token punctuation\">::</span><span class=\"token constant\">UI</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">WindowsAndMessaging</span><span class=\"token punctuation\">::</span><span class=\"token punctuation\">{</span><span class=\"token class-name\">MessageBoxW</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">MB_OK</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">MB_ICONINFORMATION</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p><code class=\"language-text\">core::PCWSTR</code> is a pointer type that points to a null-terminated UTF-16 string in the Windows API.</p>\n<p>Also, <code class=\"language-text\">Win32::UI::WindowsAndMessaging::{MessageBoxW, MB_OK, MB_ICONINFORMATION}</code> imports three items from <code class=\"language-text\">Win32::UI::WindowsAndMessaging</code>: <code class=\"language-text\">MessageBoxW</code>, <code class=\"language-text\">MB_OK</code>, and <code class=\"language-text\">MB_ICONINFORMATION</code>.</p>\n<p>Because it is written compactly, it is actually a bit hard to read until you get used to it, but in practice I was able to run the program the same way even if I wrote it as follows.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">use</span> <span class=\"token namespace\">windows<span class=\"token punctuation\">::</span>core<span class=\"token punctuation\">::</span></span><span class=\"token constant\">PCWSTR</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">use</span> <span class=\"token namespace\">windows<span class=\"token punctuation\">::</span></span><span class=\"token class-name\">Win32</span><span class=\"token punctuation\">::</span><span class=\"token constant\">UI</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">WindowsAndMessaging</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">MessageBoxW</span><span class=\"token punctuation\">;</span> \n<span class=\"token keyword\">use</span> <span class=\"token namespace\">windows<span class=\"token punctuation\">::</span></span><span class=\"token class-name\">Win32</span><span class=\"token punctuation\">::</span><span class=\"token constant\">UI</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">WindowsAndMessaging</span><span class=\"token punctuation\">::</span><span class=\"token constant\">MB_OK</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">use</span> <span class=\"token namespace\">windows<span class=\"token punctuation\">::</span></span><span class=\"token class-name\">Win32</span><span class=\"token punctuation\">::</span><span class=\"token constant\">UI</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">WindowsAndMessaging</span><span class=\"token punctuation\">::</span><span class=\"token constant\">MB_ICONINFORMATION</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h3 id=\"defining-the-function\" style=\"position:relative;\"><a href=\"#defining-the-function\" aria-label=\"defining the function 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>Defining the Function</h3>\n<p>In the next section, I define a function called <code class=\"language-text\">to_wide_null</code>.</p>\n<p>In Rust, functions are defined in the form <code class=\"language-text\">fn &lt;function_name>()</code>. The notation after <code class=\"language-text\">-></code> specifies the function’s return type.</p>\n<p>In other words, in the following code, <code class=\"language-text\">Vec&lt;u16></code> shows that this function returns a vector type.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">to_wide_null</span><span class=\"token punctuation\">(</span>s<span class=\"token punctuation\">:</span> <span class=\"token operator\">&amp;</span><span class=\"token keyword\">str</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">-></span> <span class=\"token class-name\">Vec</span><span class=\"token operator\">&lt;</span><span class=\"token keyword\">u16</span><span class=\"token operator\">></span> <span class=\"token punctuation\">{</span>\n    s<span class=\"token punctuation\">.</span><span class=\"token function\">encode_utf16</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">chain</span><span class=\"token punctuation\">(</span><span class=\"token namespace\">std<span class=\"token punctuation\">::</span>iter<span class=\"token punctuation\">::</span></span><span class=\"token function\">once</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">collect</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>More specifically, <code class=\"language-text\">s.encode_utf16().chain(std::iter::once(0)).collect()</code> inside the function UTF-16 encodes the string slice <code class=\"language-text\">s</code> of type <code class=\"language-text\">&amp;str</code>, adds a null character (0) element at the end with <code class=\"language-text\">.chain(std::iter::once(0))</code>, and returns it as the <code class=\"language-text\">Vec&lt;u16></code> vector specified by <code class=\"language-text\">.collect()</code>. (Hard to follow!)</p>\n<h3 id=\"calling-the-messageboxw-api\" style=\"position:relative;\"><a href=\"#calling-the-messageboxw-api\" aria-label=\"calling the messageboxw api 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>Calling the MessageBoxW API</h3>\n<p>Finally, the following <code class=\"language-text\">main</code> function calls the <code class=\"language-text\">MessageBoxW</code> API.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> caption <span class=\"token operator\">=</span> <span class=\"token function\">to_wide_null</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Title\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">let</span> message <span class=\"token operator\">=</span> <span class=\"token function\">to_wide_null</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"This is my first WinAPI with Rust.\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">unsafe</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token class-name\">MessageBoxW</span><span class=\"token punctuation\">(</span>\n            <span class=\"token class-name\">None</span><span class=\"token punctuation\">,</span>\n            <span class=\"token constant\">PCWSTR</span><span class=\"token punctuation\">(</span>message<span class=\"token punctuation\">.</span><span class=\"token function\">as_ptr</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            <span class=\"token constant\">PCWSTR</span><span class=\"token punctuation\">(</span>caption<span class=\"token punctuation\">.</span><span class=\"token function\">as_ptr</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            <span class=\"token constant\">MB_OK</span> <span class=\"token operator\">|</span> <span class=\"token constant\">MB_ICONINFORMATION</span><span class=\"token punctuation\">,</span>\n        <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Here, I first use the <code class=\"language-text\">to_wide_null</code> function to define <code class=\"language-text\">caption</code> and <code class=\"language-text\">message</code> as null-terminated wide strings.</p>\n<p>In VSCode, the types are displayed directly in the code, which makes this much easier to read.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 950px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/e63fc2ef2e981720f306d7547ce9b01e/906b5/image-20250403230922378.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: 37.916666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABNUlEQVQoz32Sa26DMBCEuUYDfmLj8DIxggJpIqWp2gv0/meZrlEeldrkx+fBLJpdj0mULTFMA8bXFlVbw1gH5xyMMaQWZRkpUFpSmyNXGpzzfxGCI1k3mkzChN3hjMZ36PtAJhVC32BZAqY5YGga9JVDS82qwqz4raW9RU3PkSI3SAQZxilLP6AoHLRWUFJASAml1A0hxIqkmoo1Ur3qnVhP1g+lRtM22MdJ5hHa1WAvG2RpinSzQZpmyDIG9uCov0muXbMsxa7rKM8FbprhRg8/TvDDhLArEeotXK5vkz4ikauhXLt3ZLjsD6jnI7bHN/jTGeHzG+H9C+PpA77rIQWjSZ4YclqUoAy0haSsIlwZSC7BKQoWUTlh6L263Ka46R9DkzP6FWLo8n79nF30Crvw3CzyA9Ok4z6aWo4AAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/e63fc2ef2e981720f306d7547ce9b01e/8ac56/image-20250403230922378.webp 240w,\n/static/e63fc2ef2e981720f306d7547ce9b01e/d3be9/image-20250403230922378.webp 480w,\n/static/e63fc2ef2e981720f306d7547ce9b01e/4a41d/image-20250403230922378.webp 950w\"\n              sizes=\"(max-width: 950px) 100vw, 950px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/e63fc2ef2e981720f306d7547ce9b01e/8ff5a/image-20250403230922378.png 240w,\n/static/e63fc2ef2e981720f306d7547ce9b01e/e85cb/image-20250403230922378.png 480w,\n/static/e63fc2ef2e981720f306d7547ce9b01e/906b5/image-20250403230922378.png 950w\"\n            sizes=\"(max-width: 950px) 100vw, 950px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/e63fc2ef2e981720f306d7547ce9b01e/906b5/image-20250403230922378.png\"\n            alt=\"image-20250403230922378\"\n            title=\"image-20250403230922378\"\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, I call <code class=\"language-text\">MessageBoxW</code> inside an <code class=\"language-text\">unsafe</code> block.</p>\n<p><code class=\"language-text\">unsafe</code> seems to be syntax used when bypassing Rust’s compile-time-enforced memory safety and running code for which memory safety is not enforced at compile time.</p>\n<p>There seem to be several situations where <code class=\"language-text\">unsafe</code> is used, but in this case I need to use an <code class=\"language-text\">unsafe</code> block because I am calling <code class=\"language-text\">MessageBoxW</code>, an <code class=\"language-text\">unsafe</code> function exported via <code class=\"language-text\">extern</code>.</p>\n<p>Reference: <a href=\"https://doc.rust-lang.org/book/ch20-01-unsafe-rust.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Unsafe Rust - The Rust Programming Language</a></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/ef4240ec64e2f648f64db18b70511ef8/d30ee/image-20250403231524313.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: 29.583333333333332%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA1ElEQVQY042P227DIBBE+RxjYyfmsviCQYmI+lKpUtX//5XpLlKjPFRJHoYRaOcwq74/r/j5uqF87HDXFa5uiGkFBUIIocl7f9ffXXzfdxARlmVBjLFJ1UvCcRywK8HlCJsiQiR452GdhXPuDn78QEC11gaVfM4ZpRQoCZAMUoSfLYhB7Y2h27YhcNgYg3Ecmz/TNE1QMnhhMpUMw4BxnjEMwwNkgNb6LUlOyZG4ckwJPa+hGai7Dh0PdOzvwkRSQp1PJ0zSit303KzvX672n6SYbPQLueK8mxPayOoAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/ef4240ec64e2f648f64db18b70511ef8/8ac56/image-20250403231524313.webp 240w,\n/static/ef4240ec64e2f648f64db18b70511ef8/d3be9/image-20250403231524313.webp 480w,\n/static/ef4240ec64e2f648f64db18b70511ef8/e46b2/image-20250403231524313.webp 960w,\n/static/ef4240ec64e2f648f64db18b70511ef8/58b01/image-20250403231524313.webp 980w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/ef4240ec64e2f648f64db18b70511ef8/8ff5a/image-20250403231524313.png 240w,\n/static/ef4240ec64e2f648f64db18b70511ef8/e85cb/image-20250403231524313.png 480w,\n/static/ef4240ec64e2f648f64db18b70511ef8/d9199/image-20250403231524313.png 960w,\n/static/ef4240ec64e2f648f64db18b70511ef8/d30ee/image-20250403231524313.png 980w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/ef4240ec64e2f648f64db18b70511ef8/d9199/image-20250403231524313.png\"\n            alt=\"image-20250403231524313\"\n            title=\"image-20250403231524313\"\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>Reference: <a href=\"https://docs.rs/windows-win/latest/windows_win/sys/fn.MessageBoxW.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">MessageBoxW in windows_win::sys - Rust</a></p>\n<p>As an experiment, I removed the <code class=\"language-text\">unsafe</code> block and tried to compile it, and the compiler properly produced an error.</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/d0f7c692f3f2eb2db77702c40a35ed8e/23725/image-20250403231643309.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: 36.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAAA/UlEQVQoz41R2W7EIBDLT+QmkAAhF821aZXs//+XO0y1lfLS7YMR1gzGnonKIoe0PZTz0LWCMQbWWtR1jUr9oCwK5HmONE2RZdmfiFIhIZSCswbTNGEcBwghGHEcI0kSFnqJvRONxDhDyprJdV3YHzuapmGHWpubs385FL2HoljBzTzP+Pw6WPj5vHCeJ4zW7PIl+A5RocgNCTaixCgrDBR1qirMUsLSvaD5vXN1c5jQ4bqeZxiWoSlu27YcO/Cu6yi6phSK79xDPIzEGP3b4zpHaQyiiR4v3mPZVjyOA/u+Y1lWbNsG/+Ex0JKWNfCVahvXnXP8QUBZlLfFfQOW6b7i8dg0EQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/d0f7c692f3f2eb2db77702c40a35ed8e/8ac56/image-20250403231643309.webp 240w,\n/static/d0f7c692f3f2eb2db77702c40a35ed8e/d3be9/image-20250403231643309.webp 480w,\n/static/d0f7c692f3f2eb2db77702c40a35ed8e/e46b2/image-20250403231643309.webp 960w,\n/static/d0f7c692f3f2eb2db77702c40a35ed8e/e8613/image-20250403231643309.webp 1105w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/d0f7c692f3f2eb2db77702c40a35ed8e/8ff5a/image-20250403231643309.png 240w,\n/static/d0f7c692f3f2eb2db77702c40a35ed8e/e85cb/image-20250403231643309.png 480w,\n/static/d0f7c692f3f2eb2db77702c40a35ed8e/d9199/image-20250403231643309.png 960w,\n/static/d0f7c692f3f2eb2db77702c40a35ed8e/23725/image-20250403231643309.png 1105w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/d0f7c692f3f2eb2db77702c40a35ed8e/d9199/image-20250403231643309.png\"\n            alt=\"image-20250403231643309\"\n            title=\"image-20250403231643309\"\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<h3 id=\"easily-converting-string-literals-to-utf-16-wide-strings\" style=\"position:relative;\"><a href=\"#easily-converting-string-literals-to-utf-16-wide-strings\" aria-label=\"easily converting string literals to utf 16 wide strings 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>Easily Converting String Literals to UTF-16 Wide Strings</h3>\n<p>Up to this point in the code, I had been using the <code class=\"language-text\">to_wide_null</code> function to convert strings into the UTF-16 strings passed as arguments to <code class=\"language-text\">MessageBoxW</code>.</p>\n<p>However, someone more knowledgeable told me that using a macro called <code class=\"language-text\">w!</code> makes string conversion much easier, so I rewrote the 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/c87bbc71ff232a9c85ee516e30935cc3/798d4/image-20250404174733024.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: 26.25%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAk0lEQVQY05WQuw7DIAxF+R0iIArvV0Dq0qVDh/7/r9zGRK3ULrTDsQ2CI9vscb/hcs0IMcA5f+CmWGsRQkDvHTFG5JxRSkFKCcyHCH080FrDGDOFZJS990NIEqLWOqTMGP2+JKkQAlJKLMsyhXP+UdM/RmFdV2zbBqXUX8JvhpA6a62Nlve9jZoyjdPbeX6N+cs6nmownw+Ps47IAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/c87bbc71ff232a9c85ee516e30935cc3/8ac56/image-20250404174733024.webp 240w,\n/static/c87bbc71ff232a9c85ee516e30935cc3/d3be9/image-20250404174733024.webp 480w,\n/static/c87bbc71ff232a9c85ee516e30935cc3/e46b2/image-20250404174733024.webp 960w,\n/static/c87bbc71ff232a9c85ee516e30935cc3/a8e2b/image-20250404174733024.webp 976w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/c87bbc71ff232a9c85ee516e30935cc3/8ff5a/image-20250404174733024.png 240w,\n/static/c87bbc71ff232a9c85ee516e30935cc3/e85cb/image-20250404174733024.png 480w,\n/static/c87bbc71ff232a9c85ee516e30935cc3/d9199/image-20250404174733024.png 960w,\n/static/c87bbc71ff232a9c85ee516e30935cc3/798d4/image-20250404174733024.png 976w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/c87bbc71ff232a9c85ee516e30935cc3/d9199/image-20250404174733024.png\"\n            alt=\"image-20250404174733024\"\n            title=\"image-20250404174733024\"\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>Reference: <a href=\"https://docs.rs/windows-sys/latest/windows_sys/macro.w.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">w in windows_sys - Rust</a></p>\n<p>The rewritten code is shown below. It is much cleaner.</p>\n<p>The <code class=\"language-text\">windows::core::w!</code> macro converts the target string into a null-terminated UTF-16 wide string, so there is no longer any need to obtain a pointer to a vector containing the wide string via <code class=\"language-text\">windows::core::PCWSTR</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"rust\"><pre class=\"language-rust\"><code class=\"language-rust\"><span class=\"token keyword\">use</span> <span class=\"token namespace\">windows<span class=\"token punctuation\">::</span></span><span class=\"token punctuation\">{</span>\n    <span class=\"token namespace\">core<span class=\"token punctuation\">::</span></span>w<span class=\"token punctuation\">,</span>\n    <span class=\"token class-name\">Win32</span><span class=\"token punctuation\">::</span><span class=\"token constant\">UI</span><span class=\"token punctuation\">::</span><span class=\"token class-name\">WindowsAndMessaging</span><span class=\"token punctuation\">::</span><span class=\"token punctuation\">{</span><span class=\"token class-name\">MessageBoxW</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">MB_OK</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">MB_ICONINFORMATION</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n\n<span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">let</span> caption <span class=\"token operator\">=</span> <span class=\"token macro property\">w!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Title\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">let</span> message <span class=\"token operator\">=</span> <span class=\"token macro property\">w!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"This is my first WinAPI with Rust.\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">unsafe</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token class-name\">MessageBoxW</span><span class=\"token punctuation\">(</span>\n            <span class=\"token class-name\">None</span><span class=\"token punctuation\">,</span>\n            message<span class=\"token punctuation\">,</span>\n            caption<span class=\"token punctuation\">,</span>\n            <span class=\"token constant\">MB_OK</span> <span class=\"token operator\">|</span> <span class=\"token constant\">MB_ICONINFORMATION</span><span class=\"token punctuation\">,</span>\n        <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>When I built and ran this code, I was able to use <code class=\"language-text\">MessageBoxW</code> just like with the original 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: 807px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/7c2b2042116e0b7583ef403e30fd1076/d2a60/image-20250404175125968.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: 55.833333333333336%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACFklEQVQoz21SzY4SQRjkMVaZn/6dnhmGgYFd3GA8GB+EKw9BwjNx5ahGEt07K5GjYQ8aiV520U1Z0zNBY5yk8n3d06nuqq86JisRqQT6coh0cgnjLERiIdMEwvcG2hg49llmYSz3bAZhHP/lPFdAEJLrOIrQ0VwoLSGt+YPMnSEImyQkS1AUKazjniOBJ+tD5iXPldBce0Jr+ZI4QtANEQYRorBGiCgIm0qELYJ2Lw6D9h9r0G1Q93GMjpQKicswKHvo96y/xROwRnX/F6JzjRuQIK4hRIOa0OYDxMqiGFYYTafoX12jqir0y5JSC1RZiiGRaAWrJFKjfbVKwEpaFYuzgshLdrm/LdYOJhtAW/pkGvOlMp7I1AQtYWY1cpd49FIHR8u01jAcnOQFnbqpZUpD0/sFZTskHETQDXhr0Mpt5D3tdpFmGW5uPmC//4Tt7Ufsdjtst1scDgfM53MS8oVCCggRw3LCE0anfDaFKytOX3vD6yGIKMCTiwtMrka43X/F3Zd71N/jr0c83D/4frlcopP1K/TyGEaTlAMaj8cYXz9HOX2J4YtXMEXFaNAK1kBojEdDfL474vuPE8l+4nQ64fjt6AkXiwU9TJw3UwoFQc+0Uswi99gLU4e75/OmmLVQ0r88x9s3ryn7Pd5tNtgQq9UK6/Uas9mMkmm+khGzKOlTGws/sfA/WQubmPwDKaQfSN3/BlEacCsQTxs+AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/7c2b2042116e0b7583ef403e30fd1076/8ac56/image-20250404175125968.webp 240w,\n/static/7c2b2042116e0b7583ef403e30fd1076/d3be9/image-20250404175125968.webp 480w,\n/static/7c2b2042116e0b7583ef403e30fd1076/9ec5a/image-20250404175125968.webp 807w\"\n              sizes=\"(max-width: 807px) 100vw, 807px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/7c2b2042116e0b7583ef403e30fd1076/8ff5a/image-20250404175125968.png 240w,\n/static/7c2b2042116e0b7583ef403e30fd1076/e85cb/image-20250404175125968.png 480w,\n/static/7c2b2042116e0b7583ef403e30fd1076/d2a60/image-20250404175125968.png 807w\"\n            sizes=\"(max-width: 807px) 100vw, 807px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/7c2b2042116e0b7583ef403e30fd1076/d2a60/image-20250404175125968.png\"\n            alt=\"image-20250404175125968\"\n            title=\"image-20250404175125968\"\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<h3 id=\"running-the-program\" style=\"position:relative;\"><a href=\"#running-the-program\" aria-label=\"running the program 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>Running the Program</h3>\n<p>At this point the code is complete, so you can call the <code class=\"language-text\">MessageBoxW</code> API from Rust by running <code class=\"language-text\">cargo run --bin MessageBoxW</code>.</p>\n<p>The windows crate is convenient, so invoking the API was easier than I expected.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 883px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/c781e5e7dc5f4c14ec45277ca518ee86/fe9f1/image-20250403231838773.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: 27.500000000000004%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAABMklEQVQY04WPS2rCUBSGswBxoDHx0cRilaaIpJqHSSvWuib3YbfgQASp0IGCgz6ky+ioSzBYKaWar/emdVoP/JwLl+875yiWdU6t1cAKmjTcJu22j+/7BEGA67lchSHXnQ6e7xGEgfhv0e3W8Dwb27ap1+u0HId+/1ZwHoqu65w4F5T7DpZ/Sei3E/Cm18N13UQkB0iwWq1SqZxhGKcYZlm8K5imSbFYTLp0JcLFYs4+3rGOIrbbLZHoskajEZlMRggMSqUShUKBXC4nQA1d09D+Ih2y5/N5IdRU7h8eeXvf8P31yWbzQbT+Fc5mM1KpVCKSkIyE/ouiqlmeXl7ZxRDHe5E42VLWdDolnU4nJx2AY1Ilq6rcDYesVs/M5wuWyyWTyYTxeMxgMEhOlhse2+yQHzyi4JvgAKRzAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/c781e5e7dc5f4c14ec45277ca518ee86/8ac56/image-20250403231838773.webp 240w,\n/static/c781e5e7dc5f4c14ec45277ca518ee86/d3be9/image-20250403231838773.webp 480w,\n/static/c781e5e7dc5f4c14ec45277ca518ee86/d4bc4/image-20250403231838773.webp 883w\"\n              sizes=\"(max-width: 883px) 100vw, 883px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/c781e5e7dc5f4c14ec45277ca518ee86/8ff5a/image-20250403231838773.png 240w,\n/static/c781e5e7dc5f4c14ec45277ca518ee86/e85cb/image-20250403231838773.png 480w,\n/static/c781e5e7dc5f4c14ec45277ca518ee86/fe9f1/image-20250403231838773.png 883w\"\n            sizes=\"(max-width: 883px) 100vw, 883px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/c781e5e7dc5f4c14ec45277ca518ee86/fe9f1/image-20250403231838773.png\"\n            alt=\"image-20250403231838773\"\n            title=\"image-20250403231838773\"\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>From here on, I plan to try implementing programs in Rust that use various Windows APIs, so as a first step I summarized the procedure up to using <code class=\"language-text\">MessageBoxW</code>.</p>","fields":{"slug":"/rust-winapi-en","tagSlugs":["/tag/rust/","/tag/windows/","/tag/rust-win-api/","/tag/english/"]},"frontmatter":{"date":"2025-04-03","description":"Calling Windows APIs from Rust using the windows crate.","tags":["Rust","Windows","Rust-WinAPI","English"],"title":"Calling Windows APIs from Rust Using the windows Crate","socialImage":{"publicURL":"/static/06d0bddd2b117190bef697c73673143e/rust-winapi.png"}}}},"pageContext":{"slug":"/rust-winapi-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}