<?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>Dot Net Thoughts &#187; Interop</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/interop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.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[WPF]]></category>
		<category><![CDATA[Win 32 API]]></category>
		<category><![CDATA[Windows Forms]]></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 C#, but I didn&#8217;t get any code for the implementation. So I done some searching [...]]]></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;">
#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;">
[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;">
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>
]]></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>
		<item>
		<title>Calling word Macros from .Net</title>
		<link>http://www.dotnetthoughts.net/2007/09/20/calling-word-macros-from-net/</link>
		<comments>http://www.dotnetthoughts.net/2007/09/20/calling-word-macros-from-net/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 11:30:16 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Office Interoperability]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[VB.Net]]></category>
		<category><![CDATA[Word]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/2007/09/20/calling-word-macros-from-net/</guid>
		<description><![CDATA[While using Word automation with .Net sometimes it is required ti call, word macro&#8217;s from C# or VB.Net. Here is the code to call word macros from VB.Net. Dim WordApp As New Microsoft.Office.Interop.Word.Application CType(WordApp, Object).GetType().InvokeMember(&#34;Run&#34;, Reflection.BindingFlags.Default Or Reflection.BindingFlags.InvokeMethod, Nothing, CType(WordApp, Object), New Object() {&#34;helloworld&#34;}) In the code, helloworld is the macro name.Or you can simply [...]]]></description>
			<content:encoded><![CDATA[<p>While using Word automation with .Net sometimes it is required ti call, word macro&#8217;s from C# or VB.Net.</p>
<p>Here is the code to call word macros from VB.Net.</p>
<pre class="brush: vb;">
Dim WordApp As New Microsoft.Office.Interop.Word.Application
CType(WordApp, Object).GetType().InvokeMember(&quot;Run&quot;, Reflection.BindingFlags.Default Or Reflection.BindingFlags.InvokeMethod, Nothing, CType(WordApp, Object), New Object() {&quot;helloworld&quot;})
</pre>
<p>In the code, <em>helloworld</em> is the macro name.Or you can simply call <code>WordApp.Run("helloworld")</code> will also works fine.</p>
<p> </p>
<p>You can also refer link from Microsoft<br />
<a href="http://support.microsoft.com/kb/306683" target="_blank">http://support.microsoft.com/kb/306683</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2007/09/20/calling-word-macros-from-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
