<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Calling Managed Code from a DLL Created in Visual C++ 2008</title>
	<atom:link href="http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/feed/" rel="self" type="application/rss+xml" />
	<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/</link>
	<description></description>
	<lastBuildDate>Tue, 14 Sep 2010 23:51:37 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
	<item>
		<title>By: mk</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-76</link>
		<dc:creator>mk</dc:creator>
		<pubDate>Wed, 16 Jun 2010 11:28:19 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-76</guid>
		<description>Tom - thanks for this great article.

I managed to make c++ wrapper for VB.NET. However I cannot use it in Excel VBA. 
I get an exception File not found related to the VB.NET dll file. However, I&#039;m sure this dll is in current directory, the same where c++ dll is. I also set current directory in 
Workbook_Open() event. I watched current directory in c++ dll and it is the one containing VB.NET dll.
Any ideas?

Best regards</description>
		<content:encoded><![CDATA[<p>Tom &#8211; thanks for this great article.</p>
<p>I managed to make c++ wrapper for VB.NET. However I cannot use it in Excel VBA.<br />
I get an exception File not found related to the VB.NET dll file. However, I&#8217;m sure this dll is in current directory, the same where c++ dll is. I also set current directory in<br />
Workbook_Open() event. I watched current directory in c++ dll and it is the one containing VB.NET dll.<br />
Any ideas?</p>
<p>Best regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johnson</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-29</link>
		<dc:creator>Johnson</dc:creator>
		<pubDate>Tue, 21 Jul 2009 17:38:06 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-29</guid>
		<description>Tom :
thanks for your reply.
the detial is like this :
in CustomCSharpLib.AddClass,
add two methods,
public int GetMyStatus(string strName)
{
	int status=0;
	if(strName==&quot;abc&quot;)
	{
		status=1;
	}
	return status;
}

public int GetMyStatus(byte[] bName)
{
	int status=0;
	string strName=&quot;&quot;;
            	System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(&quot;utf-8&quot;);
            	strName = encoding.GetString(bName);
	if(strName==&quot;abc&quot;)
	{
		status=1;
	}
	return status;

}

how can I call those two methods from  MyDll.dll?

Thanks
Johnson</description>
		<content:encoded><![CDATA[<p>Tom :<br />
thanks for your reply.<br />
the detial is like this :<br />
in CustomCSharpLib.AddClass,<br />
add two methods,<br />
public int GetMyStatus(string strName)<br />
{<br />
	int status=0;<br />
	if(strName==&#8221;abc&#8221;)<br />
	{<br />
		status=1;<br />
	}<br />
	return status;<br />
}</p>
<p>public int GetMyStatus(byte[] bName)<br />
{<br />
	int status=0;<br />
	string strName=&#8221;";<br />
            	System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(&#8220;utf-8&#8243;);<br />
            	strName = encoding.GetString(bName);<br />
	if(strName==&#8221;abc&#8221;)<br />
	{<br />
		status=1;<br />
	}<br />
	return status;</p>
<p>}</p>
<p>how can I call those two methods from  MyDll.dll?</p>
<p>Thanks<br />
Johnson</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Shelton</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-28</link>
		<dc:creator>Tom Shelton</dc:creator>
		<pubDate>Tue, 21 Jul 2009 16:02:01 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-28</guid>
		<description>Johnson...

I&#039;m glad you liked the article.  I&#039;m a little unclear on your question though?  Can you provide a little more information?

Thanks,
Tom</description>
		<content:encoded><![CDATA[<p>Johnson&#8230;</p>
<p>I&#8217;m glad you liked the article.  I&#8217;m a little unclear on your question though?  Can you provide a little more information?</p>
<p>Thanks,<br />
Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JOhnson</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-27</link>
		<dc:creator>JOhnson</dc:creator>
		<pubDate>Sun, 19 Jul 2009 14:41:49 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-27</guid>
		<description>Hi, Tom:
great article . thanks for whareing .

I have a question here : how to pass parameters from c++ code to  call c# method  &quot;createMyAccount(byte[] strName)&quot;  or &quot;getMyValue(string strName)&quot;

For example. 

and in CustomCSharpLib.AddClass,
add two methods,
public void createMyAccount(byte[] strName)
{
.....

}

public int getMyValue(string strName)
{
return 10;
}

Any help?

Thanks

Johnson</description>
		<content:encoded><![CDATA[<p>Hi, Tom:<br />
great article . thanks for whareing .</p>
<p>I have a question here : how to pass parameters from c++ code to  call c# method  &#8220;createMyAccount(byte[] strName)&#8221;  or &#8220;getMyValue(string strName)&#8221;</p>
<p>For example. </p>
<p>and in CustomCSharpLib.AddClass,<br />
add two methods,<br />
public void createMyAccount(byte[] strName)<br />
{<br />
&#8230;..</p>
<p>}</p>
<p>public int getMyValue(string strName)<br />
{<br />
return 10;<br />
}</p>
<p>Any help?</p>
<p>Thanks</p>
<p>Johnson</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Shelton</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-16</link>
		<dc:creator>Tom Shelton</dc:creator>
		<pubDate>Thu, 29 Jan 2009 07:13:19 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-16</guid>
		<description>Wayne,

Here are the steps I used to return a string:

1)  Added the following method to the C# AddClass:
&lt;code&gt;
       public string GetMsg ()
        {
            return &quot;Hello, World!&quot;;
        }
&lt;/code&gt;

2) Opend MyDll.h and added:
&lt;code&gt;
MYDLL_API char* GetMsg();
&lt;/code&gt;

3) Opened MyDll.cpp and added the following usings:
&lt;code&gt;
using namespace System;
using namespace System::Runtime::InteropServices;
&lt;/code&gt;

4) Added this method to MyDll.cpp:
&lt;code&gt;
MYDLL_API char* GetMsg()
{
	AddClass addClass;
	return (char*) Marshal::StringToHGlobalAnsi(addClass.GetMsg()).ToPointer();
}
&lt;/code&gt;

5)  Opened MyDll.def and added
&lt;code&gt;
GetMsg @2
&lt;/code&gt;

6) Opened CallCSharpCodeSample.cpp and added the following typedef under the original:
&lt;code&gt;
typedef char* (WINAPI* LPFN_MSGFUNC)(void);
&lt;/code&gt;

7) Added the following block of code under the original method call:
&lt;code&gt;
		LPFN_MSGFUNC getMsg = (LPFN_MSGFUNC)GetProcAddress(hMod, &quot;GetMsg&quot;);
		if (getMsg != NULL)
		{
			char* msg = getMsg();
			cout &lt;&lt; msg &lt;&lt; endl;
			cout &lt;&lt; GlobalFree((HGLOBAL) msg) &lt;&lt; endl;
		}
		else
		{
			cout &lt;&lt; &quot;Failed to load function GetMsg&quot; &lt;&lt; endl;
		}
&lt;/code&gt;

Notice the call to GlobalFree?  Since I allocated the memory using Marshal.StringToHGlobalAnsi - you have to use GlobalFree to deallocate the memory when your done with the string...

Anyway,  like I said - I&#039;m not an expert at this, so this might not be the recommended way of accomplishing this :)  But, it seems to work ok.</description>
		<content:encoded><![CDATA[<p>Wayne,</p>
<p>Here are the steps I used to return a string:</p>
<p>1)  Added the following method to the C# AddClass:<br />
<code><br />
       public string GetMsg ()<br />
        {<br />
            return "Hello, World!";<br />
        }<br />
</code></p>
<p>2) Opend MyDll.h and added:<br />
<code><br />
MYDLL_API char* GetMsg();<br />
</code></p>
<p>3) Opened MyDll.cpp and added the following usings:<br />
<code><br />
using namespace System;<br />
using namespace System::Runtime::InteropServices;<br />
</code></p>
<p>4) Added this method to MyDll.cpp:<br />
<code><br />
MYDLL_API char* GetMsg()<br />
{<br />
	AddClass addClass;<br />
	return (char*) Marshal::StringToHGlobalAnsi(addClass.GetMsg()).ToPointer();<br />
}<br />
</code></p>
<p>5)  Opened MyDll.def and added<br />
<code><br />
GetMsg @2<br />
</code></p>
<p>6) Opened CallCSharpCodeSample.cpp and added the following typedef under the original:<br />
<code><br />
typedef char* (WINAPI* LPFN_MSGFUNC)(void);<br />
</code></p>
<p>7) Added the following block of code under the original method call:<br />
<code><br />
		LPFN_MSGFUNC getMsg = (LPFN_MSGFUNC)GetProcAddress(hMod, "GetMsg");<br />
		if (getMsg != NULL)<br />
		{<br />
			char* msg = getMsg();<br />
			cout &lt;&lt; msg &lt;&lt; endl;<br />
			cout &lt;&lt; GlobalFree((HGLOBAL) msg) &lt;&lt; endl;<br />
		}<br />
		else<br />
		{<br />
			cout &lt;&lt; &quot;Failed to load function GetMsg&quot; &lt;&lt; endl;<br />
		}<br />
</code></p>
<p>Notice the call to GlobalFree?  Since I allocated the memory using Marshal.StringToHGlobalAnsi &#8211; you have to use GlobalFree to deallocate the memory when your done with the string&#8230;</p>
<p>Anyway,  like I said &#8211; I&#8217;m not an expert at this, so this might not be the recommended way of accomplishing this <img src='http://tom-shelton.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   But, it seems to work ok.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wayne</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-15</link>
		<dc:creator>Wayne</dc:creator>
		<pubDate>Thu, 29 Jan 2009 03:18:39 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-15</guid>
		<description>Hi, Tom:

This is an excellent article for calling the managed C# dll from C application.

I have a question regarding the passing C# string back to C function caller.
In C, I&#039;d like to get a string stored in Managed C# dll to display in C, how do I do that?

For example, in your MyDll.dll,
Add one function,

void GetmyMessage(char* myMsg)
{
   AddClass addClass;
  addClass.GetMyMessage(myMsg);

}

and in CustomCSharpLib.AddClass,
add one method,
public void GetMyMessage(ref byte[] strMsg)
{
  string MyMsg = &quot;Hello.&quot;;
  strMsg = Encoding.ASCII.GetBytes(MyMsg);

}

Any help?

Thanks

Wayne</description>
		<content:encoded><![CDATA[<p>Hi, Tom:</p>
<p>This is an excellent article for calling the managed C# dll from C application.</p>
<p>I have a question regarding the passing C# string back to C function caller.<br />
In C, I&#8217;d like to get a string stored in Managed C# dll to display in C, how do I do that?</p>
<p>For example, in your MyDll.dll,<br />
Add one function,</p>
<p>void GetmyMessage(char* myMsg)<br />
{<br />
   AddClass addClass;<br />
  addClass.GetMyMessage(myMsg);</p>
<p>}</p>
<p>and in CustomCSharpLib.AddClass,<br />
add one method,<br />
public void GetMyMessage(ref byte[] strMsg)<br />
{<br />
  string MyMsg = &#8220;Hello.&#8221;;<br />
  strMsg = Encoding.ASCII.GetBytes(MyMsg);</p>
<p>}</p>
<p>Any help?</p>
<p>Thanks</p>
<p>Wayne</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Call .Net Method from VC++ &#124; keyongtech</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-14</link>
		<dc:creator>Call .Net Method from VC++ &#124; keyongtech</dc:creator>
		<pubDate>Sun, 18 Jan 2009 17:37:01 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-14</guid>
		<description>[...] I understand what your asking, check out this little walk through I created for another discussion: http://tomshelton.wordpress.com/2008...visual-c-2008/  I don&#039;t claim to be an expert at this, but I have done this a couple of times, and it seemed to [...]</description>
		<content:encoded><![CDATA[<p>[...] I understand what your asking, check out this little walk through I created for another discussion: <a href="http://tomshelton.wordpress.com/2008...visual-c-2008/" rel="nofollow">http://tomshelton.wordpress.com/2008&#8230;visual-c-2008/</a>  I don&#8217;t claim to be an expert at this, but I have done this a couple of times, and it seemed to [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Shelton</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-13</link>
		<dc:creator>Tom Shelton</dc:creator>
		<pubDate>Wed, 31 Dec 2008 22:29:43 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-13</guid>
		<description>DA,

The VB dll needs to be either in the GAC, or in the same directory as MyDll.dll.  So, you will need to have at minumum:

TheApplication.exe
MyDll.dll
VBCodeLib.dll

All in the same directory.
HTH</description>
		<content:encoded><![CDATA[<p>DA,</p>
<p>The VB dll needs to be either in the GAC, or in the same directory as MyDll.dll.  So, you will need to have at minumum:</p>
<p>TheApplication.exe<br />
MyDll.dll<br />
VBCodeLib.dll</p>
<p>All in the same directory.<br />
HTH</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DA</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-12</link>
		<dc:creator>DA</dc:creator>
		<pubDate>Wed, 31 Dec 2008 15:27:20 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-12</guid>
		<description>Turns out I was wrong about the last problem. I can LoadLibrary() AND GetProcAddress() but the call fails. Any ideas?</description>
		<content:encoded><![CDATA[<p>Turns out I was wrong about the last problem. I can LoadLibrary() AND GetProcAddress() but the call fails. Any ideas?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DA</title>
		<link>http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/comment-page-1/#comment-11</link>
		<dc:creator>DA</dc:creator>
		<pubDate>Wed, 31 Dec 2008 11:14:24 +0000</pubDate>
		<guid isPermaLink="false">http://tomshelton.wordpress.com/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/#comment-11</guid>
		<description>Changed
HMODULE hMod = LoadLibrary(_T&quot;MyDll.dll&quot;));
to
HMODULE hMod = LoadLibrary(&quot;MyDll.dll&quot;);

Fixed the problem. Thanks a lot for your help!</description>
		<content:encoded><![CDATA[<p>Changed<br />
HMODULE hMod = LoadLibrary(_T&#8221;MyDll.dll&#8221;));<br />
to<br />
HMODULE hMod = LoadLibrary(&#8220;MyDll.dll&#8221;);</p>
<p>Fixed the problem. Thanks a lot for your help!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

