<?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>Henrik Pedersen&#039;s blog</title>
	<atom:link href="http://hsp.dk/feed/" rel="self" type="application/rss+xml" />
	<link>http://hsp.dk</link>
	<description>Everything about computers, electronics, humor and his everyday life.</description>
	<lastBuildDate>Wed, 10 Aug 2011 08:46:10 +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>Simple Javascript protection of HTML content.</title>
		<link>http://hsp.dk/2011/06/simple-javascript-protection-of-html-content/</link>
		<comments>http://hsp.dk/2011/06/simple-javascript-protection-of-html-content/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 22:47:57 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=179</guid>
		<description><![CDATA[This is definitively a highly debatable topic. Most people will argue that Javascript obfuscation, or just obfuscation in general has no purpose in this world. And I actually think they are right. However, rules are there to be broken, and sometime you would actually want something like this to keep the &#8220;basic copy pasters&#8221; away from your stuff. [...]]]></description>
			<content:encoded><![CDATA[<p>This is definitively a highly debatable topic. Most people will argue that Javascript obfuscation, or just obfuscation in general has no purpose in this world. And I actually think they are right.</p>
<p>However, rules are there to be broken, and sometime you would actually want something like this to keep the &#8220;basic copy pasters&#8221; away from your stuff. But how would you accomplish that in an easy and simple way?</p>
<p>&nbsp;</p>
<p>Most people just base64 encode / decode, or use eval or even unescape on their strings i Javascript. It works good but the kiddies sometimes get through. So when you want a little higher level of protection, you might want to try this, which I wrote for a friend.</p>
<p>It&#8217;s just two simple PHP functions to include in your page. Then you can use the &#8220;protected_echo&#8221; function to echo out a javascript element with all the code necessary to decrypt the content on the client side. It&#8217;s still easy for anyone with a little knowledge of Javascript / HTML  to get to the guts of this, but if you have such highly confidential material laying around on the public internet, you should really revise your strategy!</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Protected demo!&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #666666; font-style: italic;">//Start copying the functions here.</span>
<span style="color: #000000; font-weight: bold;">function</span> protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span> 
    <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
        <span style="color: #000088;">$tmp</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'%'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">bin2hex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
<span style="color: #000000; font-weight: bold;">function</span> protected_echo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;script language=<span style="color: #000099; font-weight: bold;">\&quot;</span>javascript<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt; 
eval(unescape('&quot;</span> <span style="color: #339933;">.</span> protect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;document.write(unescape(<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span>protect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>.split(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>).reverse().join(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>)))&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'.split(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>).reverse().join(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>))); 
&lt;/script&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//End copy here.</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span> 
&nbsp;
&lt;p&gt;Some random HTML stuff or anything else here...&lt;/p&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #666666; font-style: italic;">//Down here we are using the protected_echo function.</span>
protected_echo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Protection test!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>And the resulting HTML code on the client side will be like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Protected demo<span style="color: #339933;">!&lt;/</span>title<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Some random HTML stuff or anything <span style="color: #b1b100;">else</span> here<span style="color: #339933;">...&lt;/</span>p<span style="color: #339933;">&gt;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;script language</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span> 
&nbsp;
<span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span>unescape<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'92%92%92%22%22%82%e6%96%f6%a6%e2%92%82%56%37%27%56%67%56%27%e2%92%22%22%82%47%96%c6%07%37%e2%22%52%53%03%52%73%23%52%63%66%52%73%43%52%63%53%52%63%33%52%73%43%52%63%93%52%63%66%52%63%56%52%23%03%52%73%43%52%63%53%52%73%33%52%73%43%52%23%13%22%82%56%07%16%36%37%56%e6%57%82%56%47%96%27%77%e2%47%e6%56%d6%57%36%f6%46%'</span><span style="color: #339933;">.</span><span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>reverse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;&lt;/</span>body<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Hope you like it, enjoy! ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2011/06/simple-javascript-protection-of-html-content/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Looking for new software ideas</title>
		<link>http://hsp.dk/2011/04/looking-for-new-software-ideas/</link>
		<comments>http://hsp.dk/2011/04/looking-for-new-software-ideas/#comments</comments>
		<pubDate>Sun, 24 Apr 2011 19:06:27 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=149</guid>
		<description><![CDATA[Well this isn&#8217;t exactly my &#8220;normal&#8221; type of post. It is not about programming (not directly at least) and it&#8217;s not some kind of release (yet). And it&#8217;s not a tutorial where I&#8217;m going to teach you, this is more like a poll where I need your help. &#160; &#160; &#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Well this isn&#8217;t exactly my &#8220;normal&#8221; type of post.</p>
<p>It is not about programming (not directly at least) and it&#8217;s not some kind of release (yet).</p>
<p>And it&#8217;s not a tutorial where I&#8217;m going to teach you, this is more like a poll where I need <em>your</em> help.</p>
<p><a href="http://hsp.dk/wp-content/uploads/2011/04/questionSoftware.jpg"><img class="alignleft size-full wp-image-151" title="questionSoftware" src="http://hsp.dk/wp-content/uploads/2011/04/questionSoftware.jpg" alt="What do you want from YOUR software?" width="265" height="265" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>What I would like to ask</strong>, what type of program are you currently looking for / in need of. Or more specifically, what functions are you missing with your current software?</p>
<p>Is it smarter ways to manage pictures, music, videos, or maybe software that helps you doing everyday work? Is it performance monitors, email clients, security software, social networking clients or something totally else?</p>
<p>I would <em>really </em>appreciate it, if you would let your thoughts flow in the comments below! Thank you everybody!</p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2011/04/looking-for-new-software-ideas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Software release with sourcode: RSA File</title>
		<link>http://hsp.dk/2011/04/software-release-with-sourcode-rsa-file/</link>
		<comments>http://hsp.dk/2011/04/software-release-with-sourcode-rsa-file/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 10:23:32 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Sourcecode / Releases]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=129</guid>
		<description><![CDATA[&#160; Wow this gives a new definition to the internet term: &#8220;Long time no see&#8221;. But I haven&#8217;t forgotten my lovely readers out there. I just took a break (I should have informed you I guess) so I could get back coding. Lot&#8217;s have happened in this time. I&#8217;ve written quite a few applications, some [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p><em><strong>Wow this gives a new definition to the internet term: &#8220;Long time no see&#8221;.</strong></em></p>
<p>But I haven&#8217;t forgotten my lovely readers out there. I just took a break (I should have informed you I guess) so I could get back coding.</p>
<p>Lot&#8217;s have happened in this time. I&#8217;ve written quite a few applications, some which I finished, others just &#8220;bases&#8221; for later coding.</p>
<p>I&#8217;ve extended some of my own private libraries and even sent my home automation project out into V5 (more on that later).</p>
<p>But one of the few &#8220;gems&#8221; I managed to make was my little project: <em>RSA File.</em></p>
<p>Why that stupid name? Because RSA is really refering to the public key encryption RSA.</p>
<p><a href="http://hsp.dk/wp-content/uploads/2011/04/RSAFile1.jpg"><img class="alignleft size-full wp-image-133" title="RSAFile1" src="http://hsp.dk/wp-content/uploads/2011/04/RSAFile1.jpg" alt="The main window of the program" width="280" height="132" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>The whole idea to short it down is that fx. you are on a forum. And you need to have someone send over some information to you without others reading it. On this forum identity isn&#8217;t a problem so you know who are who, but everone can read everone elses messages. Or maybe a public chat where you need to exchange some secret file. What do you do?</p>
<p>The classic approach would be with a preshared encryptionkey. That could work if the chat or forum has private messages enabled.</p>
<p>But what if you can&#8217;t trust the admins in there and it&#8217;s the only way you have to communicate? Or you just want it to go fast?</p>
<p>How would you encrypt your files without ever disclosing a password in public?</p>
<p>It&#8217;s quite simple, using <em>public key cryptography </em>we can do just that! Using RSA (a common method of public key encryption) we can make a private and a public key. The public key can be sent to everyone and they can encrypt some data. But ONLY the private key can read it. If you encrypt something with the public key, the public key can&#8217;t decrypt it.</p>
<p>So everyone can send their public keys out into the public but only the private key (the original receiver) can decrypt the content. Public key encryption has however one major drawback. It only supports (at least in .NET) up to 1024 bytes of data and it&#8217;s really slow compared to classic encryption with pre shared keys.</p>
<p>&nbsp;</p>
<p><em>To read more about public key encryption you can visit <a href="http://en.wikipedia.org/wiki/Public-key_cryptography">http://en.wikipedia.org/wiki/Public-key_cryptography</a> otherwise, move along :)</em></p>
<p>&nbsp;</p>
<p>But how are we going to pull this into a program then? No file now a days is only 1024 bytes?</p>
<p>Well&#8230; We will just combine both classic (also called symmetric) encryption with public key encryption (called asymmetric encryption).</p>
<p>We will ask the user for a file, generate a completely random password, unknown to the user, and then encrypt the file with that password using symmetric encryption.</p>
<p>After that we will encrypt the password with the public key given by the user and then put it at the beginning of the file!</p>
<p>Then the receiver receives the file he will have the private key, and be able to decrypt the random password and then decrypt the whole file.</p>
<p>Everyone can know the public key but only the original receiver which holds the private key will be able to read the files.</p>
<p><a href="http://hsp.dk/wp-content/uploads/2011/04/RSAFile2.jpg"><img class="alignleft size-medium wp-image-134" title="RSAFile2" src="http://hsp.dk/wp-content/uploads/2011/04/RSAFile2-300x176.jpg" alt="The encryption window of the program" width="300" height="176" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong><em>It&#8217;s all cool and that but I just want to see the source / use the program.</em></strong></p>
<p>Fine with me, I&#8217;m also going to continue programming on my home control system now.</p>
<p>Here are the download links for the source code. You will find the program itself inside the folder as follows:</p>
<p>RSA File &gt; Bin &gt; Release</p>
<p><strong>Download:</strong></p>
<p><strong><a class="downloadlink" href="http://hsp.dk/download/RSAFile.zip" title=" downloaded 42 times" >RSA File - Public file sending (42)</a></strong></p>
<p>&nbsp;</p>
<p><em>Final note:</em></p>
<p>If you see this release elsewhere it&#8217;s because I&#8217;ve released it a few days earlier over at HackForums. The same thing is if some of you from there sees this. I have the user HSPSoftware over there so this <em>is </em> my source. While I&#8217;m at the thing with sources, my source code includes a custom theme from a guy called &#8220;Aeonhack&#8221;. Credits to you Aeon! :)</p>
<p><em>Final final note:</em></p>
<p>It just hit me that some of you are going to put me down like a cracker or something for saying that I&#8217;m over at HackForums too.</p>
<p>But let me put it like this: I&#8217;m active many places on the internet. Some more &#8220;dark&#8221; than others but I always hold myself on the right side of the law. The reason I&#8217;m over at HF is that they have some really clever people there. And I also like to consider my self as a &#8220;self declared spare time <a href="http://en.wikipedia.org/wiki/White_hat_(computer_security)">white hat hacker</a>&#8220;. So for me it&#8217;s just another way of learning. And I also think it&#8217;s healthy for every programmer to understand the &#8220;dark side&#8221; of computers.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2011/04/software-release-with-sourcode-rsa-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>hsp.dk and downtime&#8230;</title>
		<link>http://hsp.dk/2011/02/hsp-dk-and-downtime/</link>
		<comments>http://hsp.dk/2011/02/hsp-dk-and-downtime/#comments</comments>
		<pubDate>Sat, 19 Feb 2011 06:04:28 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=116</guid>
		<description><![CDATA[Hackers.. Crackers.. Bots searching the internet for something to kill&#8230; I really think I have been an offer for one of those the last couple of days. To say it shortly, while writing one of my articles the site started to tell me about no database updates was successful. And it didn&#8217;t matter if I [...]]]></description>
			<content:encoded><![CDATA[<h2>Hackers.. Crackers.. Bots searching the internet for something to kill&#8230;</h2>
<p>I really think I have been an offer for one of those the last couple of days.</p>
<p>To say it shortly, while writing one of my articles the site started to tell me about no database updates was successful. And it didn&#8217;t matter if I clicked on plugins, my homepage, users or anything. Everything kept showing the same. Sometimes I was lucky and got trough to the thing I was trying to reach. But then I tried to submit a form or change something it kept coming back.</p>
<p>It then stroke me that I should check out the frontpage&#8230; Phew&#8230; Nothing, everything went normally. I then clicked on the About page. &#8220;File not found&#8221; (shown from wordpress) and my layout was gone!</p>
<p>Crap! I then clicked around between the pages. In about 1/20 tries the site would show perfectly, the others it would make the &#8220;file not found&#8221; thing.</p>
<p>It became clear that this could not continue if my users (I&#8217;m thinking about you people! ) could not reach my site in 19/20 tries. So I began rolling back the backup.</p>
<p>A few minutes later and everything was going smoothly&#8230; But wait, now I missed big parts of my articles! That just couldn&#8217;t be true. And before I knew of it the &#8220;attack&#8221; was back on. It was there I decided to pull the plug. I saved what could be saved and killed the database and deleted all files.</p>
<p>Next morning, after reading lots of security articles, I began installing WordPress once again. This time with extreme passwords on everything and now I&#8217;ve made it so I can easily send myself a backup of my database right after publishing an article. And the files is going to be backed up once or twice a week.</p>
<p><br class="spacer_" /></p>
<p><strong>But the important thing is that I&#8217;m back. And I&#8217;m back with more articles later today if I get the time on my hands.</strong></p>
<p><br class="spacer_" /></p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2011/02/hsp-dk-and-downtime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing custom media center plugins for windows 7</title>
		<link>http://hsp.dk/2011/02/writing-custom-media-center-plugins-for-windows-7/</link>
		<comments>http://hsp.dk/2011/02/writing-custom-media-center-plugins-for-windows-7/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 03:20:17 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Home automation]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=88</guid>
		<description><![CDATA[What is Windows Media Center? Windows Media Center is a standard piece of software, shipping with most versions of Windows. It has the ability to present varying media in a common “sexy” way. It’s mostly intended for use with media center pc’s, therefore the name. It has built in support for IR remotes, and can [...]]]></description>
			<content:encoded><![CDATA[<h2>What is Windows Media Center?</h2>
<p>Windows Media Center is a standard piece of software, shipping with most versions of Windows. It has the ability to present varying media in a common “sexy” way. It’s mostly intended for use with media center pc’s, therefore the name. It has built in support for IR remotes, and can search for song and video data through Microsofts webservices.</p>
<p>I find it pretty awesome, and use it for my own media center. Primarily for playing video files. It also has extensive support for custom addins. That’s what we are going to dive into today.</p>
<p>(You can read more about Windows Media Center on wikipedia: <a href="http://en.wikipedia.org/wiki/Windows_Media_Center">http://en.wikipedia.org/wiki/Windows_Media_Center</a>)</p>
<p><br class="spacer_" /></p>
<h2>The tools we will need</h2>
<p><br class="spacer_" /></p>
<p>Before we can begin any kind of developement for Windows Media Center, we are going to need some tools. If you use Visual Studio 2010 as I do, you are going to need the Visual Studio 2008. And if you want to play with VB.NET… Well, you can forget it.</p>
<p>I personally don’t really care about which language it is, but I originally wanted to go for VB.NET. That wasn’t an option, and it didn’t stand anywhere that it wasn’t. So I thought that the SDK wasn’t installed and I started debugging. I just by accident opened Visual C# 2008 Express and saw that the SDK popped up there.</p>
<p>You can download Visual Studio 2008 Express edition on this link: <a href="http://www.microsoft.com/express/Downloads/#Visual_Studio_2008_Express_Downloads">Visual Studio 2008 Express editions.</a></p>
<p>Now onto the SDK itself. The one we will need is this file <a href="http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&amp;FamilyID=542fc60e-f3c4-43a0-8dad-b8f14a485a74">WindowsMediaCenterSDK6.msi</a>. Remember to install visual studio c# 2008 first.</p>
<p><br class="spacer_" /></p>
<h2>Creating a basic &#8220;Hello World&#8221; addin</h2>
<p><br class="spacer_" /></p>
<p>Let’s begin by opening Visual C# 2008. If everything went smoothly, you will see a new project template listed.</p>
<p><a href="http://hsp.dk/wp-content/uploads/2011/02/projectStart-1.jpg"><img class="alignleft size-medium wp-image-89" title="projectStart (1)" src="http://hsp.dk/wp-content/uploads/2011/02/projectStart-1-300x168.jpg" alt="" width="300" height="168" /></a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>Just click OK to continue. Wait a few seconds for it to create the project. It may take a little longer than normal.</p>
<p>Then the project has been created, it should automatically open the Readme.htm file in Visual Studio.</p>
<p>This actually serves as a quite good manual for starting out with addin development.</p>
<p>The first point in the list is:</p>
<h3>Create a strong name key file and add it to the assembly</h3>
<p><br class="spacer_" /></p>
<p>This is needed as we are going to install the addin in to the Global Assembly Cache (more on that later). The Global Assembly Cache is by short means a common place for .NET applications to store shared DLL files. But in order to load a DLL file into the GAC (short name), the DLL file must be signed with a strong name. It does not need to be a code signing certificate. A self signed certificate is more than enough.</p>
<p>Anyway, here are the steps involved.</p>
<p><br class="spacer_" /></p>
<ol>
<li>Goto project &gt; MediaCenterApplication1 Properties. </li>
<li>Select “Signing” in menu to the left. </li>
<li>If it’s not checked, go check the “Sign the assembly” option. </li>
<li>Click the dropdown menu and select “”. </li>
<li>Write some key file name (for this purpose I’m calling it “HSP Test Certificate”) and write a password too (in this case just “HSPSoftware”). Always remember password, even though it should be a little stronger than mine ;)</li>
<li>Hold won CTRL + SHIFT and press S on the keyboard. This is just the shortcut for saving all files. (File &gt; Save all).</li>
</ol>
<p>Press F6 to build the solution (Shortcut for Build &gt; Build solution).</p>
<h3>Enter the strong name key in the registration file</h3>
<p><br class="spacer_" /></p>
<p>In this section we are going to “link” the strong name key to the Registration file used later on, then installing the addin.</p>
<p>But first we need to get the Public Key for our strong name. To do this, we will add an “external tool” to Visual Studio for easy access in the future.</p>
<p><br class="spacer_" /></p>
<ol>
<li> First go to Tools &gt; External Tools.</li>
<li> Write “Get Public Key” or anything else which makes sense to you in to the title box.</li>
<li> Write C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\sn.exe in to the command box.</li>
<li> Write -Tp “$(TargetPath)” in the arguments box.</li>
<li> And lastly, uncheck anything else except “Use Output Window”.</li>
<li> Hit Apply and OK.</li>
<li> Go to Tools &gt; Get Public Key (or whatever you named it). Note: You may need to hit debug (F5) first, even though you can’t run a DLL file in the IDE.</li>
<li> Copy the Public Key Token from the outputwindow.</li>
<li> Open Setup &gt; Registration.xml.</li>
<li> Replace “insert_public_key_token_here” with the actual public key.</li>
<li> Save all and go to Build &gt; Rebuild Solution.</li>
</ol>
<p><br class="spacer_" /></p>
<h3>Installing the actual DLL to the GAC and registering with Windows Media Center</h3>
<p><br class="spacer_" /></p>
<p>Microsoft has been kind enough to provide us with a simple solution to do just that. It’s name is DevInstall.cmd and is placed in the root of your project… Unfortunately this don’t work for me. Neither on my old laptop. So you are free to try it out.</p>
<p>If it doesn’t work, or you just prefer to use some 15 year old kids work instead of Microsofts, you can use my own version of the script here. Their script copies the DLL and XML files to %Program Files%. My script just registers both from the Visual Studio bin/Release directory. Well, you can’t move your project folder around without re-running the script. But who does that anyway? (at least not very often). I just prefer it that way so my system isn’t cluttered up by random folders from random developments.</p>
<p>The download link is here: <a class="downloadlink" href="http://hsp.dk/download/MediaCenterInstaller.zip" title=" downloaded 121 times" >Custom Media Center Installer (121)</a>. Remember to edit the file to suit your project name. You are of course free to use it or redistribute it however you like. I don’t take any responsibility for either software- or hardware failure caused by this script.</p>
<p>Supposed you followed all the steps correctly until now, you can open Windows Media Center and see this nice little notification pop up.</p>
<p><a href="http://hsp.dk/wp-content/uploads/2011/02/Success.jpg"><img class="alignleft size-medium wp-image-93" title="Success" src="http://hsp.dk/wp-content/uploads/2011/02/Success-300x179.jpg" alt="" width="300" height="179" /></a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<h2>Creating something a little more useful</h2>
<p><br class="spacer_" /></p>
<p>By now, all the gears should be running and you a properly spinning around your office chair because of the great success you just had. BUT! … It isn’t really useful, is it? Showing some text in a popup, inside windows media center, is maybe cool around your geek friends. But your girlfriend is properly just going to slap you then your media center is starting to show weird Star Wars quotations.</p>
<p>Instead of being slapped, what about letting her control the TV from her iPhone then she can’t find the remote?</p>
<p>Or maybe you will just use this for switching to Discovery Channel, while she’s watching MTV, and get slapped anyway. The choice is yours.</p>
<p>Either way, remote control sounds pretty useful to me! So let’s get coding!</p>
<p><br class="spacer_" /></p>
<p>Until now, we have just lived high on the code provided by Microsoft. I haven’t covered changing it yet, but maybe you have already figured out how to change the default popup message into something else.</p>
<p>If you haven’t it’s okay. Just navigate to Code &gt; Application.cs.</p>
<p>You will see a lot of code in there, but we only want to dive into this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> temp <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;The background application did something.&quot;</span><span style="color: #008000;">;</span>
	DialogTest<span style="color: #008000;">&#40;</span>temp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> DialogTest<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> strClickedText<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #6666cc; font-weight: bold;">int</span> timeout <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">bool</span> modal <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> caption <span style="color: #008000;">=</span> Resources<span style="color: #008000;">.</span><span style="color: #0000FF;">DialogCaption</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>host <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">Dialog</span><span style="color: #008000;">&#40;</span>strClickedText,
							caption,
							<span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> DialogButtons<span style="color: #008000;">.</span><span style="color: #0000FF;">Ok</span> <span style="color: #008000;">&#125;</span>,
							timeout,
							modal,
							<span style="color: #0600FF; font-weight: bold;">null</span>,
							<span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span>DialogResult dialogResult<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">else</span>
	<span style="color: #008000;">&#123;</span>
		Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;DialogTest&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The start() routine is initiated from the Launch.cs. It is called then Windows Media Player is loading the addin.</p>
<p>DialogTest() is not going to be needed. This is just for easy showing of dialogs.</p>
<p><br class="spacer_" /></p>
<p>Let’s start out by going to the top and put in these namespaces under the existing.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net.Sockets</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span></pre></div></div>

<p>While you are up there, define this variable just under “private AddInHost host;”</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> TcpListener tcpServer<span style="color: #008000;">;</span></pre></div></div>

<p>We just set up the required using declarations and defined a “placeholder” for a our TcpListener.</p>
<p>We did this as we are going to use TCP based communication to receive any commands given.</p>
<p>Go back to the start() routine and add this to start the server and make a basic loop to accept incoming clients.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">tcpServer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TcpListener<span style="color: #008000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">IPAddress</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Any</span>, <span style="color: #FF0000;">3200</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
tcpServer<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
TcpClient client <span style="color: #008000;">=</span> tcpServer<span style="color: #008000;">.</span><span style="color: #0000FF;">AcceptTcpClient</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>There, you could just make the usual stream in and out and loop back. But instead we are going to launch each new client on a new thread. This is done so the server (our addin) can accept multiple simultaneous connections.</p>
<p>We will do this by creating this new routine. It accepts a single parameter, which is just an object representing a TcpClient.</p>
<p>It will typecast this object into a TcpClient, open the networkstream and then create a 4096 byte buffer for receiving any commands.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> HandleClientComm<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> client<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	TcpClient tcpClient <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>TcpClient<span style="color: #008000;">&#41;</span>client<span style="color: #008000;">;</span>
	NetworkStream clientStream <span style="color: #008000;">=</span> tcpClient<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">4096</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">int</span> bytesRead<span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		bytesRead <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			bytesRead <span style="color: #008000;">=</span> clientStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#40;</span>message, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">4096</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>bytesRead <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #008080; font-style: italic;">//Handle command here</span>
	<span style="color: #008000;">&#125;</span>
tcpClient<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Go back to the start routine once more. We just need to add a couple of lines after the accepting of a new TcpClient.</p>
<p>These lines will make the program define a new thread and then launch the HandleClientComm() while passing the connected TcpClient along to it.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Thread clientThread <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> ParameterizedThreadStart<span style="color: #008000;">&#40;</span>HandleClientComm<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
clientThread<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Now we will just make a method for passing the commands given from the connected TcpClients. (I say TcpClients because there can be more than one at a time).</p>
<p>I could go into a lot of detail here but I will just show you my code (which I use for my own Media Center). It can do the most, play an audiofile, play a videofile, change volume, change channels, change mute, stop, pause, play and so on.</p>
<p>You can add your own commands if you want to of course :)</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//This is just used for defining the available commands. Feel free to change.</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> handleRemoteCommand<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> command<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">//I don't know, just a habbit I have when working with network protocols (after some bad experinces).</span>
	command <span style="color: #008000;">=</span> command<span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//First we check for basic play, pause, stop commands.</span>
	<span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;play&quot;</span><span style="color: #008000;">:</span>
			MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaExperience</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Transport</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PlayRate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span>PlayRates<span style="color: #008000;">.</span><span style="color: #0000FF;">PLAYRATE_PLAY</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;pause&quot;</span><span style="color: #008000;">:</span>
			MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaExperience</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Transport</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PlayRate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span>PlayRates<span style="color: #008000;">.</span><span style="color: #0000FF;">PLAYRATE_PAUSE</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;stop&quot;</span><span style="color: #008000;">:</span>
			MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaExperience</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Transport</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PlayRate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span>PlayRates<span style="color: #008000;">.</span><span style="color: #0000FF;">PLAYRATE_STOP</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008080; font-style: italic;">//If mute.</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;mute&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		command <span style="color: #008000;">=</span> command<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;true&quot;</span><span style="color: #008000;">:</span>
				MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">AudioMixer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mute</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;false&quot;</span><span style="color: #008000;">:</span>
				MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">AudioMixer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Mute</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008080; font-style: italic;">//Set's the volume. This took some time to figure out. Can be a little unprecise sometimes.</span>
	<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;volume&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		command <span style="color: #008000;">=</span> command<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #6666cc; font-weight: bold;">int</span> volumeSet <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			volumeSet <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span>
		<span style="color: #008000;">&#123;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>volumeSet <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> volumeSet <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">50</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">//If someone at Microsoft ever read this little comment... Please make this easier for all of us! ;)</span>
			<span style="color: #6666cc; font-weight: bold;">int</span> actualVolume <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Ceiling</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">AudioMixer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Volume</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">655.35</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">/</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>actualVolume <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> volumeSet<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #008080; font-style: italic;">//This freezes the media center for a second or two. But it's acutally the way Microsoft</span>
				<span style="color: #008080; font-style: italic;">//tells us to set the volume. You can also just change the system volume but I'm</span>
				<span style="color: #008080; font-style: italic;">//not going into that now. For the purpose of this tutorial, this will do.</span>
				<span style="color: #6666cc; font-weight: bold;">int</span> timesToGo <span style="color: #008000;">=</span> actualVolume <span style="color: #008000;">-</span> volumeSet<span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> timesToGo<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#123;</span>
					MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">AudioMixer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">VolumeDown</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #008080; font-style: italic;">//Well... In my case it actually speeds up a little when I give it a little bit of time inbetween commands</span>
					<span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Thread</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>actualVolume <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> volumeSet<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #008080; font-style: italic;">//This freezes the media center for a second or two. But it's acutally the way Microsoft</span>
				<span style="color: #008080; font-style: italic;">//tells us to set the volume. You can also just change the system volume but I'm</span>
				<span style="color: #008080; font-style: italic;">//not going into that now. For the purpose of this tutorial, this will do.</span>
				<span style="color: #6666cc; font-weight: bold;">int</span> timesToGo <span style="color: #008000;">=</span> volumeSet <span style="color: #008000;">-</span> actualVolume<span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> timesToGo<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#123;</span>
					MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">AudioMixer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">VolumeUp</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
					<span style="color: #008080; font-style: italic;">//Well... In my case it actually speeds up a little when I give it a little bit of time inbetween commands</span>
					<span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Thread</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008080; font-style: italic;">//This changes the channel. You can only use this if you have TV tuner.</span>
	<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;channel&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaCenter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TV</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Epg</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Lineup</span> LineUP <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaCenter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TV</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Epg</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Lineup</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		command <span style="color: #008000;">=</span> command<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Generic</span><span style="color: #008000;">.</span><span style="color: #0000FF;">KeyValuePair</span> CID <span style="color: #0600FF; font-weight: bold;">in</span> LineUP<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCallSigns</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>CID<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">PlayMedia</span><span style="color: #008000;">&#40;</span>MediaType<span style="color: #008000;">.</span><span style="color: #0000FF;">TV</span>, CID<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaExperience</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GoToFullScreen</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008080; font-style: italic;">//Use this for playing a single audiofile</span>
	<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;playaudio&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		command <span style="color: #008000;">=</span> command<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>&quot;</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span><span style="color: #008080; font-weight: bold;">\\</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">FileInfo</span> nfo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">FileInfo</span><span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>nfo<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">PlayMedia</span><span style="color: #008000;">&#40;</span>MediaType<span style="color: #008000;">.</span><span style="color: #0000FF;">Audio</span>, command, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaExperience</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GoToFullScreen</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008080; font-style: italic;">//Use this for playing a single videofile</span>
	<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>command<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;playvideo&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		command <span style="color: #008000;">=</span> command<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>&quot;</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span><span style="color: #008080; font-weight: bold;">\\</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">FileInfo</span> nfo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">FileInfo</span><span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>nfo<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">PlayMedia</span><span style="color: #008000;">&#40;</span>MediaType<span style="color: #008000;">.</span><span style="color: #0000FF;">Video</span>, command, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			MediaCenterEnvironment<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaExperience</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GoToFullScreen</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008080; font-style: italic;">//Can't remember where I got these from... Or if it just was MSDN.</span>
<span style="color: #008080; font-style: italic;">//Anyway, if someone feels insulted, feel free to speak up.</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">enum</span> PlayRates
<span style="color: #008000;">&#123;</span>
PLAYRATE_STOP <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
PLAYRATE_PAUSE <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>,
PLAYRATE_PLAY <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="spacer_" /></p>
<p>At last just add the following couple of lines at the end of the handleClientComm() loop</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">UTF8Encoding encoder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UTF8Encoding<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
handleRemoteCommand<span style="color: #008000;">&#40;</span>encoder<span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>message, <span style="color: #FF0000;">0</span>, bytesRead<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><br class="spacer_" /></p>
<p>Just build it / rebuild it. And install it as the first plugin. (My script automatically uninstalls and reinstalls the updated addin. Microsoft’s seems to do that too).</p>
<p>Open Windows Media Center… and…. Voila! Nothing. Well, we that’s because we made it remote controlled. Nothing happens unless we give the order. So let’s create some really simple client. Just make a new project and make this void.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">void</span> sendMediaCenterCommand<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> command, <span style="color: #6666cc; font-weight: bold;">string</span> mediaCenterLocation, <span style="color: #6666cc; font-weight: bold;">int</span> mediaCenterPort<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #000000;">System.<span style="color: #0000FF;">Net</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sockets</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">TcpClient</span> client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Net</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sockets</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">TcpClient</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	client<span style="color: #008000;">.</span><span style="color: #0000FF;">Connect</span><span style="color: #008000;">&#40;</span>mediaCenterLocation, mediaCenterPort<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> outBytes <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Encoding</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UTF8</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>command<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">System.<span style="color: #0000FF;">Net</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sockets</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">NetworkStream</span> stream <span style="color: #008000;">=</span> client<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">.</span><span style="color: #0000FF;">CanRead</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> stream<span style="color: #008000;">.</span><span style="color: #0000FF;">CanWrite</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		stream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>outBytes, <span style="color: #FF0000;">0</span>, outBytes<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		stream<span style="color: #008000;">.</span><span style="color: #0000FF;">Flush</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	stream<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	stream<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	client<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now you can call sendMediaCenterCommand() as much as you want with any command. It takes three arguments.</p>
<ul>
<li>command: The command you want to send.</li>
<li>mediaCenterLocation: The IP of the mediacenter. Just 127.0.0.1 if it’s local.</li>
<li>mediaCenterPort: The port you run the addin on. In this case 3200.</li>
</ul>
<p>You can find all the sourcecode including a full client a little lower on this page. You are free to redistribute it and use it however you like, but a little credit (my name or a link back – maybe both) is very appreciated.</p>
<a class="downloadlink" href="http://hsp.dk/download/MediaCenterSourceCode.zip" title=" downloaded 133 times" >Windows Media Center Addin Source Code (133)</a>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2011/02/writing-custom-media-center-plugins-for-windows-7/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unofficial Google Translate in C# and VB.NET</title>
		<link>http://hsp.dk/2011/01/unofficial-google-translate-in-c-and-vb-net/</link>
		<comments>http://hsp.dk/2011/01/unofficial-google-translate-in-c-and-vb-net/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 02:45:37 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=84</guid>
		<description><![CDATA[Let me first start by apologizing for being away for so long time without writing any posts. I was just not in the mood if you know what I mean. But today I’m going to show you how to translate some text using Google Translate, right inside your own .NET programs. I was in need [...]]]></description>
			<content:encoded><![CDATA[<p>Let me first start by apologizing for being away for so long time without writing any posts. I was just not in the mood if you know what I mean.</p>
<p>But today I’m going to show you how to translate some text using Google Translate, right inside your own .NET programs.</p>
<p>I was in need of that feature today, and at first glance I just thought “sure Google is nice, they have an API for programmers like me”. Turns out they do, but that’s a JSON API intended for javascript. Yes yes, we can indeed access that in .NET but I didn’t want to start messing with JSON at the moment and I always like a challenge. So I begin to look into the possibilities for screen scraping, a process I’m quite comfortable with as I’ve done a lot of it.</p>
<p><em>But be warned. If Google changes anything in their layout your application is likely to break. I will not offer support on this, but If you ask nice I may be able to help you anyway.</em></p>
<p>For me that doesn’t really matter as it’s primarily intended for personal use on a project I’m constantly working on.</p>
<p>So let’s get coding. First take a look at translation. For testing purposes I will go from Danish to English. But normally Google hides the parameters inside Ajax and POST so I will help you do the hard work and show you this URL.</p>
<p><a href="http://translate.google.com/?hl=en&amp;ie=UTF8&amp;text=Hej+verden&amp;langpair=da|en">http://translate.google.com/?hl=en&amp;ie=UTF8&amp;text=Hej+verden&amp;langpair=da|en</a></p>
<p><br class="spacer_" /></p>
<p>
It’s the direct translation URL. Put in your own text instead of “Hej Verden”, change the langpair to suit your needs (da|en means from Danish to English) and you are good to<br />
go.</p>
<p>
Let’s take that into .NET. Today I’m going to present it in C# but there will be a VB example at the end.</p>
<p>Let’s start out by creating a new Windows Forms project and open code view. You should begin with some imports. There should be some auto generated ones, so just insert<br />
this at the end of the imports.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;">;</span></pre></div></div>

<p>After that we can create or initial function. It will take 3 parameters. “input” is gonna be the text to be translated, “langFrom” is the language to translate from in shortcode (like<br />
da or en, for Danish or English) and “langTo” is gonna be the language to translate to, in the same format as “langFrom”.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> TranslateText<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> input, <span style="color: #6666cc; font-weight: bold;">string</span> langFrom, <span style="color: #6666cc; font-weight: bold;">string</span> langTo<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">//Function here</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now we can move on to the next step where we will actually fetch some data from Google. We will create a new instance of WebClient and add the appropriate headers to it<br />
(to make sure Google is going to send us UTF-8 encoded text). From where we will take the parameters passed to the function and insert them into our previous translation<br />
URL, and after that fetch the content.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> TranslateText<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> input, <span style="color: #6666cc; font-weight: bold;">string</span> langFrom, <span style="color: #6666cc; font-weight: bold;">string</span> langTo<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">//Defines a new WebClient</span>
	WebClient Client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Sets the client encoding to UTF8</span>
	Client<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Charset&quot;</span>, <span style="color: #666666;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Creates the string. And yes I prefer this over string.format ! ;)</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> downloadUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://www.google.com/translate_t?hl=da&amp;ie=UTF8&amp;text=&quot;</span> <span style="color: #008000;">+</span> input <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&amp;langpair=&quot;</span><span style="color: #008000;">+</span>langFrom<span style="color: #008000;">+</span><span style="color: #666666;">&quot;|&quot;</span><span style="color: #008000;">+</span>langTo<span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Downloads the string from the URL above</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> data <span style="color: #008000;">=</span> Client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span>downloadUrl<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> data<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now we have the data stored inside our “data” variable. Let’s just parse it real fast. We will begin by finding where the “resultbox” and afterwards parse our way trough until we<br />
hit two “</span>” right after each other (indicating the end of the resultbox).</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> TranslateText<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> input, <span style="color: #6666cc; font-weight: bold;">string</span> langFrom, <span style="color: #6666cc; font-weight: bold;">string</span> langTo<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">//Defines a new WebClient</span>
	WebClient Client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Sets the client encoding to UTF8</span>
	Client<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Charset&quot;</span>, <span style="color: #666666;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Creates the string. And yes I prefer this over string.format ! ;)</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> downloadUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://www.google.com/translate_t?hl=da&amp;ie=UTF8&amp;text=&quot;</span> <span style="color: #008000;">+</span> input <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&amp;langpair=&quot;</span><span style="color: #008000;">+</span>langFrom<span style="color: #008000;">+</span><span style="color: #666666;">&quot;|&quot;</span><span style="color: #008000;">+</span>langTo<span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Downloads the string from the URL above</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> data <span style="color: #008000;">=</span> Client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span>downloadUrl<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Searches for the beginning of the resultbox and cuts everything away before that</span>
	data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;span id=result_box&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">+</span><span style="color: #FF0000;">19</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Finds the ending of the resultbox by searching for two spans right after each other</span>
	data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;/span&gt;&lt;/span&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">+</span><span style="color: #FF0000;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> data<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now we have the contents of the “resultbox” (and a little of it’s beginning) and are ready to move on to the next step. Here we will use a regex for counting the occurences of<br />
spans and afterwards loop through the entire datablock, extract each span and put the together in the variable “translatedText” and return it at the end.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> TranslateText<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> input, <span style="color: #6666cc; font-weight: bold;">string</span> langFrom, <span style="color: #6666cc; font-weight: bold;">string</span> langTo<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">//Defines a new WebClient</span>
	WebClient Client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Sets the client encoding to UTF8</span>
	Client<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Charset&quot;</span>, <span style="color: #666666;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Creates the string. And yes I prefer this over string.format ! ;)</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> downloadUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://www.google.com/translate_t?hl=da&amp;ie=UTF8&amp;text=&quot;</span> <span style="color: #008000;">+</span> input <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&amp;langpair=&quot;</span><span style="color: #008000;">+</span>langFrom<span style="color: #008000;">+</span><span style="color: #666666;">&quot;|&quot;</span><span style="color: #008000;">+</span>langTo<span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Downloads the string from the URL above</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> data <span style="color: #008000;">=</span> Client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span>downloadUrl<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Searches for the beginning of the resultbox and cuts everything away before that</span>
	data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;span id=result_box&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">+</span><span style="color: #FF0000;">19</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Finds the ending of the resultbox by searching for two spans right after each other</span>
	data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;/span&gt;&lt;/span&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">+</span><span style="color: #FF0000;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Defines a new regex used for counting all spans inside the resultbox</span>
	Regex spans <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Regex<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;span&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Finds the count and puts it inside the variable spanOccurences</span>
	<span style="color: #6666cc; font-weight: bold;">int</span> spanOccurences <span style="color: #008000;">=</span> spans<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Defines an empty string for use in the for loop</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> translatedText <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//Extract each tiny bit of text from each span in the resultbox</span>
	<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> spanOccurences<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">//Defines currentBlock and sets it to everything which comes after the first &quot;&lt;span&quot;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> currentBlock <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;span&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">//Finds the ending of the current span and removes everything after that</span>
		currentBlock <span style="color: #008000;">=</span> currentBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>currentBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;/span&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">//Goes back to the beginning and cleans everything from inside the first span</span>
		currentBlock <span style="color: #008000;">=</span> currentBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>currentBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&gt;&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">//Removes the current processed span from the beginning of the data for next extraction</span>
		data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;/span&gt;&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">//Adds the extracted text to the translatedText variable</span>
		translatedText <span style="color: #008000;">+=</span> currentBlock<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008080; font-style: italic;">//Returns the translated text</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> translatedText<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And the full code in C# looks like.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Drawing</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Forms</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">namespace</span> TranslateScraper
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> Form1 <span style="color: #008000;">:</span> Form
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> Form1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			InitializeComponent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Form1_Load<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span>TranslateText<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Tillykke! Dine programmer kan nu bruge Google Oversæt&quot;</span>, <span style="color: #666666;">&quot;da&quot;</span>, <span style="color: #666666;">&quot;en&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
		<span style="color: #008080; font-style: italic;">/// Translates a text using screenscaping on Google Translate</span>
		<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
		<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;input&quot;&gt;The string to translate&lt;/param&gt;</span>
		<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;langFrom&quot;&gt;The language to translate from. Fx &quot;en&quot; for English or &quot;da&quot; for Danish&lt;/param&gt;</span>
		<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;langTo&quot;&gt;The language to translate to in the same format as langFrom&lt;/param&gt;</span>
		<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> TranslateText<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> input, <span style="color: #6666cc; font-weight: bold;">string</span> langFrom, <span style="color: #6666cc; font-weight: bold;">string</span> langTo<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">//Defines a new WebClient</span>
			WebClient Client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Sets the client encoding to UTF8</span>
			Client<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Charset&quot;</span>, <span style="color: #666666;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Creates the string. And yes I prefer this over string.format ! ;)</span>
			<span style="color: #6666cc; font-weight: bold;">string</span> downloadUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://www.google.com/translate_t?hl=da&amp;ie=UTF8&amp;text=&quot;</span> <span style="color: #008000;">+</span> input <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&amp;langpair=&quot;</span><span style="color: #008000;">+</span>langFrom<span style="color: #008000;">+</span><span style="color: #666666;">&quot;|&quot;</span><span style="color: #008000;">+</span>langTo<span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Downloads the string from the URL above</span>
			<span style="color: #6666cc; font-weight: bold;">string</span> data <span style="color: #008000;">=</span> Client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadString</span><span style="color: #008000;">&#40;</span>downloadUrl<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Searches for the beginning of the resultbox and cuts everything away before that</span>
			data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;span id=result_box&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">+</span><span style="color: #FF0000;">19</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Finds the ending of the resultbox by searching for two spans right after each other</span>
			data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;/span&gt;&lt;/span&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">+</span><span style="color: #FF0000;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Defines a new regex used for counting all spans inside the resultbox</span>
			Regex spans <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Regex<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;span&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Finds the count and puts it inside the variable spanOccurences</span>
			<span style="color: #6666cc; font-weight: bold;">int</span> spanOccurences <span style="color: #008000;">=</span> spans<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Defines an empty string for use in the for loop</span>
			<span style="color: #6666cc; font-weight: bold;">string</span> translatedText <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
			<span style="color: #008080; font-style: italic;">//Extract each tiny bit of text from each span in the resultbox</span>
			<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> spanOccurences<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #008080; font-style: italic;">//Defines currentBlock and sets it to everything which comes after the first &quot;&lt;span&quot;</span>
				<span style="color: #6666cc; font-weight: bold;">string</span> currentBlock <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;span&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008080; font-style: italic;">//Finds the ending of the current span and removes everything after that</span>
				currentBlock <span style="color: #008000;">=</span> currentBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>currentBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;/span&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008080; font-style: italic;">//Goes back to the beginning and cleans everything from inside the first span</span>
				currentBlock <span style="color: #008000;">=</span> currentBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>currentBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&gt;&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008080; font-style: italic;">//Removes the current processed span from the beginning of the data for next extraction</span>
				data <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&lt;/span&gt;&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008080; font-style: italic;">//Adds the extracted text to the translatedText variable</span>
				translatedText <span style="color: #008000;">+=</span> currentBlock<span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #008080; font-style: italic;">//Returns the translated text</span>
			<span style="color: #0600FF; font-weight: bold;">return</span> translatedText<span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And in VB.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Text</span>.<span style="color: #0000FF;">RegularExpressions</span>
<span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Net</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Class</span> Form1
	<span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> Form1_Load<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> System.<span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> <span style="color: #FF8000;">MyBase</span>.<span style="color: #0000FF;">Load</span>
		<span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span>TranslateText<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Tillykke! Dine programmer kan nu bruge Google oversæt&quot;</span>, <span style="color: #808080;">&quot;da&quot;</span>, <span style="color: #808080;">&quot;en&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
	<span style="color: #008080; font-style: italic;">''' &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">''' Translates a text using screenscaping on Google Translate</span>
	<span style="color: #008080; font-style: italic;">''' &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">''' &lt;param name=&quot;input&quot;&gt;The string to translate&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">''' &lt;param name=&quot;langFrom&quot;&gt;The language to translate from. Fx &quot;en&quot; for English or &quot;da&quot; for Danish&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">''' &lt;param name=&quot;langTo&quot;&gt;The language to translate to in the same format as langFrom&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">''' &lt;returns&gt;&lt;/returns&gt;</span>
	<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> TranslateText<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> <span style="color: #FF8000;">input</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> langFrom <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">ByVal</span> langTo <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>
		<span style="color: #008080; font-style: italic;">'Defines a new WebClient</span>
		<span style="color: #0600FF;">Dim</span> Client <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> WebClient<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">'Sets the client encoding to UTF8</span>
		Client.<span style="color: #0000FF;">Headers</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Charset&quot;</span>, <span style="color: #808080;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">'Creates the string. And yes I prefer this over string.format ! ;)</span>
		<span style="color: #0600FF;">Dim</span> downloadUrl <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;http://www.google.com/translate_t?hl=da&amp;ie=UTF8&amp;text=&quot;</span> <span style="color: #008000;">&amp;</span> <span style="color: #FF8000;">input</span> <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;&amp;langpair=&quot;</span> <span style="color: #008000;">&amp;</span> langFrom <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;|&quot;</span> <span style="color: #008000;">&amp;</span>
		<span style="color: #008080; font-style: italic;">'Downloads the string from the URL above</span>
		<span style="color: #0600FF;">Dim</span> data <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> Client.<span style="color: #0000FF;">DownloadString</span><span style="color: #000000;">&#40;</span>downloadUrl<span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">'Searches for the beginning of the resultbox and cuts everything away before that</span>
		data <span style="color: #008000;">=</span> data.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>data.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;span id=result_box&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">19</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">'Finds the ending of the resultbox by searching for two spans right after each other</span>
		data <span style="color: #008000;">=</span> data.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>data.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;/span&gt;&lt;/span&gt;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">7</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">'Defines a new regex used for counting all spans inside the resultbox</span>
		<span style="color: #0600FF;">Dim</span> spans <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> Regex<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;span&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">'Finds the count and puts it inside the variable spanOccurences</span>
		<span style="color: #0600FF;">Dim</span> spanOccurences <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> spans.<span style="color: #0000FF;">Matches</span><span style="color: #000000;">&#40;</span>data<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Count</span>
		<span style="color: #008080; font-style: italic;">'Defines an empty string for use in the for loop</span>
		<span style="color: #0600FF;">Dim</span> translatedText <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
		<span style="color: #008080; font-style: italic;">'Extract each tiny bit of text from each span in the resultbox</span>
		<span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">To</span> spanOccurences <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>
			<span style="color: #008080; font-style: italic;">'Defines currentBlock and sets it to everything which comes after the first &quot;&lt;span&quot;</span>
			<span style="color: #0600FF;">Dim</span> currentBlock <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> data.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>data.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;span&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span>
			<span style="color: #008080; font-style: italic;">'Finds the ending of the current span and removes everything after that</span>
			currentBlock <span style="color: #008000;">=</span> currentBlock.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>currentBlock.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;/span&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #008080; font-style: italic;">'Goes back to the beginning and cleans everything from inside the first span</span>
			currentBlock <span style="color: #008000;">=</span> currentBlock.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>currentBlock.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&gt;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
			<span style="color: #008080; font-style: italic;">'Removes the current processed span from the beginning of the data for next extraction</span>
			data <span style="color: #008000;">=</span> data.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>data.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;&lt;/span&gt;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">7</span><span style="color: #000000;">&#41;</span>
			<span style="color: #008080; font-style: italic;">'Adds the extracted text to the translatedText variable</span>
			translatedText <span style="color: #008000;">+=</span> currentBlock
	<span style="color: #FF8000;">Next</span>
	<span style="color: #008080; font-style: italic;">'Returns the translated text</span>
	<span style="color: #FF8000;">Return</span> translatedText
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Class</span></pre></div></div>

<p>And that concludes this tutorial. Thanks for reading, I hope to be back soon with some fresh new content, and possibly an article about my home automation system.</p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2011/01/unofficial-google-translate-in-c-and-vb-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Launching a nuclear war with VB.NET</title>
		<link>http://hsp.dk/2010/12/launching-a-nuclear-war-with-vb-net/</link>
		<comments>http://hsp.dk/2010/12/launching-a-nuclear-war-with-vb-net/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 02:17:18 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Home automation]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=72</guid>
		<description><![CDATA[Today I’m going to show you how to build a “nuclear command center” complete with voice synthesis and a spoken password, just like the movies ;) (Or at least it’s the control interface. Unless you connect it to some sort of military installation, which I strongly suggest you not to, it will only act like [...]]]></description>
			<content:encoded><![CDATA[<p>Today I’m going to show you how to build a “nuclear command center” complete with voice synthesis and a spoken password, just like the movies ;)</p>
<p>(Or at least it’s the control interface. Unless you connect it to some sort of military installation, which I strongly suggest you <span style="text-decoration: underline;">not</span> to, it will only act like a control center and not destroy any civilians while running.)</p>
<p>Anyway, let’s move on with the tutorial.</p>
<p><br class="spacer_" /></p>
<p><strong>The main interface</strong></p>
<p>Just make a standard windows forms application, nothing there. Here is my layout. I just used some completely free icons from <a href="http://www.iconfinder.net">www.iconfinder.net</a>.</p>
<p><a href="http://hsp.dk/wp-content/uploads/2011/02/nuclear.jpg"><img class="alignleft size-full wp-image-73" title="nuclear" src="http://hsp.dk/wp-content/uploads/2011/02/nuclear.jpg" alt="" width="300" height="87" /></a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>I also created some simple routines like “updateNuclearStatus(status)” and “showMessage(message)” to smoothen things up when we need to update our interface. If you want to skip this step, you can download the complete source code at the end. The</p>
<p><strong>The voice recognition stuff</strong></p>
<p>In order for us to do some real cool voice authentication, we will have to use the Windows Speech API. This will provide us with access to the windows text to speech engine, and also the voice recognition engine. At first you will need to add a reference to system.speech. Do this like so.</p>
<p><a href="http://hsp.dk/wp-content/uploads/2010/11/1Speech.jpg"><img class="alignleft size-full wp-image-64" title="1Speech" src="http://hsp.dk/wp-content/uploads/2010/11/1Speech.jpg" alt="" width="300" height="274" /></a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<div id="_mcePaste">Right click your project name and choose Add Reference.</div>
<p><br class="spacer_" /></p>
<p><a href="http://hsp.dk/wp-content/uploads/2010/11/2speech.jpg"><img class="alignleft size-full wp-image-65" title="2speech" src="http://hsp.dk/wp-content/uploads/2010/11/2speech.jpg" alt="" width="300" height="252" /></a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>Scroll down to “System.Speech” and click on it, when “OK”.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>Next we will import it and create some variables to work with. One SpeechSynthesizer and one SpeechRecognizer. The first provides access to text to speech and the other do the opposite thing.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Speech</span>
<span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Speech</span>.<span style="color: #0000FF;">Recognition</span>.<span style="color: #0000FF;">SrgsGrammar</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Class</span> MainForm
	<span style="color: #FF8000;">Private</span> <span style="color: #FF8000;">WithEvents</span> speechRecognitionEngine <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> Recognition.<span style="color: #0000FF;">SpeechRecognizer</span>
	<span style="color: #FF8000;">Private</span> speechSynthesis <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> Synthesis.<span style="color: #0000FF;">SpeechSynthesizer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Class</span></pre></div></div>

<p>Now we will initialize both our variables. First the SpeechSynthesizer. That’s the easy one. Just set a little slow speaking voice, with 100% volume and let it say something cool when the application is first launched.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;">speechSynthesis.<span style="color: #0600FF;">Volume</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span>
speechSynthesis.<span style="color: #0600FF;">Rate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>
speechSynthesis.<span style="color: #0000FF;">SpeakAsync</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Welcome to HSP Nuclear command center.&quot;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>And now onto the interesting part where we will configure the SpeechRecognizer. We will begin by creating a new grammatic document. And when create a new grammatic rule, where we will add some words we want our program to listen for. In fact we will add the whole phonetic alphabet + some extra commands (“Initiate launch sequence”, “Confirm”, “Abort”). We will when add these rules to our grammatic document and make them root. At last we load the whole grammatic document into our SpeechRecognizer.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Dim</span> grammaticDocument <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> SrgsDocument
<span style="color: #0600FF;">Dim</span> phoneticRule <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> SrgsRule<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Phonetic&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">Dim</span> phoneticList <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> SrgsOneOf<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Alpha&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Bravo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Charlie&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Delta&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Echo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Foxtrot&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Golft&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Hotel&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;India&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Juliet&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Kilo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Lima&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Mike&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;November&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Oscar&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Papa&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Quebec&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Romeo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Sierra&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Tango&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Uniform&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Victor&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Whiskey&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;X-ray&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Yankee&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Zulu&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #008080; font-style: italic;">'Just add the commands themself at the end.</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Confirm&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Initiate launch sequence&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> SrgsItem<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Abort&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
phoneticRule.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>phoneticList<span style="color: #000000;">&#41;</span>
grammaticDocument.<span style="color: #0000FF;">Rules</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>phoneticRule<span style="color: #000000;">&#41;</span>
grammaticDocument.<span style="color: #0000FF;">Root</span> <span style="color: #008000;">=</span> phoneticRule
speechRecognitionEngine.<span style="color: #0000FF;">LoadGrammar</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">New</span> Recognition.<span style="color: #0000FF;">Grammar</span><span style="color: #000000;">&#40;</span>grammaticDocument<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>Now we can add an event handler at the end of the form loading function and make it point to a new routine. <br />
 Event handler</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #FF8000;">AddHandler</span> speechRecognitionEngine.<span style="color: #0000FF;">SpeechRecognized</span>, <span style="color: #804040;">AddressOf</span> speechRecognized</pre></div></div>

<p>New routine</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> speechRecognized<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Speech</span>.<span style="color: #0000FF;">Recognition</span>.<span style="color: #0000FF;">RecognitionEventArgs</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008080; font-style: italic;">'Futere code goes here</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

<p>Now we have our recognition working. You could add MsgBox(e.Result.Text) inside our function if you wanted to, run the application and when trying to say “Alpha” into the microphone. There should be a popup window with the text “Alpha”.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><strong>The logic and added coolness</strong></p>
<p><br class="spacer_" /></p>
<p>Now that we have fully working program which recognizes some words, it is time to move on. Yeah you could have it show a popup each time you say something, or even repeat it. But why do that, when we can do so much cooler things. Like replicate the classic nuclear verification process which Hollywood loves to use all the time. So we will continue by adding some new variables at the top of the program.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">'We will use this for password verification.</span>
<span style="color: #FF8000;">Private</span> position <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
<span style="color: #008080; font-style: italic;">'This will store the password (or passwords actually) in an array. Define your own at the end, seperated by spaces like mine.</span>
<span style="color: #FF8000;">Private</span> password<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Alpha Whiskey Kilo Lima Echo Tango Delta Charlie&quot;</span>, <span style="color: #808080;">&quot; &quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #008080; font-style: italic;">'This will be used to dertime if the user has succesfully authenticated.</span>
<span style="color: #FF8000;">Private</span> presidentRights <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span></pre></div></div>

<p>Next up, add these subs somewhere in your program.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> accessGranted<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
speechSynthesis.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Access granted. Welcome mister president.&quot;</span><span style="color: #000000;">&#41;</span>
presidentRights <span style="color: #008000;">=</span> <span style="color: #0600FF;">True</span>
position <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> accessDenied<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
speechSynthesis.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Access denied. Password prompt resetting.&quot;</span><span style="color: #000000;">&#41;</span>
presidentRights <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span>
position <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> <span style="color: #0600FF;">abort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
speechSynthesis.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Password prompt resetting.&quot;</span><span style="color: #000000;">&#41;</span>
presidentRights <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span>
position <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></div></div>

<p>And now the real funny part. Now we are going to validate the user password. I hope the comments makes it clear what it’s doing.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">If</span> presidentRights <span style="color: #FF8000;">Then</span>
	<span style="color: #008080; font-style: italic;">'If the user is authenticated as &quot;president&quot;, the program will listen for the two keywords.</span>
	<span style="color: #0600FF;">Select</span> <span style="color: #0600FF;">Case</span> e.<span style="color: #0000FF;">Result</span>.<span style="color: #0000FF;">Text</span>
		<span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;Initiate launch sequence&quot;</span>
			<span style="color: #008080; font-style: italic;">'Says something if the launch is initiated.</span>
			speechSynthesis.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Launch sequence initiated. Everything blows up!&quot;</span><span style="color: #000000;">&#41;</span>
			System.<span style="color: #0000FF;">Threading</span>.<span style="color: #0000FF;">Thread</span>.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2500</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0600FF;">abort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;Abort&quot;</span>
			<span style="color: #008080; font-style: italic;">'Says something else if the launch is aborted.</span>
			speechSynthesis.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Launch sequence aborted. We are saved.&quot;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0600FF;">abort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Select</span>
	presidentRights <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span>
<span style="color: #FF8000;">Else</span>
	<span style="color: #0600FF;">Select</span> <span style="color: #0600FF;">Case</span> e.<span style="color: #0000FF;">Result</span>.<span style="color: #0000FF;">Text</span>
		<span style="color: #008080; font-style: italic;">'If the user chooses to confirm it validates the password (real simple.)</span>
		<span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;Confirm&quot;</span>
			<span style="color: #0600FF;">If</span> position <span style="color: #008000;">=</span> password.<span style="color: #0000FF;">Length</span> <span style="color: #FF8000;">Then</span>
				<span style="color: #008080; font-style: italic;">'If OK, call accessGranted()</span>
				accessGranted<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #FF8000;">Else</span>
				<span style="color: #008080; font-style: italic;">'If wrong, call accessDenied()</span>
				accessDenied<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
		<span style="color: #0600FF;">Case</span> <span style="color: #808080;">&quot;Abort&quot;</span>
			<span style="color: #008080; font-style: italic;">'Calls abort in case user choose to abort.</span>
			<span style="color: #0600FF;">abort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0600FF;">Case</span> password<span style="color: #000000;">&#40;</span>position<span style="color: #000000;">&#41;</span>
			<span style="color: #008080; font-style: italic;">'Checks password. If right, the counter is increment.</span>
			position <span style="color: #008000;">+=</span> <span style="color: #FF0000;">1</span>
		<span style="color: #0600FF;">Case</span> <span style="color: #FF8000;">Else</span>
			<span style="color: #008080; font-style: italic;">'If anything else (like wrong password) the position is reset to 0.</span>
			position <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
	<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Select</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></pre></div></div>

<p>There you have it. A complete replica of the popular atomic launch system in the movies.</p>
<p><br class="spacer_" /></p>
<p><strong>Demonstration</strong></p>
<p><p>This is my first screencast, but I think it came out pretty good…</p>
</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><iframe src="http://player.vimeo.com/video/17978859" width="400" height="300" frameborder="0"></iframe>
<p><a href="http://vimeo.com/17978859">Launching a nuclear war with VB.NET</a> from <a href="http://vimeo.com/hsp">Henrik Pedersen</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><b>Download</b></p>
<p>Lazy people can feed of my work here, even without copy pasting ;) &#8211; <a class="downloadlink" href="http://hsp.dk/download/NuclearControlCenter.zip" title=" downloaded 61 times" >SpokenPassword (61)</a></p>
<p><br class="spacer_" /></p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2010/12/launching-a-nuclear-war-with-vb-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A little class for speech synthesis</title>
		<link>http://hsp.dk/2010/12/a-little-class-for-speech-synthesis/</link>
		<comments>http://hsp.dk/2010/12/a-little-class-for-speech-synthesis/#comments</comments>
		<pubDate>Sat, 18 Dec 2010 01:22:43 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=62</guid>
		<description><![CDATA[Sorry for no posts the last couple of days. I’ve been busy doing all kinds of weird things ;) I just wanted to share this very simple little class I just made for using the Speech API built into Windows. It is already fairly simple very simple to use, but I’m the kind of guy [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for no posts the last couple of days. I’ve been busy doing all kinds of weird things ;)</p>
<p>I just wanted to share this very simple little class I just made for using the Speech API built into Windows.</p>
<p>It is already fairly simple very simple to use, but I’m the kind of guy who likes everything to be my way. So I wrote this to “decrapify” the API so I just have two simple properties and some simple functions / subs to access it.</p>
<p>Enjoy! :)</p>
<p><br class="spacer_" /></p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Speech</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Class</span> SpeechSynthesis
<span style="color: #FF8000;">Private</span> voiceSynthesis <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> Synthesis.<span style="color: #0000FF;">SpeechSynthesizer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">Private</span> voiceRate <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">2</span>
<span style="color: #FF8000;">Private</span> voiceVolume <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">75</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Used while initializing this object. Called automaticly when making a new instance like: Dim o as new SpeechSenthesis().</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> <span style="color: #FF8000;">New</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
voiceSynthesis.<span style="color: #0600FF;">Volume</span> <span style="color: #008000;">=</span> voiceVolume
voiceSynthesis.<span style="color: #0600FF;">Rate</span> <span style="color: #008000;">=</span> voiceRate
voiceSynthesis.<span style="color: #0000FF;">SelectVoice</span><span style="color: #000000;">&#40;</span>voiceSynthesis.<span style="color: #0000FF;">GetInstalledVoices</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">VoiceInfo</span>.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Used to start speaking a string asynchronously.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' &lt;span&gt; &lt;/span&gt;The text to speak as a string.</span>
<span style="color: #008080; font-style: italic;">''' A boolean value representing whatever to cancel other speaking instances of this object or throwing a</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> speakAsynchronously<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> text <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">Optional</span> <span style="color: #FF8000;">ByVal</span> forceSpeak <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> forceSpeak <span style="color: #FF8000;">Then</span>
<span style="color: #0600FF;">If</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Ready</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">SpeakAsync</span><span style="color: #000000;">&#40;</span>text<span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">ElseIf</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Speaking</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">SpeakAsyncCancelAll</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
voiceSynthesis.<span style="color: #0000FF;">SpeakAsync</span><span style="color: #000000;">&#40;</span>text<span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #FF8000;">Else</span>
<span style="color: #0600FF;">If</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Ready</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">SpeakAsync</span><span style="color: #000000;">&#40;</span>text<span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">Else</span>
<span style="color: #FF8000;">Throw</span> <span style="color: #FF8000;">New</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Already speaking.&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Used to start speaking a string synchronously.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' The text to speak as a string.</span>
<span style="color: #008080; font-style: italic;">''' A boolean value representing whatever to cancel other speaking instances of this object or throwing a</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> speakSynchronously<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> text <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>, <span style="color: #FF8000;">Optional</span> <span style="color: #FF8000;">ByVal</span> forceSpeak <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Boolean</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> forceSpeak <span style="color: #FF8000;">Then</span>
<span style="color: #0600FF;">If</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Ready</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span>text<span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">ElseIf</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Speaking</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">SpeakAsyncCancelAll</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
voiceSynthesis.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span>text<span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #FF8000;">Else</span>
<span style="color: #0600FF;">If</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Ready</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">SpeakAsync</span><span style="color: #000000;">&#40;</span>text<span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">Else</span>
<span style="color: #FF8000;">Throw</span> <span style="color: #FF8000;">New</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Already speaking.&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Stops the current asyncronously speaking from this object.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> stopSpeakingAsynchronously<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Speaking</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">SpeakAsyncCancelAll</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Pauses the currentasyncronously speaking from this object.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> pauseSpeaking<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Speaking</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">Pause</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Resumes speaking.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> resumeSpeaking<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> voiceSynthesis.<span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> Synthesis.<span style="color: #0000FF;">SynthesizerState</span>.<span style="color: #0000FF;">Paused</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #FF8000;">Resume</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Sets the volume of this object. Ranging from 0 - 100. Standard is 75.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> <span style="color: #0600FF;">volume</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
<span style="color: #FF8000;">Get</span>
<span style="color: #FF8000;">Return</span> voiceVolume
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Get</span>
<span style="color: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> value <span style="color: #008000;">&amp;</span>gt; <span style="color: #FF0000;">100</span> <span style="color: #FF8000;">Then</span>
voiceVolume <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span>
<span style="color: #FF8000;">ElseIf</span> value <span style="color: #008000;">&amp;</span>lt; <span style="color: #FF0000;">0</span> <span style="color: #FF8000;">Then</span>
voiceVolume <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
<span style="color: #FF8000;">Else</span>
voiceVolume <span style="color: #008000;">=</span> value
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
voiceSynthesis.<span style="color: #0600FF;">Volume</span> <span style="color: #008000;">=</span> voiceVolume
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Set</span>
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Property</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Used to define the voicerate of this object. Ranging from -10 to 10. Standard is -2.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Property</span> <span style="color: #0600FF;">rate</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
<span style="color: #FF8000;">Get</span>
<span style="color: #FF8000;">Return</span> voiceRate
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Get</span>
<span style="color: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> value <span style="color: #008000;">&amp;</span>gt; <span style="color: #FF0000;">10</span> <span style="color: #FF8000;">Then</span>
voiceRate <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span>
<span style="color: #FF8000;">ElseIf</span> value <span style="color: #008000;">&amp;</span>lt; <span style="color: #008000;">-</span><span style="color: #FF0000;">10</span> <span style="color: #FF8000;">Then</span>
voiceRate <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">10</span>
<span style="color: #FF8000;">Else</span>
voiceRate <span style="color: #008000;">=</span> value
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
voiceSynthesis.<span style="color: #0600FF;">Rate</span> <span style="color: #008000;">=</span> voiceRate
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Set</span>
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Property</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Get's a list of available voices as an array, when you don't need all the BS ;)</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> getAvailableVoiceNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #0600FF;">Array</span>
<span style="color: #0600FF;">Dim</span> voiceList<span style="color: #000000;">&#40;</span>voiceSynthesis.<span style="color: #0000FF;">GetInstalledVoices</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>
<span style="color: #0600FF;">Dim</span> counter <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
<span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> voice <span style="color: #FF8000;">As</span> Speech.<span style="color: #0000FF;">Synthesis</span>.<span style="color: #0000FF;">InstalledVoice</span> In voiceSynthesis.<span style="color: #0000FF;">GetInstalledVoices</span>
voiceList<span style="color: #000000;">&#40;</span>counter<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> voice.<span style="color: #0000FF;">VoiceInfo</span>.<span style="color: #0000FF;">Name</span>
counter <span style="color: #008000;">+=</span> <span style="color: #FF0000;">1</span>
<span style="color: #FF8000;">Next</span>
<span style="color: #FF8000;">Return</span> voiceList
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Get's a list of available voices as a list of VoiceInfo when you need everything.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> getAvailableVoices<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> List<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Of</span> Synthesis.<span style="color: #0000FF;">VoiceInfo</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">Dim</span> voiceList <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> List<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Of</span> Synthesis.<span style="color: #0000FF;">VoiceInfo</span><span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> voice <span style="color: #FF8000;">As</span> Speech.<span style="color: #0000FF;">Synthesis</span>.<span style="color: #0000FF;">InstalledVoice</span> In voiceSynthesis.<span style="color: #0000FF;">GetInstalledVoices</span>
voiceList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>voice.<span style="color: #0000FF;">VoiceInfo</span><span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">Next</span>
<span style="color: #FF8000;">Return</span> voiceList
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">''' Set's the voice based on a name. Standard when creating this object is just the first one available.</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #008080; font-style: italic;">'''</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Sub</span> setVoiceByName<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> name <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> voice <span style="color: #FF8000;">As</span> Speech.<span style="color: #0000FF;">Synthesis</span>.<span style="color: #0000FF;">InstalledVoice</span> In voiceSynthesis.<span style="color: #0000FF;">GetInstalledVoices</span>
<span style="color: #0600FF;">If</span> voice.<span style="color: #0000FF;">VoiceInfo</span>.<span style="color: #0000FF;">Name</span>.<span style="color: #0600FF;">Trim</span> <span style="color: #008000;">=</span> name.<span style="color: #0600FF;">Trim</span> <span style="color: #FF8000;">Then</span>
voiceSynthesis.<span style="color: #0000FF;">SelectVoice</span><span style="color: #000000;">&#40;</span>name.<span style="color: #0600FF;">Trim</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">Exit</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #FF8000;">Next</span>
<span style="color: #FF8000;">Throw</span> <span style="color: #FF8000;">New</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;The voice isn't installed on this computer.&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Class</span></pre></div></div>

<p>Just put this inside a new class called SpeechSynthesis.vb and begin using it. Everything should be pretty self exploratory, especially with the added XML documentation (okay overkill but I was bored).</p>
<p>Oh and by the way. You will need to add a reference to system.speech like this:</p>
<p><a href="http://hsp.dk/wp-content/uploads/2010/11/1Speech.jpg"><img class="alignleft size-full wp-image-64" title="1Speech" src="http://hsp.dk/wp-content/uploads/2010/11/1Speech.jpg" alt="" width="300" height="274" /></a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><a href="http://hsp.dk/wp-content/uploads/2010/11/2speech.jpg"><img class="alignleft size-full wp-image-65" title="2speech" src="http://hsp.dk/wp-content/uploads/2010/11/2speech.jpg" alt="" width="300" height="252" /></a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>So there you have it! And for the lazy people I have a download as always:<a class="downloadlink" href="http://hsp.dk/download/SimpleSpeechSynthesis.zip" title=" downloaded 24 times" >Speech Decrapifyer class (24)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2010/12/a-little-class-for-speech-synthesis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino relay circuit</title>
		<link>http://hsp.dk/2010/12/arduino-relay-circuit/</link>
		<comments>http://hsp.dk/2010/12/arduino-relay-circuit/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 01:11:19 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=57</guid>
		<description><![CDATA[This time, I’m following up on a request from a reader. (Alway nice to hear someone other than your mom and da… No wait they don’t read this either!..) Joke aside. I got a nice little mail asking me if I could teach how to interface an Arduino with 220v. Both yes, and no. Today [...]]]></description>
			<content:encoded><![CDATA[<p>This time, I’m following up on a request from a reader.</p>
<p>(Alway nice to hear someone other than your mom and da… No wait they don’t read this either!..) Joke aside.</p>
<p>I got a nice little mail asking me if I could teach how to interface an Arduino with 220v. Both yes, and no. Today I will teach you how to interface it with a relay, and it will work fine with 220v. But be aware, unless you are extremely good with electronics, I don’t recommend anyone to try this with 220v (or 120v.)</p>
<p>But if you still want to try it out with “real electricity” when go get yourself a nice little “power sawing outlet” with a usb port. Those wonderful little things, contains a 5v relay with a complete driver circuit. So you can just cut the usb of and provide 5+ plus and ground on the red and black cable. But that extends beyond this small tutorial.</p>
<p><strong>What you will need</strong></p>
<ul>
<li>1x BC547 transistor. Better buy 10 or 50 a time (cheaper, and you a likely to burn some).</li>
<li>1x 5v relay. Again, just buy a couple. It’s cheaper that way, and it’s good to have spare parts. </li>
<li>1x 10K ohm resistor. You should know by now. </li>
<li>1x Protection diode.</li>
<li>1x Arduino board.</li>
<li>Optional 1x Led, Jumper wires, breadboard.</li>
</ul>
<p><strong>A pretty little hand drawn shematic</strong></p>
<p><a href="http://hsp.dk/wp-content/uploads/2011/02/IMAG0172.jpg"><img class="alignleft size-medium wp-image-58" title="SchematicForArduinoRelay" src="http://hsp.dk/wp-content/uploads/2011/02/IMAG0172-300x179.jpg" alt="" width="300" height="179" /></a></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>This circuit should be pretty self exploratory. The Bc547 act’s as a switch, connected with one leg to ground on the Arduino, the middle leg to a 10K ohm resistor, and when to Arduino pin 12, and the last leg to the relay. (If you have the curved side against you, the left leg is to ground, middle to resistor/pin 12, and right to relay.) Here is the datasheet. And yes the BC547 only provide you with upto 100 milliamp. But it doesn’t matter with this relay, and many others work just fine too.</p>
<p>The other side of the relay is connected directly to +5v. And now an important part!. A protection diode. What is that? It is a diode put in reverse over the coil on a relay, to prevent “spikes” coming out of the relay when the coil is released. Normally it will not conduct any electricity, but in case of a voltage spike, it will shorten out the spike before it reaches the BC547 or anything else which might die from the impact. In my video at the end, I have no protection diode so I’m really playing with fire. But I was just to lazy, don’t be an idiot like me, and use a diode! ;)</p>
<p><br class="spacer_" /></p>
<p><strong>The programming</strong></p>
<p>This is kinda like a bad joke. You don’t need anything here other than the code to pulse (blink) pin 12. Just in case, and to provide material for an upcoming flame war, I’m going to give you the code here.</p>
<p><br class="spacer_" /></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> setup<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
pinMode<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">12</span>, OUTPUT<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> loop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
digitalWrite<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">12</span>, HIGH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
digitalWrite<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">12</span>, LOW<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
delay<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="spacer_" /></p>
<p><b>Finally a very bad little demonstration</b></p>
<p><br class="spacer_" /></p>
<p><iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/CoK88EwgjA8" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2010/12/arduino-relay-circuit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An approach to generating primes</title>
		<link>http://hsp.dk/2010/12/an-approach-to-generating-primes/</link>
		<comments>http://hsp.dk/2010/12/an-approach-to-generating-primes/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 23:14:51 +0000</pubDate>
		<dc:creator>hspsoftware</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://hsp.dk/?p=48</guid>
		<description><![CDATA[Hello once again, this time I’m back with a new kind of tutorial. I will show you how to generate prime numbers with PHP. A basic knowledge of PHP will definitely help you a lot, but else it should be easy to follow. What is a prime number? A prime number is a number which [...]]]></description>
			<content:encoded><![CDATA[<p>Hello once again, this time I’m back with a new kind of tutorial. I will show you how to generate prime numbers with PHP. A basic knowledge of PHP will definitely help you a lot, but else it should be easy to follow.</p>
<p><br class="spacer_" /></p>
<p><strong>What is a prime number?</strong></p>
<p>A prime number is a number which only divides cleanly (without decimals) with 1 and the number itself. For example, 2, 3, 5, 7, and 11 is all prime numbers. They only divide by them self and by 1.</p>
<p>But what can we use those special numbers for? In fact it’s those special numbers, who keeps all of our valuable information secret. Prime numbers is widely used in all sorts of encryption because there is no definitive way to easily calculate large primes. No your PayPal account is not protected by the number 11, but more likely with a 30 digit number which would take a hell lot of computing time to calculate.</p>
<p><em>No, this tutorial has no relation to Optimus prime from Transformers ;)</em></p>
<p><em><br />
 </em></p>
<p><strong> How do we make PHP talk primes?</strong></p>
<p><strong> </strong>There is a lot of different ways to do that, but for now we will just use a simple way called Trail Division. In trail division, try and divide every number from 2 up to the number you wish to test minus one. I any of the numbers divide cleanly, you can tell it’s not a prime. And if no one divides, it is a prime. So simple is that. But let’s take an example just to clarify.</p>
<p><br class="spacer_" /></p>
<p>We take the number 11. Now we take the square root of eleven which results in the long number:</p>
<p>3,3166247903553998491149327366707</p>
<p>Now we try and divide this number by 2.</p>
<p>3,3166247903553998491149327366707 / 2 = 1,6583123951776999245574663683354</p>
<p>No clean division. Now we try dividing by 3.</p>
<p>3,3166247903553998491149327366707 / 3 = 1,1055415967851332830383109122236</p>
<p>Still no clean division. This continues all the way up to 12.</p>
<p>Now we know that 11 is a prime number, because nothing besides 1 and 11 divides cleanly.</p>
<p><br class="spacer_" /></p>
<p>But wait? Why did I take the square root of 11 before I started dividing? This is just a simple trick to minimize the calculations. It doesn’t really matter here while only trying to test such small numbers. But what if we try and calculate all primes up to a million? Trust me, this will drastically decrease the computation time needed.</p>
<p><br class="spacer_" /></p>
<p><b>Let’s move on to the actual coding work.</b></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>PHP Prime generator<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;index.php&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>b<span style="color: #339933;">&gt;</span>Please provide a positive integer <span style="color: #b1b100;">as</span> an upper limit <span style="color: #b1b100;">for</span> the prime generation<span style="color: #339933;">.&lt;/</span>b<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;intUpTo&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Generate!&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Begin by saving this file as index.php on your webserver. It is just a basic draft for the rest of our coding work,<br />
containing a form and a basic HTML structure.</p>
<p>Now we can start coding the actual PHP. The first thing we need to do is checking if something is submitted to our<br />
script, and when preparing the submitted content for processing. That’s is easy using the PHP isset() function and the<br />
$_POST[] variable.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PHP Prime generator&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;
&lt;b&gt;Please provide a positive integer as an upper limit for the prime generation.&lt;/b&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; name=&quot;intUpTo&quot; value=&quot;100&quot; /&gt;
&lt;br /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Generate!&quot; /&gt;
&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;Primes from 2 to &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Inside our new conditional statement, we can now make a so called“for loop”, going form 2 to the maximum number<br />
inputted by the user. We will also declare the variable boolIsPrime (as true) right at the beginning of our newly created<br />
loop. This will serve to check if the number being processed is actual a prime later on in our code.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PHP Prime generator&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;
&lt;b&gt;Please provide a positive integer as an upper limit for the prime generation.&lt;/b&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; name=&quot;intUpTo&quot; value=&quot;100&quot; /&gt;
&lt;br /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Generate!&quot; /&gt;
&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;Primes from 2 to &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$intUpTo</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$boolIsPrime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Now it really get’s tough! Inside the previous loop, we will now create a new loop. This will go from 2 up to the<br />
square root of the current position in the upper loop. Take a look for your self!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PHP Prime generator&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;
&lt;b&gt;Please provide a positive integer as an upper limit for the prime generation.&lt;/b&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; name=&quot;intUpTo&quot; value=&quot;100&quot; /&gt;
&lt;br /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Generate!&quot; /&gt;
&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;Primes from 2 to &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$intUpTo</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$boolIsPrime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">&lt;=</span><span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Still following along? I hope so! ;) Because now we will use the arithmetic operator “modulus”. This is a wonderful<br />
friend to have while doing all kinds of mathematical work in PHP. If you don’t know it, it works like 7 % 2 (% is<br />
modulus). This will return 1. Why? Because modulus is remainder after division. 7 / 2 = 3,5. 6 / 2 = 3. 7 – 6 = 1. So<br />
the remainder is 1.</p>
<p>We will use this operator to check if two numbers divide cleanly. If we take 10 % 5. It will return 0, because 10 / 5 =<br />
2. So these two numbers divide cleanly. So we can just check inside our latest loop if the current position in the outer<br />
loop is cleanly dividable by the position in the inner loop. And if it is, we will assign false to the boolIsPrime variable.<br />
And we will also use “break” to exit the inner loop, as we don’t need to test all the other numbers as it would not be a<br />
prime anyway.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PHP Prime generator&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;
&lt;b&gt;Please provide a positive integer as an upper limit for the prime generation.&lt;/b&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; name=&quot;intUpTo&quot; value=&quot;100&quot; /&gt;
&lt;br /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Generate!&quot; /&gt;
&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;Primes from 2 to &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$intUpTo</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$boolIsPrime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">&lt;=</span><span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$e</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$boolIsPrime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Now we only need to add a conditional statement after the inner loop, to print out the position of the outer loop if<br />
boolIsPrime is true. Now the code looks like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PHP Prime generator&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;
&lt;b&gt;Please provide a positive integer as an upper limit for the prime generation.&lt;/b&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; name=&quot;intUpTo&quot; value=&quot;100&quot; /&gt;
&lt;br /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Generate!&quot; /&gt;
&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;Primes from 2 to &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$intUpTo</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$boolIsPrime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">&lt;=</span><span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$e</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$boolIsPrime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$boolIsPrime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; is a prime.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>There you have it. A full PHP prime generator. Oh and as a little bonus. If you experience problems with the script<br />
timing out while calculating larger values, you could try and add set_time_out(120) to the top like so, to change the<br />
script time limit to 120 seconds (2 minutes). You can also set it to 0 for infinite. But be careful. This will put an<br />
enormous stress onto your server.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">120</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PHP Prime generator&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;
&lt;b&gt;Please provide a positive integer as an upper limit for the prime generation.&lt;/b&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; name=&quot;intUpTo&quot; value=&quot;100&quot; /&gt;
&lt;br /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Generate!&quot; /&gt;
&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'intUpTo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;b&gt;Primes from 2 to &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$intUpTo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$intUpTo</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$boolIsPrime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">&lt;=</span><span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$e</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$boolIsPrime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$boolIsPrime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; is a prime.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>You can see a demo output <a href="http://hsp.dk/wp-content/uploads/2011/02/phpPrimeGeneratorOutput.txt">here</a>, generated in just under 0,5 seconds or so on a 1,8 GHz machine, with all primes up<br />
to 1000. Demo output . Or you can just grab the complete source if you are lazy.<a class="downloadlink" href="http://hsp.dk/download/PHPPrimeGenerator.zip" title=" downloaded 32 times" >PHP Prime Generator (32)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hsp.dk/2010/12/an-approach-to-generating-primes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

