<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: CatterPingu</title>
    <description>The latest articles on Forem by CatterPingu (@catterpingu).</description>
    <link>https://forem.com/catterpingu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F536113%2Fa930b529-404d-4dce-aab0-1f5fddd762c6.png</url>
      <title>Forem: CatterPingu</title>
      <link>https://forem.com/catterpingu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/catterpingu"/>
    <language>en</language>
    <item>
      <title>DLLs in Rust🦀</title>
      <dc:creator>CatterPingu</dc:creator>
      <pubDate>Fri, 20 Oct 2023 16:46:19 +0000</pubDate>
      <link>https://forem.com/catterpingu/dlls-in-rust-1g3j</link>
      <guid>https://forem.com/catterpingu/dlls-in-rust-1g3j</guid>
      <description>&lt;p&gt;Dynamic Link Libraries or the DLLs are components which are called by a process through the loader when needed during runtime. &lt;/p&gt;

&lt;p&gt;DLLs are specially useful so that you're not writing a piece of code again and again and again...&lt;/p&gt;

&lt;p&gt;We'll look at writing a DLL in rust just because writing rust is fun. This should be useful whether you're someone starting on the journey of wielding the power of DLLs or looking for some exposure to the Win32 Api in rust. &lt;/p&gt;

&lt;h3&gt;
  
  
  Executable for loading the DLL
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a binary rust application package using the cargo package manager. 
&lt;code&gt;cargo new LoadDll&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1s2xsizdze90i3rm290.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1s2xsizdze90i3rm290.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To load a dynamic library, we'll need the &lt;code&gt;LoadLibrary&lt;/code&gt; method from the Win32 Api. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the DLL has been loaded, we'll need to pass it to the &lt;code&gt;GetProcAddess&lt;/code&gt; with the name of the function to be loaded which in our case is a function named 'main'(This function will be present int the DLL).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hellodll&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;unsafe&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;LoadLibraryA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;PCSTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hellodll.dll&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="nf"&gt;.as_ptr&lt;/span&gt;&lt;span class="p"&gt;()))}{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;dllmain&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;unsafe&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;GetProcAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hellodll&lt;/span&gt;
                                                    &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;PCSTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"main&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="nf"&gt;.as_ptr&lt;/span&gt;&lt;span class="p"&gt;()))};&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dllmain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;dllmain&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;unsafe&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;dllmain&lt;/span&gt;&lt;span class="p"&gt;()};&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NO SUCH FUNC IN DLL&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DLL not found&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;




&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Remember that all the methods from the Win32 need to be wrapped in an unsafe block which implies that the rust compiler can't guarantee the memory safety of the functions.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imports in the &lt;code&gt;cargo.toml&lt;/code&gt; file:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dependencies&lt;/span&gt;&lt;span class="py"&gt;.windows&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.51.1"&lt;/span&gt;
&lt;span class="n"&gt;features&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Win32_System_LibraryLoader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Win32_Foundation"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now, build the exe using the cargo package manager: &lt;br&gt;
&lt;code&gt;cargo build --release&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If everything has been built correctly, on running the executable we should get:&lt;code&gt;Dll not found&lt;/code&gt; error message because we have not yet created a DLL!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2a2idrk7m8sf964zw12d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2a2idrk7m8sf964zw12d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To be sure that our exe is actually looking for the DLL, fire up the SysInternals' &lt;a href="https://learn.microsoft.com/en-us/sysinternals/downloads/procmon" rel="noopener noreferrer"&gt;Process Monitor&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To view just the information related to whether our exe is searching for the DLL, we'll set 3 filters. &lt;code&gt;Process Name&lt;/code&gt;, &lt;code&gt;Result&lt;/code&gt; and &lt;code&gt;Path&lt;/code&gt; as: &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep2j7zkb9xeziviy60ht.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep2j7zkb9xeziviy60ht.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feraowwxv69vqqau3b3uz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feraowwxv69vqqau3b3uz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2rgjbr8kaj56w8z63qy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2rgjbr8kaj56w8z63qy.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now clicking on &lt;code&gt;apply&lt;/code&gt; and starting the "monitoring", run &lt;code&gt;loaddll.exe&lt;/code&gt; and we should be getting something like:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4jzpymacvg0wxms4sv0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4jzpymacvg0wxms4sv0.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
This gives us the order of the subdirectories the executable tried to search for the DLL and found it at neither. But now we're sure that our exe is looking for a dll named &lt;code&gt;hellodll&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  DLL for loaddll.exe
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First we create a rust library using: &lt;code&gt;cargo new hellodll --lib&lt;/code&gt;. This creates a lib.rs in our rust project without a main. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then write the necessary information in the &lt;code&gt;cargo.toml&lt;/code&gt; file. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We have to specify in the &lt;code&gt;toml&lt;/code&gt; file that we're trying to write a DLL.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"cdylib"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This specifies that this is a C Dynamic Library. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next, we'll make the necessary imports from the windows crate.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dependencies&lt;/span&gt;&lt;span class="py"&gt;.windows&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.51.1"&lt;/span&gt;
&lt;span class="n"&gt;features&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;"Win32_Foundation"&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Win32_System_Services"&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Win32_System_Console"&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Win32_Security"&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Win32_System_Memory"&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Win32_System_Threading"&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Win32_System_WindowsProgramming"&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Win32_System_Diagnostics_Debug"&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Win32_UI_WindowsAndMessaging"&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We'll open a simple &lt;code&gt;MessageBox&lt;/code&gt; from the function which is being imported by the &lt;code&gt;loaddll.exe&lt;/code&gt; and call it "main"(remember that this can be called anything). In order to have this function called from the executable, it also needs to be an &lt;code&gt;extern "C"&lt;/code&gt; function.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;unsafe&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;MessageBoxA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;HWND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;PCSTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello from DLL&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="nf"&gt;.as_ptr&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;PCSTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"HI&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="nf"&gt;.as_ptr&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;MESSAGEBOX_STYLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;


&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Next, to load a dll into the memory at runtime, it has to have a &lt;code&gt;DllMain&lt;/code&gt; function which is like the entry-point to a DLL.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"system"&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;DllMain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;dll_module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;HANDLE&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;call_reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;lpv_reserved&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;call_reason&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we aren't looking to do something special right now when the DLL is loaded, we'll put a place holder &lt;code&gt;_=&amp;gt;&lt;/code&gt; in place of &lt;code&gt;attach&lt;/code&gt; and &lt;code&gt;detach&lt;/code&gt;. &lt;br&gt;
&lt;em&gt;For Reference: &lt;a href="https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain" rel="noopener noreferrer"&gt;MSDN DllMain Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fas8alr694nd8osvbml7i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fas8alr694nd8osvbml7i.png" alt="Image description"&gt;&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now, we also need to specify that the rust compiler should not mess up the function names themselves. This can be done by the &lt;code&gt;#[no_mangle]&lt;/code&gt; attribute above both the "main" and the "DllMain". &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now we're all done and we'll &lt;code&gt;cargo build --release&lt;/code&gt; to build the DLL inside the &lt;code&gt;release&lt;/code&gt; folder of our project directory. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lastly, to be sure that our DLL is exporting the functions according to our requirements, we can inspect the DLL using the &lt;a href="https://ntcore.com/?page_id=388" rel="noopener noreferrer"&gt;NT Core's CFF Explorer Suite&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvjen5cpbzd1l0a51kb6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvjen5cpbzd1l0a51kb6.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Sure enough, out &lt;code&gt;hellodll&lt;/code&gt; exports two fucntions &lt;code&gt;main&lt;/code&gt; and &lt;code&gt;DllMain&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finally, place both the executable and the dll in the same directory 
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxc401am3qqh6k8zt0t8.png" alt="Image description"&gt;
and on running the the &lt;code&gt;exe&lt;/code&gt;, we get a message box indicating the expected behaviour of our &lt;code&gt;DLL&lt;/code&gt; called from the &lt;code&gt;exe&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F42qw4yona5ohm68m5loz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F42qw4yona5ohm68m5loz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you've found this enjoyable and useful, and I'm look forward for the amazing things you'll build if you decide to delve deeper into Rust and Windows development. &lt;/p&gt;

&lt;h5&gt;
  
  
  Reference:
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=K6dAquqj5E4" rel="noopener noreferrer"&gt;The Taggart Institute&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://samrambles.com/guides/window-hacking-with-rust/creating-a-dll-with-rust/index.html" rel="noopener noreferrer"&gt;Sam Rambles&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>rust</category>
      <category>winapi</category>
      <category>cybersecurity</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
