<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pulkit Garg on Medium]]></title>
        <description><![CDATA[Stories by Pulkit Garg on Medium]]></description>
        <link>https://medium.com/@dev.pulkit?source=rss-c22df3bbe3aa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qUzJIjChocSMt46b4Kgw2A.png</url>
            <title>Stories by Pulkit Garg on Medium</title>
            <link>https://medium.com/@dev.pulkit?source=rss-c22df3bbe3aa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 22:23:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dev.pulkit/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unleashing the power of C++ within C# through P/Invoke]]></title>
            <link>https://medium.com/@dev.pulkit/unleashing-the-power-of-c-within-c-through-p-invoke-3fd45a8163e7?source=rss-c22df3bbe3aa------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fd45a8163e7</guid>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Pulkit Garg]]></dc:creator>
            <pubDate>Wed, 27 Dec 2023 08:52:02 GMT</pubDate>
            <atom:updated>2023-12-27T08:52:02.550Z</atom:updated>
            <content:encoded><![CDATA[<p>Hola!!<br>In this article we will cover how to use C++ code inside C# project.<br>Let’s First begin with some theoretical part of this concept to understand some technical terms.</p><figure><img alt="Image showing C# and C++ logos" src="https://cdn-images-1.medium.com/max/720/1*YPKGVtba551q3UWxxLmwwQ.png" /></figure><p>1. What is Managed and Unmanaged code.<br> — — — — — — — — — — — — — — — — — — — — — — — — <br>Managed code<br> — C# code which is executed by CLR runtime<br> — Code that can not access low level system resources (OS)<br> — This is also called “Safe code”.<br> — Automatic Garbage Collection, Memory Management, Type referencing are managed by CLR</p><p>Unmanaged code<br> — C# code which is directly executed by OS (resources external to CLR).<br> — Code can access low level system resources (OS memory, etc.)<br> — This is also called as “non safe Code”.<br> — No Automatic Garbage Collection, Type checking is bypassed.</p><p>C++ is unmanaged code and C# is managed code by CLR<br>C++ can use pointers to directly access memory whereas C# cannot by itself.<br>One of the issues faced in making C++ code interoperable with C# is how to go about moving data across the boundary between managed and unmanaged code. This process is known as “M<strong>arshaling”</strong>.</p><p>Now, lets jump directly to what you all are waiting for .. Hands-on Practice.<br>Lets get started with creating our first project in C++ (.dll library project).<br>now, if you are not a C++ developer, or not much code in cpp as primary language then i got you covered.<br>-&gt; First download Visual studio 2022.<br>-&gt; Install “Desktop development with c++” within visual studio installer.<br>-&gt; Create a new project by using template c++ (.dll library project). Name the Project “Example” or name of your wish just dont forget to change the reference file names whereever required.<br>-&gt; Under source files folder create a new file with “example.cpp”<br>-&gt; Add this sample code under this file.<br>-&gt; Compile/build the project.</p><pre>#include &quot;pch.h&quot;<br>#include &lt;stdio.h&gt;<br><br>extern &quot;C&quot;<br>{<br> __declspec(dllexport) int Add(int a, int b)<br> {<br>  return a + b;<br> }<br> __declspec(dllexport) int Subtract(int a, int b)<br> {<br>  return a - b;<br> }<br><br> __declspec(dllexport) int Multiply(int a, int b)<br> {<br>  return a * b;<br> }<br>}</pre><p>Now, its time to create C# Project (Console Application)<br>-&gt; Create a new console C# application<br>-&gt; In Program.cs file add this code snippet.</p><pre>using System.Runtime.InteropServices;<br><br>class Program<br>{<br>    [DllImport(&quot;example.dll&quot;, CallingConvention = CallingConvention.Cdecl)]<br>    public static extern int Add(int a, int b);<br><br>    [DllImport(&quot;example.dll&quot;, CallingConvention = CallingConvention.Cdecl)]<br>    public static extern int Subtract(int a, int b);<br><br>    [DllImport(&quot;example.dll&quot;, CallingConvention = CallingConvention.Cdecl)]<br>    public static extern int Multiply(int a, int b);<br><br>    public static void Main()<br>    {<br><br>        int first = 10, second = 5;<br>        Console.WriteLine(&quot;Input are !&quot;);<br>        Console.WriteLine(&quot;First : &quot; + first);<br>        Console.WriteLine(&quot;second : &quot; + second);<br><br>        Console.WriteLine(&quot;Add : &quot; + Add(first, second));<br>        Console.WriteLine(&quot;Subtract : &quot; + Subtract(first, second));<br>        Console.WriteLine(&quot;Multiply : &quot; + Multiply(first, second));<br>    }<br>}</pre><p>Note: to add multiple functions, you need to add different [DllImport()] attribute for each function (as shown above).<br>-&gt; Now Compile this C# program. <br>-&gt; It will give you error, example.dll is not found (DllNotFoundException)<br>-&gt; now you need to copy the output files (.dll and supporting files) from C++ output folder to bin/* of C# project compiled output.<br>-&gt; Now, Again run the C# application.<br>-&gt; It works now!!</p><p>Thanks for reading this;<br>Follow me for more such amazing content.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fd45a8163e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>