<?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>Wyatt Says... &#187; Programming</title>
	<atom:link href="http://wyday.com/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://wyday.com/blog</link>
	<description>Design articles to make your users deliriously happy</description>
	<lastBuildDate>Sun, 06 May 2012 17:39:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to debug a Windows Service written in C# or VB.NET</title>
		<link>http://wyday.com/blog/2012/how-to-debug-a-windows-service-in-csharp-or-vb-net/</link>
		<comments>http://wyday.com/blog/2012/how-to-debug-a-windows-service-in-csharp-or-vb-net/#comments</comments>
		<pubDate>Sun, 06 May 2012 17:39:31 +0000</pubDate>
		<dc:creator>Wyatt O&#39;Day</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://wyday.com/blog/?p=157</guid>
		<description><![CDATA[How to debug a Windows Service like you would debug a Windows Forms app.]]></description>
			<content:encoded><![CDATA[<p><img alt="Windows Service" src="http://img.wyday.com/blog/images/2012/debug-service/service-icon.png" class="alignright" width="160" height="148" />Over the last couple of months I&#8217;ve gotten a surprising number of questions asking about the same thing: how to debug a Windows Service like you would debug a normal Window Forms application. There are lots of hacks to pseudo-debug a Windows service like running your code from a console, or even adding a massive amount of logging throughout your code.</p>
<p>But what if you need to run your Windows Service as it normally runs (i.e. as an actual service) in order to track down bugs?</p>
<p>The good news is that&#8217;s there&#8217;s a little snippet of code to make debugging Windows Services a snap. The first thing to do is to add the following <code>DebugMode()</code> function to your app:</p>
<p><strong>C#</strong></p>
<pre>
/// &lt;summary&gt;Helper function to attach a debugger to the running service.&lt;/summary&gt;
[Conditional("DEBUG")]
static void DebugMode()
{
    if (!Debugger.IsAttached)
        Debugger.Launch();

    Debugger.Break();
}
</pre>
<p><strong>Visual Basic .NET</strong></p>
<pre>
''' &lt;summary&gt;Helper function to attach a debugger to the running service.&lt;/summary&gt;
&lt;Conditional("DEBUG")&gt;
Shared Sub DebugMode()
    If Not Debugger.IsAttached Then
        Debugger.Launch()
    End If

    Debugger.Break()
End Sub
</pre>
<p>When you call the newly added <code>DebugMode()</code> function within your Windows Service, if there isn&#8217;t already a debugger attached to your service it gives you the option of adding a debugger:</p>
<p><img alt="Moneybookers a.k.a. Skrill" src="http://img.wyday.com/blog/images/2012/debug-service/vs-jit-debugger.png" class="aligncenter" width="440" height="475" /></p>
<p>Now you can debug your service like you would a Windows Forms application. Also, because you&#8217;re using the <strong><code>[Conditional("DEBUG")]</code></strong> attribute on the <code>DebugMode()</code> function when you compile your service in &#8220;Release&#8221; mode all the calls to the &#8220;<code>DebugMode()</code>&#8221; function will be removed.</p>
<p>Pretty cool, huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://wyday.com/blog/2012/how-to-debug-a-windows-service-in-csharp-or-vb-net/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to automate Moneybookers (Skrill) using status_url (IPN)</title>
		<link>http://wyday.com/blog/2011/automate-moneybookers-skrill-using-status_url-ipn-php-asp-net/</link>
		<comments>http://wyday.com/blog/2011/automate-moneybookers-skrill-using-status_url-ipn-php-asp-net/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 12:14:55 +0000</pubDate>
		<dc:creator>Wyatt O&#39;Day</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[vb.net]]></category>

		<guid isPermaLink="false">http://wyday.com/blog/?p=149</guid>
		<description><![CDATA[How to use Moneybookers to automate digital delivery (product keys, eBooks, etc). PHP and ASP.NET code examples included.]]></description>
			<content:encoded><![CDATA[<p><img alt="Moneybookers a.k.a. Skrill" src="http://img.wyday.com/blog/images/2011/moneybookers/skrill-mb.png" class="alignright" width="160" height="148" /><a href="http://www.moneybookers.com/" rel="nofollow">Moneybookers</a> is quickly becoming a viable competitor to PayPal. And because of this our <a href="http://wyday.com/limelm/">LimeLM</a> customers have been asking for a completely automated payment form example similar to how our PayPal example works. That is, the customer enters the number of licenses they want, they click &#8220;Buy now&#8221;, they enter their info on the PayPal page, then they click &#8220;Order&#8221;, and a few seconds later the customer is magically sent their shiny new product key.</p>
<p>With PayPal this whole process is simple &#8212; just use the <a href="https://www.paypal.com/ipn" rel="nofollow">PayPal IPN (instant payment notification)</a>. That is, you specify an URL for PayPal to talk to once the order is complete. From there you can validate the order and take any action you like (e.g. sending product keys).</p>
<p>Moneybookers has a similar process except there are hardly any examples showing how to use it properly. And the example code that <em>is</em> on the web is riddled with <em>very serious</em> security vulnerabilities (SQL injections, failing to validate the order comes from Moneybookers, etc., etc.).</p>
<p>This article will show you how to use the Moneybookers equivalent to PayPal&#8217;s IPN. That is, you&#8217;ll be able to automatically generate product keys once a customer orders your software using Moneybookers. The code I post will be for both C# (ASP.NET) and PHP. If you would rather download a fully built payment form that lets you user switch between payment methods (credit card, bank transfer, PayPal, or Moneybookers), then you can <strong><a href="http://wyday.com/limelm/signup/">signup for LimeLM</a></strong> and <a href="http://wyday.com/limelm/api/">download the LimeLM Web API Pack</a>. Here&#8217;s a screenshot of what payment form example looks like:</p>
<p><img alt="Payment example included in the LimeLM Web API Pack" src="http://img.wyday.com/blog/images/2011/moneybookers/payment.png" class="aligncenter" width="670" height="546" /></p>
<p>Or, you can read on for the example that&#8217;s not specific to LimeLM.</p>
<h3>Step 1. Signup for a Moneybookers test account</h3>
<p>The first thing you need to do is get a test &#8220;merchant&#8221; account from Moneybookers. Unfortunately Moneybookers still doesn&#8217;t have a streamlined process for creating a test account, and their support staff is rather surly, so follow the instructions carefully:</p>
<ol>
<li><a href="https://www.moneybookers.com/app/register.pl" rel="nofollow">Sign up for a Moneybookers &#8220;Business account&#8221;</a>. <strong>Do not use the same email you&#8217;ll be using for your real account.</strong></li>
<li><a href="https://www.moneybookers.com/app/register.pl" rel="nofollow">Sign up for a Moneybookers &#8220;Personal account&#8221;</a>. Use a different email than the &#8220;Business account&#8221;. <strong>Do not use the same email you&#8217;ll be using for your real account.</strong></li>
<li><a href="https://www.moneybookers.com/app/help.pl?s=contact" rel="nofollow">Contact Moneybookers and ask them to convert these 2 accounts to test accounts</a>. It doesn&#8217;t need to be a long email. Just something simple like:<br />
<blockquote>
<p>We need you to turn 2 accounts to test accounts:</p>
<p>&#8220;Buyer account&#8221;:<br/>Email: buyer-email@example.com<br/>Customer ID: 12456789</p>
<p>&#8220;Merchant account&#8221;:<br/>Email: merchant-email@example.com<br/>Customer ID: 98765421</p>
</blockquote>
</li>
</ol>
<h3>Step 2. Create a &#8220;secret word&#8221;</h3>
<p>In your new merchant account you&#8217;ll need to create a &#8220;Secret Word&#8221; on the &#8220;Merchant tools&#8221; page:</p>
<p><img alt="Creating a Moneybookers secret word" src="http://img.wyday.com/blog/images/2011/moneybookers/mb-secret.png" class="aligncenter" width="400" height="264" /></p>
<p>Now that you&#8217;ve created your test Moneybookers accounts and set your secret word you&#8217;re ready to add the payment form to your website.</p>
<h3>Step 3 (option 1). Use our pre-built payment form</h3>
<p>If you&#8217;re using <a href="http://wyday.com/limelm/">LimeLM</a> you can use our pre-built payment form. Just configure a few settings, add the payment form to your site, and you&#8217;re ready to go. See the &#8220;<a href="http://wyday.com/limelm/help/automate-license-generation-with-skrill-moneybookers/">Automate license generation with Skrill (a.k.a. Moneybookers)</a>&#8221; article.</p>
<h3>Step 3 (option 2). Create your own payment form</h3>
<p>If you don&#8217;t want to use the pre-built payment example in the LimeLM API Pack, or you&#8217;re not using C#, VB.NET, or PHP, then you can still automate your orders with Moneybookers. The first step is to add the Moneybookers payment form to your website:</p>
<pre>
&lt;form action="https://www.moneybookers.com/app/payment.pl" method="post"&gt;
  &lt;input type="hidden" name="pay_to_email" value="<strong>merchant-email@example.com</strong>"/&gt;
  &lt;input type="hidden" name="status_url" value="<strong>http://example.com/verify.cgi</strong>"/&gt;
  &lt;input type="hidden" name="language" value="EN"/&gt;
  &lt;input type="hidden" name="amount" value="<strong>Total amount (e.g. 39.60)</strong>"/&gt;
  &lt;input type="hidden" name="currency" value="<strong>Currency code (e.g. USD)</strong>"/&gt;
  &lt;input type="hidden" name="detail1_description" value="<strong>YourApp</strong>"/&gt;
  &lt;input type="hidden" name="detail1_text" value="<strong>License</strong>"/&gt;
  &lt;input type="submit" value="Pay!"/&gt;
&lt;/form&gt;
</pre>
<p>Change the &#8220;status_url&#8221; field to point to your script that will verify and generate the product keys and change the &#8220;pay_to_email&#8221; field to the test &#8220;merchant email&#8221; you created earlier. Then configure the price, currency code, and product name.</p>
<h3>Optionally set your logo</h3>
<p>You can customize the Moneybookers payment screen with your own logo. The logo must be hosted on a secure site &mdash; that is, the link must start with <strong>https://</strong> not <strong>http://</strong>. Also, the logo must be at most 200px wide and 50px tall. If you have a logo that meets those requirements then add a &#8220;logo_url&#8221; field to your form. For example:</p>
<pre>&lt;input type="hidden" name="logo_url" value="<strong>https://example.com/logo.png</strong>"/&gt;</pre>
<p>This is an example showing what the wyDay logo looks like on the Moneybookers checkout page:</p>
<p><img alt="wyDay logo as seen on a Moneybookers checkout screen" src="http://img.wyday.com/blog/images/2011/moneybookers/mb-logo.png" class="aligncenter" width="464" height="116" /></p>
<h3>Step 4. Verify the Moneybookers order</h3>
<p>After a customer has completed their order through Moneybookers, Moneybookers will contact the script you provided in the &#8220;status_url&#8221; argument (e.g. &#8220;http://example.com/verify.cgi&#8221;). Moneybookers will POST the order information to your script and it&#8217;s up to you to verify that it&#8217;s a valid order and not just some hacker trying to get free product key from you.</p>
<p>Luckily Moneybookers gives the prescribed method for verifying orders in their <a href="http://www.moneybookers.com/app/help.pl?s=m_manual" rel="nofollow">gateway integration manual</a>. Quoting from their manual:</p>
<blockquote><p>
A hidden text field called md5sig is included in the form submitted to the Merchant&#8217;s server. The value of this field is a 128 bit message digest, expressed as a string of thirty-two hexadecimal digits in UPPERCASE. The md5sig is constructed by performing an MD5 calculation on a string built up by concatenating the other fields returned to the status_url. Specifically the MD5 hash is a concatenation of the following fields:</p>
<ul>
<li>merchant_id</li>
<li>transaction_id</li>
<li>the uppercase MD5 value of the ASCII equivalent of the secret word submitted in the &#8220;Merchant Tools&#8221; section of the Merchant&#8217;s online Moneybookers account.</li>
<li>mb_amount</li>
<li>mb_currency</li>
<li>status</li>
</ul>
</blockquote>
<h4>PHP example code</h4>
<p>In PHP this looks like (taken from <a href="http://wyday.com/limelm/help/how-to-generate-product-keys-after-order/#paychecker">paychecker.php</a>):</p>
<pre>
// Validate the Moneybookers signature
$concatFields = $_POST['merchant_id']
    .$_POST['transaction_id']
    .strtoupper(md5('<strong>Paste your secret word here</strong>'))
    .$_POST['mb_amount']
    .$_POST['mb_currency']
    .$_POST['status'];

$MBEmail = '<strong>merchant-email@example.com</strong>';

// Ensure the signature is valid, the status code == 2,
// and that the money is going to you
if (strtoupper(md5($concatFields)) == $_POST['md5sig']
    &#038;&#038; $_POST['status'] == 2
    &#038;&#038; $_POST['pay_to_email'] == $MBEmail)
{
    // Valid transaction.

    //TODO: <a href="http://wyday.com/limelm/help/api/limelm.pkey.generate/">generate the product keys</a> and
    //      send them to your customer.
}
else
{
    // Invalid transaction. Bail out
    exit;
}
</pre>
<h4>C# (ASP.NET) example code</h4>
<p>In C# (for ASP.NET) this involves a bit more work. First create a simple helper function that creates the uppercase MD5 hash of a string:</p>
<pre>
static string StringToMD5(string str)
{
    MD5CryptoServiceProvider cryptHandler = new MD5CryptoServiceProvider();
    byte[] ba = cryptHandler.ComputeHash(Encoding.UTF8.GetBytes(str));

    StringBuilder hex = new StringBuilder(ba.Length * 2);

    foreach (byte b in ba)
        hex.AppendFormat("{0:X2}", b);

    return hex.ToString();
}
</pre>
<p>Then, the validation code will look something like this:</p>
<pre>
// Validate the Moneybookers signature
string concatFields = Request.Form["merchant_id"]
    + Request.Form["transaction_id"]
    + StringToMD5("<strong>Paste your secret word here</strong>")
    + Request.Form["mb_amount"]
    + Request.Form["mb_currency"]
    + Request.Form["status"];

string MBEmail = "<strong>merchant-email@example.com</strong>";

// Ensure the signature is valid, the status code == 2,
// and that the money is going to you
if (Request.Form["md5sig"] == StringToMD5(concatFields)
    &#038;&#038; Request.Form["status"] == "2"
    &#038;&#038; Request.Form["pay_to_email"] == MBEmail)
{
    // Valid transaction.

    //TODO: <a href="http://wyday.com/limelm/help/api/limelm.pkey.generate/">generate the product keys</a> and
    //      send them to your customer.
}
else
{
    // Invalid transaction. Bail out
    return;
}
</pre>
<h3>Step 5. Further verification, generating product keys, etc.</h3>
<p>There&#8217;s one further step of verification I didn&#8217;t talk about: making sure the customer paid the correct amount. That is, verifying the &#8220;mb_amount&#8221; field is correct. For instance you don&#8217;t want to send a user a product key if they only pay 1 penny instead of the full amount. Also, you can further extend the payment form and the verification code to handle quantity. But this is a bit beyond the scope of the article. If you want to see that in action then download the example payment form included in the <a href="http://wyday.com/limelm/api/">LimeLM Web API Pack</a>.</p>
<p>Once you&#8217;ve verified the order you can use the <a href="http://wyday.com/limelm/help/api/limelm.pkey.generate/">limelm.pkey.generate</a> web API function to generate product keys and email them to your customer.</p>
<h3>Step 6. Test the payment form</h3>
<p>Now that you have everything configured you&#8217;re ready to test your Moneybookers payment. Run through the complete payment process to see everything works how you expect it to work.</p>
<h3>Step 7. Create a real Moneybookers account, change the setting</h3>
<p>After you&#8217;ve finished testing your payment process you&#8217;re ready to <a href="https://www.moneybookers.com/app/register.pl" rel="nofollow">sign up for a real Moneybookers &#8220;Business account&#8221;</a> and change the &#8220;pay_to_email&#8221; field to the email you used to create this account.</p>
<h2>Download full example code</h2>
<p>We have a fully built payment page for PHP and ASP.NET (for C# and VB.NET) included in the LimeLM Web API Pack (<strong><a href="http://wyday.com/limelm/api/">get it on your API page</a></strong>). If you haven&#8217;t already signed up for LimeLM then <a href="http://wyday.com/limelm/signup/">sign up now</a>. All plans have a 30-day free trial. Or, if you&#8217;re just putting your toes in the water, there&#8217;s even <a href="http://wyday.com/limelm/signup/">a free plan</a> that has no time limit and doesn&#8217;t require a credit card.</p>
]]></content:encoded>
			<wfw:commentRss>http://wyday.com/blog/2011/automate-moneybookers-skrill-using-status_url-ipn-php-asp-net/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to detect .NET 2.0 vs. .NET 4.0 assemblies, and x86 vs. x64 vs. AnyCPU</title>
		<link>http://wyday.com/blog/2010/how-to-detect-net-assemblies-x86-x64-anycpu/</link>
		<comments>http://wyday.com/blog/2010/how-to-detect-net-assemblies-x86-x64-anycpu/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 02:09:31 +0000</pubDate>
		<dc:creator>Wyatt O&#39;Day</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[wyBuild]]></category>
		<category><![CDATA[wyUpdate]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://wyday.com/blog/?p=145</guid>
		<description><![CDATA[How to detect .NET assemblies, their target platform (x86, x64, etc.), and other details.]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="http://img.wyday.com/blog/images/2010/how-to-detect-net-assemblies/net_logo.png" title=".NET Framework" class="alignright" width="150" height="143" />In today’s article I’m going to talk about an interesting problem: detecting .NET assemblies. More than that, I’ll be talking about detecting some features of .NET assemblies and how you can expand and mold our code for your own uses. The code’s at the bottom of the article, it’s written in C# and licensed under the BSD license. Go get it.</p>
<p>I’ve seen this question pop up in a few different forms:</p>
<ul>
<li>How do I detect .NET assemblies?</li>
<li>How can I detect the difference between .NET 2.0 and .NET 4.0 assemblies?</li>
<li>How can I detect the difference between x86, x64, and AnyCPU .NET assemblies?</li>
</ul>
<p>And the list goes on and on. But this raises the question…</p>
<h2>Why detect .NET assemblies?</h2>
<p>We detect .NET assemblies because we respect humans’ time. Let me explain.</p>
<p>When <a href="http://wyday.com/wyupdate/">wyUpdate (our open source updater)</a> installs updates it can do a few things beyond simple patching, registry changing, and file copying. Namely it can:</p>
<ul>
<li>NGEN assemblies</li>
<li>Install &#038; update COM assemblies using RegAsm</li>
<li>Install &#038; update assemblies in the GAC (Global Assembly Cache).</li>
</ul>
<p>Which means wyUpdate needs to know whether the executable (or dll) is a .NET assembly, whether it’s strong signed, and what platform target it is (i.e. x86, x64, or Any CPU).</p>
<p>We could ask the user for every file, but that’s such a hassle. Who wants to waste time checking boxes for every exe and dll in their project? Rather than wasting the users’ time we quickly scan the .dll and .exe files for their details when the update is built inside <a href="http://wyday.com/wybuild/">wyBuild</a>.</p>
<h2>How not to do .NET detection</h2>
<p>Do not use LoadLibrary(), or Assembly.Load() functions to load an assembly in memory to then parse it. This breaks when you have an x86 process trying to a load an x64 assembly (or vice versa).</p>
<p><img alt="" src="http://img.wyday.com/blog/images/2010/how-to-detect-net-assemblies/target.png" title="Platform target" class="aligncenter" width="349" height="102" /></p>
<h2>How to detect .NET</h2>
<p>Instead of using LoadLibrary (or one of its brethren) we’ll just treat the executables as dumb files. That is, just run a simple loop over the file and skip over the unneeded parts. You can check out the C# code posted at the bottom of this article, but you should be aware of 2 resources we used when designing the .NET detection algorithm:</p>
<ul>
<li>CLI Partition II: Metadata Definition and Semantics (<a href="http://download.microsoft.com/download/7/3/3/733AD403-90B2-4064-A81E-01035A7FE13C/MS%20Partition%20II.pdf">get the PDF</a>) from the <a href="http://msdn.microsoft.com/en-us/netframework/aa569283.aspx">ECMA C# and Common Language Infrastructure Standards</a></li>
<li><a href="http://www.microsoft.com/whdc/system/platform/firmware/pecoff.mspx">Portable Executable and Common Object File Format Specification</a> (i.e. the PECOFF Spec)</li>
</ul>
<p>The PECOFF spec gives you the general layout of .exe and .dll files, and the CLI Partition II gives .NET specific features that we detect. Namely, is the assembly strong signed, is it built for Any CPU or x86 alone, and what base version of the .NET framework is it built for (2.0 or 4.0).</p>
<p>Also, when you check out the code, notice how the code handles PE32 files versus how it handles PE32+ files. That is to say, 32-bit assemblies have a subtly different layout than 64-bit assemblies.</p>
<h2>Tell me if you find this useful – how are you using it?</h2>
<p>If you find this code useful, tell me how you’re using it in the comments.</p>
<h2>Get the C# source</h2>
<p><a href="http://wyupdate.googlecode.com/files/AssemblyParser-v2.zip">Download the AssemblyDetails C# source</a>. It works with .NET 2.0, 3.0, 3.5, 4.0.</p>
<p><strong>Example usage:</strong></p>
<pre>
AssemblyDetails ad = AssemblyDetails.FromFile(filename);

// ad == null for non .NET assemblies
if (ad != null)
    Console.WriteLine(Path.GetFileName(filename) + ": " + ad.CPUVersion + ", " + ad.FrameworkVersion);
else
    Console.WriteLine(Path.GetFileName(filename) + ": Not a .NET 2.0+ executable.");
</pre>
<p><strong>Update 7/3/2010: </strong> There was a slight bug in the first version. Re-download the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://wyday.com/blog/2010/how-to-detect-net-assemblies-x86-x64-anycpu/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
