{"id":2789,"date":"2022-05-04T07:10:40","date_gmt":"2022-05-04T01:40:40","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2789"},"modified":"2022-05-04T07:16:27","modified_gmt":"2022-05-04T01:46:27","slug":"using-dll-functions-in-rust","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/","title":{"rendered":"Using DLL Functions in Rust"},"content":{"rendered":"<p>When you program in <em><strong>Rust<\/strong><\/em>, especially in a non-trivial project, there is a good chance that you will need to call <em><strong>&#8220;external&#8221;<\/strong><\/em> functions (usually, <em><strong>C\/C++<\/strong><\/em>) that are available in a <em><strong>DLL<\/strong><\/em> (we are talking about the <em><strong>Windows<\/strong><\/em> platform here). It could be because you wish to re-use some code that you have earlier written in <em><strong>C\/C++<\/strong><\/em>, or you might be calling a 3rd party library that provides some functionality you need at this point. OK, how easy is this process?<\/p>\n<p>As it turns out, this is not difficult at all. In this article, I will show the steps to get this integration working.<\/p>\n<p>First, my development environment:<\/p>\n<p style=\"padding-left: 40px;\">1) Visual Studio Professional 2022 (64 bit) 17.1.0 on Windows 10.<\/p>\n<p style=\"padding-left: 40px;\">2) Rust 1.59.0 running on Windows 10 (64 bit)<\/p>\n<p style=\"padding-left: 40px;\">3) CLion 2021.3.4 IDE with Rust Plugin<\/p>\n<h3>Building the DLL and LIB<\/h3>\n<p>Although I could have assumed that we already have the <em><strong>DLL<\/strong><\/em> and corresponding <em><strong>LIB<\/strong><\/em>, let me show you how to create a simple <em><strong>DLL<\/strong><\/em> from scratch using <strong><em>Visual Studio 2022<\/em><\/strong>.<\/p>\n<p>1) Launch <em><strong>VS 2022<\/strong><\/em>. Select the option to <em><strong>&#8220;Create a New Project&#8221;<\/strong><\/em> and choose the type as <em><strong>&#8220;DLL&#8221;. <\/strong><\/em>Click <em><strong>&#8220;Next&#8221;.<\/strong><\/em><\/p>\n<figure id=\"attachment_2790\" aria-describedby=\"caption-attachment-2790\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2790\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/dll-project\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project.jpg\" data-orig-size=\"1334,937\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1651589019&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"New DLL Project\" data-image-description=\"&lt;p&gt;New DLL Project&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;New DLL Project&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project-1024x719.jpg\" class=\"wp-image-2790\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project.jpg?resize=550%2C386&#038;ssl=1\" alt=\"New DLL Project\" width=\"550\" height=\"386\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project.jpg?resize=300%2C211&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project.jpg?resize=1024%2C719&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project.jpg?resize=768%2C539&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project.jpg?w=1334&amp;ssl=1 1334w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2790\" class=\"wp-caption-text\"><strong>New DLL Project<\/strong><\/figcaption><\/figure>\n<p>2) Then specify the <em><strong>Project Name<\/strong><\/em> and <em><strong>Location<\/strong><\/em> and click <em><strong>&#8220;Create&#8221;<\/strong><\/em>:<\/p>\n<figure id=\"attachment_2791\" aria-describedby=\"caption-attachment-2791\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2791\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/dll-project2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project2.jpg\" data-orig-size=\"1334,937\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1651589087&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Project Name and Location\" data-image-description=\"&lt;p&gt;Project Name and Location&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Project Name and Location&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project2-1024x719.jpg\" class=\"wp-image-2791\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project2.jpg?resize=550%2C386&#038;ssl=1\" alt=\"Project Name and Location\" width=\"550\" height=\"386\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project2.jpg?resize=300%2C211&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project2.jpg?resize=1024%2C719&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project2.jpg?resize=768%2C539&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-Project2.jpg?w=1334&amp;ssl=1 1334w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2791\" class=\"wp-caption-text\"><strong>Project Name and Location<\/strong><\/figcaption><\/figure>\n<p>3) This will result in the auto generation of project files, including a bare-bones <em><strong>&#8220;dllmain.cpp&#8221;<\/strong><\/em> file:<\/p>\n<figure id=\"attachment_2792\" aria-describedby=\"caption-attachment-2792\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2792\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/generated-file\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg\" data-orig-size=\"863,483\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1651589266&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Generated Code\" data-image-description=\"&lt;p&gt;Generated Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generated Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg\" class=\"wp-image-2792\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg?resize=550%2C308&#038;ssl=1\" alt=\"Generated Code\" width=\"550\" height=\"308\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg?resize=768%2C430&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg?resize=800%2C450&amp;ssl=1 800w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg?resize=352%2C198&amp;ssl=1 352w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Generated-file.jpg?w=863&amp;ssl=1 863w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2792\" class=\"wp-caption-text\"><strong>Generated Code<\/strong><\/figcaption><\/figure>\n<p>Let us delete the generated function and type in our function, which will be called from <em><strong>Rust<\/strong><\/em>:<\/p>\n<figure id=\"attachment_2793\" aria-describedby=\"caption-attachment-2793\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/C-function.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2793\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/c-function\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/C-function.jpg\" data-orig-size=\"918,309\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1651570193&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Our Exported Function\" data-image-description=\"&lt;p&gt;Our Exported Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Our Exported Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/C-function.jpg\" class=\"wp-image-2793\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/C-function.jpg?resize=550%2C185&#038;ssl=1\" alt=\"Our Exported Function\" width=\"550\" height=\"185\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/C-function.jpg?resize=300%2C101&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/C-function.jpg?resize=768%2C259&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/C-function.jpg?w=918&amp;ssl=1 918w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2793\" class=\"wp-caption-text\"><strong>Our Exported Function<\/strong><\/figcaption><\/figure>\n<p>The extra qualifiers before the function reurn type ensure that this will be exported from the <em><strong>DLL<\/strong><\/em> without any name mangling.<\/p>\n<p>4) Make sure the target is <em><strong>&#8220;x64&#8221;<\/strong><\/em>. Additionally, for our example, we will choose <em><strong>&#8220;Release&#8221;<\/strong><\/em> mode.<\/p>\n<p>5) Build the project. This will result in the files <em><strong>&#8220;DllExample.dll&#8221;<\/strong><\/em> and <em><strong>&#8220;DllExample.lib&#8221;<\/strong><\/em> being created in <em><strong>&#8220;x64\\Release&#8221;<\/strong><\/em> sub-directory within the project directory.<\/p>\n<p>We need the above two files when we build our <em><strong>Rust<\/strong><\/em> project in <em><strong>CLion<\/strong><\/em>.<\/p>\n<h3>Rust Example that Uses the DLL<\/h3>\n<p>We now launch <em><strong>CLion<\/strong><\/em> and create a <em><strong>New Rust<\/strong><\/em> project. Here is the actual <em><strong>Rust<\/strong><\/em> code we will be using:<\/p>\n<figure id=\"attachment_2795\" aria-describedby=\"caption-attachment-2795\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Rust-code.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2795\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/rust-code\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Rust-code.jpg\" data-orig-size=\"953,562\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1651562370&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Rust Code\" data-image-description=\"&lt;p&gt;Rust Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Rust Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Rust-code.jpg\" class=\"wp-image-2795\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Rust-code.jpg?resize=600%2C354&#038;ssl=1\" alt=\"Rust Code\" width=\"600\" height=\"354\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Rust-code.jpg?resize=300%2C177&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Rust-code.jpg?resize=768%2C453&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Rust-code.jpg?w=953&amp;ssl=1 953w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2795\" class=\"wp-caption-text\"><strong>Rust Code<\/strong><\/figcaption><\/figure>\n<p>Since we are making a call to an <em><strong>external<\/strong><\/em> function, we have to declare the equivalent function signature in <em><strong>Rust<\/strong><\/em>, and also specify the external library to use. Notice that we are indicating the <em><strong>&#8220;kind&#8221;<\/strong><\/em> as <em><strong>&#8220;static&#8221;<\/strong><\/em> since we will be linking the <em><strong>Rust<\/strong><\/em> code with our <em><strong>DllExample.lib<\/strong><\/em><\/p>\n<p>Another important thing to note in the above code is the use of <em><strong>&#8220;unsafe&#8221;<\/strong><\/em> block. This is required since an external (non-<strong><em>Rust<\/em><\/strong>) code might have <em><strong>&#8220;unsafe&#8221;<\/strong> <strong>behavior<\/strong><\/em>.<\/p>\n<p>Before we build the <em><strong>Rust<\/strong><\/em> project, we need to copy our <em><strong>DllExample.lib<\/strong><\/em> file to a location that can be configured in the <em><strong>&#8220;LIB&#8221;<\/strong><\/em> path. This is what I did:<\/p>\n<p>a) Copied the LIB file to the <em><strong>Rust<\/strong><\/em> project directory. Here are the files in this directory:<\/p>\n<figure id=\"attachment_2796\" aria-describedby=\"caption-attachment-2796\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Lib-location.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2796\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/lib-location\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Lib-location.jpg\" data-orig-size=\"550,308\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1651562635&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"LIB File Directory\" data-image-description=\"&lt;p&gt;LIB File Directory&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;LIB File Directory&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Lib-location.jpg\" class=\"wp-image-2796\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Lib-location.jpg?resize=500%2C280&#038;ssl=1\" alt=\"LIB File Directory\" width=\"500\" height=\"280\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Lib-location.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Lib-location.jpg?resize=352%2C198&amp;ssl=1 352w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Lib-location.jpg?w=550&amp;ssl=1 550w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2796\" class=\"wp-caption-text\"><strong>LIB File Directory<\/strong><\/figcaption><\/figure>\n<p>Then I updated the <em><strong>&#8220;LIB&#8221;<\/strong><\/em> environment variable to include the path: <em><strong>&#8220;F:\\Rust Projects\\CallingDLL\\&#8221;<\/strong><\/em><\/p>\n<p>b) Copied the <em><strong>DllExample.dll<\/strong><\/em> file to the <em><strong>&#8220;debug&#8221;<\/strong><\/em> subdirectory under <em><strong>&#8220;target&#8221;<\/strong> <\/em>directory:<\/p>\n<figure id=\"attachment_2797\" aria-describedby=\"caption-attachment-2797\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-location.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2797\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/dll-location\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-location.jpg\" data-orig-size=\"725,405\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1651562581&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Executable and DLL\" data-image-description=\"&lt;p&gt;Executable and DLL&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Executable and DLL&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-location.jpg\" class=\"wp-image-2797\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-location.jpg?resize=500%2C279&#038;ssl=1\" alt=\"Executable and DLL\" width=\"500\" height=\"279\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-location.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/DLL-location.jpg?w=725&amp;ssl=1 725w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2797\" class=\"wp-caption-text\"><strong>Executable and DLL<\/strong><\/figcaption><\/figure>\n<p>This is required in order to run the final <em><strong>Executable<\/strong><\/em> file.<\/p>\n<p>We can now build and run the <strong><em>Rust<\/em><\/strong> project.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Here is the program output as it appears within <em><strong>CLion IDE<\/strong><\/em>:<\/p>\n<figure id=\"attachment_2798\" aria-describedby=\"caption-attachment-2798\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2798\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/04\/using-dll-functions-in-rust\/output-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Output.jpg\" data-orig-size=\"1132,237\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1651562442&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Output-1024x214.jpg\" class=\"wp-image-2798\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Output.jpg?resize=550%2C115&#038;ssl=1\" alt=\"Program Output\" width=\"550\" height=\"115\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Output.jpg?resize=300%2C63&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Output.jpg?resize=1024%2C214&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Output.jpg?resize=768%2C161&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Output.jpg?w=1132&amp;ssl=1 1132w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2798\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>This shows that our external function in the <em><strong>DLL<\/strong><\/em> is called from within <em><strong>Rust<\/strong><\/em>. You can download the relevant files from <a href=\"http:\/\/www.rangakrish.com\/downloads\/RustDLLExample.zip\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>That was straightforward, right? You may find <a href=\"https:\/\/docs.rust-embedded.org\/book\/interoperability\/c-with-rust.html\" target=\"_blank\" rel=\"noopener\"><em><strong>this<\/strong><\/em><\/a> article useful if you would like to explore <em><strong>&#8220;Foreign Function Interface (FFI)&#8221;<\/strong><\/em> of <em><strong>Rust<\/strong><\/em> in greater detail.<\/p>\n<p>Have a nice week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you program in Rust, especially in a non-trivial project, there is a good chance that you will need to call &#8220;external&#8221; functions (usually, C\/C++) that are available in a DLL (we are talking about the Windows platform here). It could be because you wish to re-use some code that you have earlier written in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[49,17,314],"tags":[67,149,320,317],"class_list":["post-2789","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","category-rust","tag-c","tag-dll","tag-ffi","tag-rust"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-IZ","jetpack-related-posts":[{"id":3070,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/02\/building-v-language-dll\/","url_meta":{"origin":2789,"position":0},"title":"Building V Language DLL","author":"admin","date":"April 2, 2023","format":false,"excerpt":"The relatively new V Programming Language (Vlang) boasts of some interesting features such as \u201cfast compilation\u201d and runtime performance \u201cas fast as C\u201d, at the same time being a \u201csimple language\u201d. As I normally do with other languages that I learn and use, I decided to see how easy it\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"V Program","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/V-source-267x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3017,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/02\/16\/calling-external-dll-functions-from-julia\/","url_meta":{"origin":2789,"position":1},"title":"Calling External DLL Functions from Julia","author":"admin","date":"February 16, 2023","format":false,"excerpt":"Julia supports calling external functions, especially those written in \u201cC\u201d language. As Julia documentation says, such calls do not involve any \u201cboilerplate\u201d code and hence are efficient. In today\u2019s article, I am going to show how to call functions defined in a DLL (Windows 64 bit). Instead of using direct\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"The \"lispEval\" Function","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2645,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/12\/25\/calling-lisp-functions-from-d-language\/","url_meta":{"origin":2789,"position":2},"title":"Calling Lisp Functions from D Language","author":"admin","date":"December 25, 2021","format":false,"excerpt":"After exploring \"newLisp\" in the past few posts, I would like to start looking at the \"D Programming Language\"\u00a0(DLang). DLang\u00a0has been around since 2001. It was originally created by Walter Bright and later Andrei Alexandrescu joined the team in 2007. The main inspiration for DLang was C++, although it uses\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Lisp Function Called from D","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":944,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/06\/10\/calling-lisp-functions-from-go-language\/","url_meta":{"origin":2789,"position":3},"title":"Calling Lisp Functions from Go Language","author":"admin","date":"June 10, 2018","format":false,"excerpt":"In the previous post, we saw how to invoke a Go function from Lisp. In today's post, I would like to explain how to go in the other direction, namely, how to call Lisp functins from Go. There are two ways to do this. The first approach is to load\u2026","rel":"","context":"In &quot;Golang&quot;","block_context":{"text":"Golang","link":"https:\/\/www.rangakrish.com\/index.php\/category\/golang\/"},"img":{"alt_text":"Direct DLL Call","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=525%2C300 1.5x"},"classes":[]},{"id":928,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/05\/27\/calling-go-functions-from-lisp\/","url_meta":{"origin":2789,"position":4},"title":"Calling Go Functions from Lisp","author":"admin","date":"May 27, 2018","format":false,"excerpt":"For the past few weeks I was travelling in the USA and had the pleasure of meeting several friends, some of whom are senior developers and architects. It was interesting to learn that many of them were either using Go language in their daily work, or were passionately endorsing the\u2026","rel":"","context":"In &quot;Golang&quot;","block_context":{"text":"Golang","link":"https:\/\/www.rangakrish.com\/index.php\/category\/golang\/"},"img":{"alt_text":"Part of Header File","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1004,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/08\/05\/sicstus-prolog-building-a-windows-dll\/","url_meta":{"origin":2789,"position":5},"title":"Sicstus Prolog &#8211; Building a Windows DLL","author":"admin","date":"August 5, 2018","format":false,"excerpt":"Last week I upgraded to the latest version (4.4.1) of Sicstus Prolog\u00a0for Windows.\u00a0Since the Prolog engine can be embedded (royalty free) in other applications, it is useful to understand how to create a DLL (on Windows) for this purpose. In today's article, I would like to outline this process in\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Creating Prolog Program Image","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Saving-Prolog-Image.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Saving-Prolog-Image.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Saving-Prolog-Image.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2789"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2789\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}