Archive for the ‘Code’ Category

Free alternative to .NET Reflector

Wednesday, December 18th, 2013

ILSpy is free and open source.

Introduction to MFC

Thursday, February 7th, 2013

The Microsoft Foundation Class library is a framework for Windows GUIs in C++, based on the Win32 API C library.

To create a simple window, you need to create an app and a window.

The app class handles most of the behind the scenes stuff, including program entry. There must be one globally defined instance of the app for the program to run.

Here is a very basic MFC application. It just shows a blank window.

#include <afxwin.h>
 
 
class CHelloWorldFrame : public CFrameWnd
{
public:
	DECLARE_MESSAGE_MAP()
};
 
BEGIN_MESSAGE_MAP(CHelloWorldFrame, CFrameWnd)
END_MESSAGE_MAP()
 
 
 
class CHelloWorldApp : public CWinApp
{
public:
	CHelloWorldApp() : CWinApp(_T("App Name")) {}
 
	BOOL InitInstance()
	{
		CFrameWnd* frame = new CHelloWorldFrame();
		m_pMainWnd = frame;
		frame->Create(NULL, _T("Window Caption"));
		frame->ShowWindow(SW_SHOW);
		return TRUE;
	}
};
 
CHelloWorldApp theApp;

If you want to create a form with buttons, text boxes etc, you should probably look at CFormView. Alternatively, create a dialog-based application in Visual Studio rather than single document (SDI).

SQL Server random unique identifiers

Friday, May 4th, 2012

A common method of producing random unique identifiers in SQL server is by using a GUID field, calling newid() to generate the data. For the most part, this works because it’s 128 bits worth of random data, which means there is a very low probability of duplicate records for most databases.

However, it is also common to combine this with the checksum() function to reduce it to a 32 bit integer. This makes collisions much more likely, even in relatively small databases. For example, the GUIDs 28258F69-6536-4198-BE37-94960ABF054F and 49B60D4B-DC4A-4E18-825E-B4C99713D011 both checksum to 0xC3AD13D3. With a table of about 100,000 rows collisions will start to occur more frequently by the birthday paradox.

def P(x, n):
  return 1 - ((x-1)/x)**(n * (n-1)/2)
 
>>> P(2**32,77500)
0.503022489601693
 
>>> P(2**32,200000)
0.9905011979711653
 
>>> P(2**53,10000000)
0.005535735601501335
 
>>> P(2**53,100000000)
0.42599174817345076

Using this maths, we can see that using a 32 bit random number, the probability of getting at least one collision is 50% at around 77,500 rows and 99% at 200,000 rows. We can also see that if we increase this to a 53 bit number, 10 million rows gives a 0.55% chance of getting at least collision and 100 million rows gives a 42.5% chance of getting at least one collision, so 64 bit should be plenty.

For higher precision numbers, we can use mpmath

from mpmath import *
mp.dps=50
def P(x, n):
  return 1 - ((x-1)/x)**(n * (n-1)/2)
 
>>> print( P(mpf(2)**mpf(64), mpf(1000000000)) )
0.026741008873954667138505944308767166153477279783999
>>> print( P(mpf(2)**mpf(64), mpf(10000000000)) )
0.93349681454859684590636893705314709163709804436875

From here you can see that a 64 bit number has a 2.6% chance of getting a single collision in a 1 billion row table, and a 93% chance in a 10 billion row table.

A compromise of both is to simply truncate the GUID at 64 bits and optionally convert to a bigint.

SELECT CONVERT(BIGINT, CONVERT(BINARY(8), newid()))

If you leave it as binary and don’t need to convert to an integer type, this does not have to be 8 bytes. For example, you could have a 5 or a 10 byte code.

None of these are perfect but the probability of a collision decreases with more bits. If 128 bit is too long for you (e.g. to display to users) but 32 bit generates too many collisions, try a compromise such as 64 bit.

If you are consistent enough, you may even be able to store the original GUID and just display the truncated form, which could allow you to change the length displayed later without changing the probability of collisions. This is more flexible but may lead to confusion among users and consistency is required (differing lengths could lead to bugs).

Licensing your libraries

Tuesday, February 8th, 2011

For most spare time projects, licensing can be an afterthought. Personally, I hardly put licences on anything I write. Most of the time this is just because I expect people to use it anyway. I treat licences more as a restriction than a freedom.

Really, if you don’t put a licence on your code then people can’t use it but if anyone asked I would probably let them. A licence should let them know whether they can use it without having to ask you.

However, anything serious that you release needs a licence. Choosing the right licence for your project can be time consuming and involve reading and completely understanding legal documents, which isn’t what most of us want to do. It is very common for people to release code under the GNU General Public Licence (GPL) or Lesser General Public Licence (LGPL).

Terms such as “derived works” and what constitutes these are even more of a problem.

There are a few key points around releasing under these licences. Please bear in mind that I do not have a legal background.

  1. Anyone is free to distribute your work, even if you have sold it to them.
  2. You must provide source code for anything you release.
  3. Anyone is free to modify your work.
  4. If you use code licensed under the GPL in your project, your project must also be licensed under the GPL. This means that for people not releasing under the GPL, your code is essentially useless and to all intents and purposes does not exist.

The first point here makes selling your work difficult. Even if you successfully sell it, anyone who has bought it can just give it away for free.

The second point here makes releasing closed source binaries a problem. Anything you release must be released with accompanying source code. This causes further problems for commercial software, as attempts to safeguard it against piracy can be easily removed, which is also legal by the third point here.

The third point means that people can effectively take what you have done, make slight changes to it and pass it off as their own work.

However, the fourth point here is the biggest problem. When you licence under the GPL, you are not only causing the above problems for yourself (which may be ideal for you), you are also forcing anyone who uses your code to have all the same problems (which probably won’t be ideal for them). If they don’t want these problems in their own work, they can’t use yours. Fortunately there is a solution for this. The LGPL varies from the GPL in that projects don’t have to use the same licence as you in order to use your code. This makes it ideal for freely releasing libraries that anyone can use.

Somewhat worryingly, FSF and GNU are trying to trying to get people to use GPL instead of LGPL and, it seems, the only reason they are doing this is to give “free” software an advantage. I don’t agree with this ethically. For free software to be free, everyone should be able to use it and that includes people who want to use it in commercial projects.

I’ve lost count of how many times I’ve found a nice little snippet of code that does exactly what I want and the author seems to want to release it to the public for everyone, but has released it under the GPL making it unusable in a non-GPL project.

There are many other licences out there to choose from, such as the MIT and BSD licences. So, please choose the licence for your project carefully and think of the consequences for your target audience before blindly slapping a GPL sticker on it.

Printer friendly Fudzilla RSS

Wednesday, September 29th, 2010

Fudzilla is a nice site for catching up with daily tech news. I prefer to set up my email client Thunderbird to aggregate the RSS, but when you have a 1920×1200 monitor and Fudzilla still puts so much crap at the top of the article that it ends up looking like this and you have to scroll down before you can even read a few lines of text, its becomes a terrible user experience.

Wouldn’t it be better to get the text at a reasonable size, taking 100% of the width available, with no crap at the top that you have to scroll past?

Well, you can. The printer friendly view does all of this, and all you have to do is change the link in the RSS to use the printer friendly URL instead of the standard one. Here is some php do do just that! Just put this on a server somewhere and subscribe to that URL instead of the normal fudzilla feed.

<?php
header("Content-Type: application/rss+xml; charset=utf-8");
 
$url = "http://www.fudzilla.com/?format=feed";
 
$rss = fopen($url, "rb");
$contents = stream_get_contents($rss);
fclose($rss);
 
$contents = str_replace("?</guid>", "%3f</guid>", $contents);
$contents = str_replace("</guid>", "?tmpl=component&amp;print=1</guid>", $contents);
echo($contents);
?>

You may be able to just view the description in your RSS aggregator, but it tends not to show the whole article. In the past, fudzilla has put just the subtitle in there, for example.


Update: This has been broken by fudzilla using a HTTP 302 redirect and a cookie but anything that will take the cookie from the first request and use it for the second request should be able to handle this easily. Here’s a function for it that uses cURL.

function download_string_curl($url)
{
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  curl_setopt($ch, CURLOPT_COOKIEFILE, "THIS_FILE_DOES_NOT_EXIST");
  $contents = curl_exec($ch);
  curl_close($ch);
  return $contents;
}

As a note to Fudzilla: This is only necessary because of the sheer amount of crap you put at the top of your page. You shouldn’t have to scroll down an entire screen height before you can even see the content of that page. That’s just poor design. If adding cookies and a redirect was an attempt to stop screen scraping, it didn’t work. You can disable that now and save yourself some bandwidth.

Compiling zlib.lib on Windows

Thursday, August 26th, 2010

Update: see comments section for a more up to date method of doing this. Don’t forget to run contrib\masmx86\bld_ml32.bat from the Visual Studio Command Prompt before compiling this way though.

This article still applies if you want to compile on older versions e.g. MSVC 6.0, for older projects or if you have problems with the new method.


zlib is the standard for lossless data compression. The DEFLATE compression algorithm is the basis for just about every lossless compression format out there, including “zip” and “gzip”, which is itself part of zlib.

There are two ways that it can be used from C/C++ projects in Windows.

Firstly, it can be used by dynamic linking (dll). This means using zdll.lib and shipping the appropriate version of zlib1.dll with your project. This is not a problem, as Windows versions of both of these files are provided.

The second way is to use static linking. That is, having all of the code in one .lib file and compiling it into your exe so that you do not have to distribute zlib1.dll. This means compiling zlib.lib.

In version 1.2.4 of zlib, a “projects” directory was provided, with a Microsoft Visual C++ 6.0 project. However, it seems that version 1.2.5 has not included this project. This means that the best solution is to go and get the 1.2.4 source and compile it yourself. However, the zlib project seems to be kept inside the libpng project on sourceforge.net, so it is not immediately obvious where to find older versions of the zlib source code.

zlib 1.2.4 source (zip)

Extract the zip, open projects\visualc6\zlib.dsp in Visual Studio (I used 2005) and compile “LIB Release” (and optionally “LIB Debug”)

Copy zlib.h and zconf.h from “include” to your Visual Studio “include” directory, and zlib.lib (and zlibd.lib if you made it) to your Visual Studio “lib” directory.

On 64 bit Windows, with Visual Studio 2005, this is “C:\Program Files (x86)\Microsoft Visual Studio 8\VC\” so adjust for your version of Visual Studio.

You now just need to add “zlib.lib” to your “Linker -> Input -> Additional Dependencies” line in your C++ project configuration to use it (and optionally zlibd.lib for the debug version).

Remote recursive sha1sum with php

Wednesday, June 2nd, 2010

To calculate the SHA-1 sums, display them and make them available for download in a sums.gz file:

<?php echo(`find ./some_directory/ -type f | grep -v sums.gz | xargs sha1sum | gzip -c | tee sums.gz | zcat`); ?>

To check the sums:

<?php echo(`zcat sums.gz | sha1sum -c -`); ?>

Get external IP address with Python

Wednesday, May 26th, 2010

Here’s a quick snippet of Python code (tested in 3.0) to quickly look up your external IP address over HTTP and display it:

import urllib.request
print(str(urllib.request.urlopen("http://automation.whatismyip.com/n09230945.asp").read(), "utf8"))

wp-syntax to look like Visual Studio

Tuesday, March 23rd, 2010

wp-syntax is a nice plugin for WordPress using GeSHi to produce syntax highlighted blocks of code.

The default colours aren’t very nice though, and there is no way to easily change them. wp-syntax-colorizer (horrible name) makes it easier to set the colours, but defaults to even worse colours.

Most of us want readable colours that we are used to from IDEs, and most of us will be using either Visual Studio or Eclipse. I mainly use the former. To get it to use the visual studio colours I edit wp-syntax-colorizer as follows:

function my_custom_geshi_styles(&$geshi)
{
  $overall = "black";
  $keyword = "blue";
  $literal = "maroon";
  $comment = "green";
 
  $geshi->set_overall_style("color: $overall;", true);
 
  $geshi->set_keyword_group_style(1, "color: $keyword;", true);
  $geshi->set_keyword_group_style(2, "color: $keyword;", true);
  $geshi->set_keyword_group_style(3, "color: $keyword;", true);
  $geshi->set_keyword_group_style(4, "color: $keyword;", true);
 
  $geshi->set_symbols_style("color: $overall;", true);
  $geshi->set_methods_style(1, "color: $overall;", true);
  $geshi->set_regexps_style(1, "color: $overall;", true);
 
  $geshi->set_strings_style("color: $literal;", true);
  $geshi->set_numbers_style("color: $literal;", true);
 
  $geshi->set_comments_style(1, "color: $comment;", true);
  $geshi->set_comments_style('MULTI',"color: $comment;", true);
}

Remove Size bbcode from phpbb

Thursday, March 4th, 2010

One of the most annoying things about phpBB is the ability for people to randomly use [size=”200″]Huge[/size] BB code, with no easy way to remove it and every time you update between versions it comes back.

To disable it:
in /include/bbcode.php remove the whole

case 5:
...
break;

section.

To remove it from the posting page:
in /styles/prosilver/template/posting_buttons.html remove the whole

<select>
...
</select>

section.

After that, delete your “cache” directory so that it uses the new copy.

Remember that you have to repeat this process after every update to phpBB3.