<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dotnet thoughts &#187; RegSvr</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/regsvr/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Wed, 08 Feb 2012 03:18:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Consuming a C++ DLL in C#</title>
		<link>http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/</link>
		<comments>http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 09:41:24 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Win 32 API]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[RegSvr]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=323</guid>
		<description><![CDATA[While working on my current project, I had to use some low level I/O operations, but it was difficult using C#, and I got some C++ implementations. Then I thought of creating a DLL in C++ and use it in &#8230; <a href="http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While working on my current project, I had to use some low level I/O operations, but it was difficult using C#, and I got some C++ implementations. Then I thought of creating a DLL in C++ and use it in C#, but I didn&#8217;t get any code for the implementation. So I done some searching and I found a solution. It is not a complete solution, but it works <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Creating a DLL using C++</strong><br />
For creating a DLL in C++, I was used cl.exe, which comes with .net framework. For the implementation I just wrote simple C++ file.(Simple.cpp)</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

extern &quot;C&quot; __declspec(dllexport) char* Hello();
char* Hello()
{
	return &quot;Hello world&quot;;
}
</pre>
<p>I think this is pretty much clear.The extern &#8220;C&#8221; __declspec(dllexport) allows generate export names automatically. For more details :<a href="http://msdn.microsoft.com/en-us/library/a90k134d(VS.80).aspx" target="_blank">Exporting from a DLL Using __declspec(dllexport)(MSDN)</a></p>
<p>After creating this Simple.cpp file, go to Visual Studio Tools &gt; Visual Studio 2008 Command Prompt. Go to the location where you have stored the Simple.cpp file and for compiling and linking C++ file you can use &#8220;cl.exe /LD Simple.cpp&#8221;.(For more details about cl.exe options checkout : <a href="http://msdn.microsoft.com/en-us/library/fwkeyyhe(VS.71).aspx" target="_blank">Compiler Options Listed Alphabetically(MSDN)</a> It will compile and Link, and gives a DLL as output.</p>
<p><strong>Consuming a C++ library in C#</strong><br />
When I try to add the DLL by Add Reference, Visual Studio will not allows to add C++ library as Reference. So I tried it with Interop option, by using DLLImportAttribute.</p>
<pre class="brush: csharp; title: ; notranslate">
[DllImport(@&quot;D:\Simple.dll&quot;, EntryPoint = &quot;Hello&quot;)]
public extern static string Hello();
</pre>
<p>Then you can call this function in C# like the following</p>
<pre class="brush: csharp; title: ; notranslate">
private void button1_Click(object sender, EventArgs e)
{
            MessageBox.Show(Hello());
}
</pre>
<p>It will display a Messagebox with &#8220;Hello World&#8221;. Thats it you consumed a C++ DLL in C#.</p>
<p><strong>Issue in the implementation</strong></p>
<ol>
<li>I can&#8217;t use the DllImport function without the full location. To avoid this I tried to Register the DLL using RegSvr32.But I got some error from RegSvr like this.</li>
<blockquote><p>The module &#8220;D:\Simple.dll&#8221; was loaded but the entry-point DllRegisterServer was not found.</p>
<p>Make sure that &#8220;D:\Simple.dll&#8221; is a valid DLL or OCX file and then try again.</p></blockquote>
</ol>
<p>I still exploring the things, I will update once I got the solution for this. Happy Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/12/09/ilmerge-a-brief-introduction/" title="Permanent link to ILMerge – A Brief Introduction">ILMerge – A Brief Introduction</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/06/30/how-to-use-taskdialog-api-in-c/" title="Permanent link to How to use TaskDialog API in C#">How to use TaskDialog API in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/12/14/how-to-turn-off-monitor-programmatically-using-c/" title="Permanent link to How to turn off monitor programmatically using C#">How to turn off monitor programmatically using C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/12/18/how-to-record-sound-using-c/" title="Permanent link to How to record sound using C#">How to record sound using C#</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

