<?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>Tim Van Wassenhove &#187; C++</title>
	<atom:link href="http://www.timvw.be/category/information-technology/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timvw.be</link>
	<description>The journey of a thousand miles begins with one step.</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:07:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using .Net assemblies in your WIN32 application</title>
		<link>http://www.timvw.be/using-net-assemblies-in-your-win32-application/</link>
		<comments>http://www.timvw.be/using-net-assemblies-in-your-win32-application/#comments</comments>
		<pubDate>Sat, 22 Apr 2006 00:42:49 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.timvw.be/using-net-assemblies-in-your-win32-application/</guid>
		<description><![CDATA[Imagine that you&#8217;ve got an extensive codebase using WIN32/MFC and don&#8217;t want to give that up but on the other hand you&#8217;d like to take advantage of DOTNET classes then here&#8217;s a simple solution: First we write an Interface and an Implementation with C# as following: public interface IQuoteClient { String getQuote(); Boolean setQuote(String quote); [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine that you&#8217;ve got an extensive codebase using WIN32/MFC and don&#8217;t want to give that up but on the other hand you&#8217;d like to take advantage of DOTNET classes then here&#8217;s a simple solution: First we write an Interface and an Implementation with C# as following:</p>
<pre class="brush: csharp;">
public interface IQuoteClient {
  String getQuote();
  Boolean setQuote(String quote);
}

public class QuoteClient : IQuoteClient {
  // COM requires a parameterless constructor
  public QuoteClient() { ; }
  public string getQuote() { return String.Format&quot;quote&quot;;}
  public bool setQuote(string quote) { return true; }
}
</pre>

<p>Go the project Properties and check the &#8220;Make assembly COM-Visible&#8221; box which you find in the Application tab, Assembly Information. Then you go to the Build tab and check &#8220;Register for COM interop&#8221; box and at the Signing tab you check the &#8220;Sign the assembly&#8221; box and assign a key. Build the project.</p>

<p>Now we have to extract a typelibrary, register the typelibrary and install it in the global assembly cache. Open a Visual Studio 2005 Command Prompt and go to your project\bin\Debug directory. Type the following commands:</p>
<pre class="brush: bash;">
tlbexp QuoteClient.dll
regasm QuoteClient.dll /tlb:QuoteClient.tlb
gacutil /i QuoteClient.dll
</pre>

<p>Now you can import the classes in this assembly from your WIN32 application as following:</p>
<pre class="brush: cpp;">
#import &quot;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.tlb&quot;
#import &quot;D:\projects\Test\QuoteClient\bin\Debug\ClientLibrary.tlb&quot; no_namespace named_guids
</pre>

<p>And you can use them just like any other COM component:</p>
<pre class="brush: cpp;">
CoInitialize(NULL);

IQuoteClient *qc = NULL;
HRESULT hr = CoCreateInstance(
  CLSID_QuoteClient, 
  NULL, 
  CLSCTX_INPROC_SERVER, 
  IID_IQuoteClient, 
  reinterpret_cast&lt;void**&gt;(&amp;qc)
);

if (SUCCEEDED(hr)) {
  cout &lt;&lt; &quot;Quote: &quot; &lt;&lt; qc-&gt;getQuote() &lt;&lt; endl;
  qc-&gt;Release();
  qc = NULL;
}

CoUninitialize();
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/using-net-assemblies-in-your-win32-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More marshalling&#8230;</title>
		<link>http://www.timvw.be/more-marshalling/</link>
		<comments>http://www.timvw.be/more-marshalling/#comments</comments>
		<pubDate>Wed, 01 Mar 2006 00:49:22 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.timvw.be/more-marshalling/</guid>
		<description><![CDATA[This snippet uses GetPrivateProfileString that is available in kernel32.dll. Apparently microsoft has decided to remove this useful function from the dotnet api. [DllImport(&#34;kernel32&#34;, SetLastError=true)] extern int GetPrivateProfileString( String ^pSection, String ^pKey, String ^pDefault, StringBuilder ^pValue, int pBufferLen, String ^pFile ); StringBuilder ^buf = gcnew StringBuilder(256); GetPrivateProfileString( &#34;logsection&#34;, &#34;file&#34;, &#34;default&#34;, buf, buf-&#62;Capacity, &#34;example.ini&#34; ); std::string _log_file [...]]]></description>
			<content:encoded><![CDATA[<p>This snippet uses <a href="http://windowssdk.msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getprivateprofilestring.asp">GetPrivateProfileString</a>  that is available in kernel32.dll. Apparently microsoft has decided to remove this useful function from the dotnet api.</p>

<pre class="brush: cpp;">
[DllImport(&quot;kernel32&quot;, SetLastError=true)]
extern int GetPrivateProfileString(
  String ^pSection, 
  String ^pKey, 
  String ^pDefault, 
  StringBuilder ^pValue, 
  int pBufferLen, 
  String ^pFile
);

StringBuilder ^buf = gcnew StringBuilder(256);
GetPrivateProfileString(
  &quot;logsection&quot;, 
  &quot;file&quot;, 
  &quot;default&quot;, 
  buf, 
  buf-&gt;Capacity, 
  &quot;example.ini&quot;
);

std::string _log_file = new string(
  (char*) Marshal::StringToHGlobalAnsi(logf).ToPointer()
);
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/more-marshalling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading unmanaged structs with .NET</title>
		<link>http://www.timvw.be/reading-unmanaged-structs-with-net/</link>
		<comments>http://www.timvw.be/reading-unmanaged-structs-with-net/#comments</comments>
		<pubDate>Mon, 27 Feb 2006 00:50:41 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.timvw.be/reading-unmanaged-structs-with-net/</guid>
		<description><![CDATA[Last week i&#8217;ve spend a lot of time studying System::Runtime::InteropServices. It took me a while to figure out how i could read unmanaged structs with .NET System::IO. Here is a bit of sample code (Should be obvious enough to write a template or generic class for all sorts of structs, just like i did at [...]]]></description>
			<content:encoded><![CDATA[<p>Last week i&#8217;ve spend a lot of time studying <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemRuntimeInteropServices.asp">System::Runtime::InteropServices</a>. It took me a while to figure out how i could read unmanaged structs with .NET <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemio.asp">System::IO</a>. Here is a bit of sample code (Should be obvious enough to write a template or generic class for all sorts of structs, just like i did at the office)</p>
<pre class="brush: cpp;">
typedef struct {
  char name[9];
  int name;
  double sterr;
} TEST;

FileStream ^f = gcnew FileStream(&quot;C:\\TEST.DAT&quot;, FileMode::Open, FileAccess::ReadWrite);
BinaryReader ^r = gcnew BinaryReader(f);
array&lt;Byte&gt; ^buf = r-&gt;ReadBytes(sizeof(TEST));
TEST test;
Marshal::Copy(buf, 0, (IntPtr) &amp;test, sizeof(TEST));
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/reading-unmanaged-structs-with-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preparing for my internship</title>
		<link>http://www.timvw.be/preparing-for-my-internship/</link>
		<comments>http://www.timvw.be/preparing-for-my-internship/#comments</comments>
		<pubDate>Sat, 11 Feb 2006 19:26:22 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.timvw.be/preparing-for-my-internship/</guid>
		<description><![CDATA[Only two more days before my internship starts. I&#8217;m a bit nervous and excited to dive into this adventure. Today i decided to fresh my knowledge of (MS)-C++ a bit up. I&#8217;ve read a tutorial on function pointers and naming conventions. A couple of weeks ago i already had a look at pointers to member [...]]]></description>
			<content:encoded><![CDATA[<p>Only two more days before my internship starts. I&#8217;m a bit nervous and excited to dive into this adventure. Today i decided to fresh my knowledge of (MS)-C++ a bit up. I&#8217;ve read a tutorial on <a href="http://www.newty.de/fpt/index.html">function pointers</a> and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_argument_passing_and_naming_conventions.asp">naming conventions</a>. A couple of weeks ago i already had a look at <a href="http://linuxquality.sunsite.dk/articles/memberpointers/">pointers to member functions</a>.</p>
<pre class="brush: cpp;">
#include &lt;iostream&gt;
using namespace std;

void customcallback() {
        cout &lt;&lt; &quot;running custom callback&quot; &lt;&lt; endl;
}

typedef int (*method)(int, int);

int sum(int a, int b) {
        return a + b;
}

method dosum() {
        return &amp;sum;
}

int main() {
        void (*plugin)() = NULL;
        plugin = &amp;customcallback;
        plugin();

        method mymethod = dosum();
        cout &lt;&lt; mymethod(10, 4) &lt;&lt; endl;

        return 0;
}
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/preparing-for-my-internship/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Unix daemon</title>
		<link>http://www.timvw.be/a-unix-daemon/</link>
		<comments>http://www.timvw.be/a-unix-daemon/#comments</comments>
		<pubDate>Thu, 24 Jun 2004 22:51:41 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.timvw.be/a-unix-daemon/</guid>
		<description><![CDATA[Today i&#8217;ve written a daemon that communicates with the Netsize SMS Gateway. The daemon sources are available for download. You will have to implement your void getCode(double number, char * code) method yourself though.]]></description>
			<content:encoded><![CDATA[<p>Today i&#8217;ve written a daemon that communicates with the <a href="http://www.netsize.com">Netsize SMS Gateway</a>. The <a href="http://www.timvw.be/wp-content/code/cpp/daemon.zip">daemon sources</a> are available for download. You will have to implement your void getCode(double number, char * code) method yourself though.</p>
<p><img src="http://www.timvw.be/wp-content/images/daemon.png" alt="daemon" /></p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/a-unix-daemon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
