Saturday, 29 November 2008

Excel Survival Guide

If you see dashed lines in your spreadsheets....
Tools->Options->View and uncheck Page Breaks (Alt-K)

If you want colored borders: Cntrl-1 around the cells to border up and select the color.

Alt-TI -> make sure "Analysis ToolPak" and "Analysis Toolpak" - VBA are added. This will bring in some useful worksheet functions. Examples are:


  • EDATE(start_date, months). Note on dates: MS Excel stores dates as sequential serial numbers, so they can be used in calculations. By default, Jan 1 1900 is serial number 1, and Jan 1 2008 is serial number 39448. MS Excel for Mac uses 1904 as start date.
  • Alt-TDD (Tools, Data Analysis). Select analysis methodology e.g ANOVA single factor.

Saturday, 22 November 2008

Multithreaded Server Development in C#

ThreadPools - typically have a maximum number of threads. If all threads are busy, additional tasks are placed in a queue till one thread becomes available. If no threads are available, create a new set of threads to fill the thread pool. Some classes to consider:

ThreadPool - contains a HostProtectionAttribute (introduced in .NET 2.0). Here is some problem code using ThreadPool.

public static void Main() {
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); // Queue task
// Thread.Sleep(1000);
Console.WriteLine("Main thread exits."); }

The code above is bad as the program exits before ThreadProc runs (race condition).

WaitCallback - a callback method (delegate) to be called by a thread pool thread
AutoResetEvent - notifies one waiting thread an event has occurred
ManualResetEvent - notifies one or more waiting threads that an event has occurred.

If there are parts of the server you want to code in C++ you can create a C++ DLL using the Win32 Application Wizard. Here are the options you need to set:
  • Select Application Type->DLL and Additional Options->Export Symbols
  • If calling from non-Unicode code, do Project->Properties (ALT-F7), Configuration Properties->General->Character set (set to use multi-byte character set)
  • ALT-f7 -> Configuration Properties -> C/C++ -> Code Generation -> Runtime Libray: “Multi-threaded Debug (/MTd)”. DLL will statically link to the C runtime library. It will increase the size of the DLL but DLL will not be dependent on the correct version of the C runtime library DLL. This is the default anyway.

Sunday, 16 November 2008

CRT Library Blues

CRT libraries (Cruntime) are available in 3 distinct flavas in Win32 SDK.

One single-threaded and two multi-threaded variants.
  • libc.lib, statically linked simplicity for single-threaded programs
  • libcmt.lib, multithreaded cousin
  • crtdll.lib, belle of the ball, an import library for CRTDLL.DLL (part of NT) that supports multi-threaded programs.
CRT redist package can be downloaded here, which in addition to CRT and Std C++ runtime components, also includes stuff for OpenMP (shared-memory parallel programming)and MSDIA.

Thursday, 13 November 2008

StreamReaders, StringWriters, MemoryStreams, FileStreams, Serialization in .NET

StreamReader - read files as lines of strings
FileStream - read files as arrays of bytes (need an Encoding to tranlsate back to strings)
MemoryStream - reads data from memory instead of from file

System.Runtime.Serialization
System.Xml.Serialization

Example of serialization of List:

TextWriter writer = new StreamWriter(PATH);
XmlSerializer serializer = new XmlSerializer(typeof(List<long>)) -- typeof returns an object of type Type
serializer.Serialize(writer, mylist);
writer.Close();

StringWriter - stream that can morph into a string
Path.GetTempFileName - create temp file and return full path

Monday, 10 November 2008

Over-Aggressive IntelliSense

IntelliSense can be a productivity boon - provided you don't need to undo rubbish autocompletes. Sadly bad autocompletes are very common. To minimise the frequency of false completions do the following:

Go to: Tools->Options [ Text Editor->C#->IntelliSense]
Go to: Committed by typing the following characters: [everything except letters and numbers!]

Remove . ) = ; } from the list.

Joys of TreeView

The first experience building a TreeView will take ages, the next not so long, and the next after that will be quick. Once you know the a) objects, b) various constructors, c) properties and d) events its very easy.

Creating hierarchy of nodes: TreeNode main = new TreeNode(); main.Name = "main"; main.Text = "main"; main.Nodes.Add(someNode);
Constructors from MSDN:
The TreeNode constructor has six different overloads.
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.treenode.aspx
Notice how everything on MSDN is stored on ASPX (xhtml) pages.

Favorite Overloads TreeNode("text"); TreeNode("text", childNodeArray)
To get selected data: main.SelectedNode.Text
How do you reverse an array of TreeNodes? Array.Reverse(treeNodeArray);
AfterSelect event handler: processes a node selection. To process nodes of only a certain depth do: if (e.Node.Level == x) { do_something(); } where e is a TreeViewEventArgs object.

Remember how to program this using JTrees in Java?

Java is IRON, C# is STEEL.

Sunday, 9 November 2008

Debugging 401 Errors in Internet Information Services

HTTP, an application level protocol for distributed hypermedia systems, came into existence in 1990 with HTTP/0.9, a simple protocol for raw data transfer over the Internet.

With HTTP 1.x came a built-in mechanism for authentication, fully documented in RFC 2617. HTTP 1.1 supports two modes of bulit-in authentication "Basic" and "Digest". Basic authentication transmits the username/password pair in unencrypted form from browser to server. It should only be used over an encrypted medium e.g. SSL (Secure Sockets Layer recently renamed by the IETF to TLS, or Transport Layer Security). Digest authentication sends a one-way hash of the username/password pair, calculated with a time-sensitive salt value from the server. The purpose of the salt value is that the username-password hash is always unique, and protected from replay attacks. Note though that only the authentication details are protected by the hash and any data sent following successful authentication will be visible to any party with access to the network traffic. To overcome this, an encrypted stream is needed.

Attempting to retrieve a page using HTTP authentication can result in an HTTP 401 (Unauthorized) error (some other interesting error codes are listed here, including the currently unused 402 error - Payment Required). The response from the server will include a "WWW-authenticate" header of the form (WWW-Authenticate: AUTH_SCHEME realm="REALM_NAME"). A valid value for AUTH_SCHEME would be Basic, to denote basic authentication. The "realm" is the section of the website protected by the authentication scheme. Since HTTP is stateless, servers don't remember who is logged in and clients must send correct headers for each protected page they wish to access.

IIS provides substatus codes to analyse the underlying causes of 401 errors. The codes are: 1) invalid credentials, 2) wrong authentication method, 3) ACL issue, 4) ISAPI (Internet Server API)/CGI issue, and, 5) access denied by URL authorization policy on server.

Wednesday, 5 November 2008

How to Write a Good Old Windows Batch File (GOTO considered useful)

Introduction

This post is about effective batch file writing. No Windows programmer is complete without a working knowledge of batch files. Here are some reasons you would use good old-fashioned batch files as a preferred technology over newer, shinier alternatives:

Advantages of Batch Files
  1. Scripting languages are not installed on client PCs, doing so would be a hassle
  2. Need to run some commands on a network drive which may have partial trust issues if implemented in .NET

Tips and Tricks

  1. For comments, use :: instead of REM. The reasons will become apparent. : is a notation for labels in Windows batch files.
  2. When setting variables do: set VAR="". Every time you use VAR you need to say %VAR%. That's pretty obvious when you're echoing stuff to the screen, but once you start to get into conditional statements, it can be easy to forget the necessary %% delimiters e.g. you need them when doing: mkdir %VAR%, or IF EXIST %VAR% GOTO VAREXISTS:, or COPY %SOURCE%\*.* %TARGET%\. See how ubiquitous percentage signs are in real-life batch file writing!
  3. To access command-line arguments use %1, %2, .. , %n. Again the percentage sign - this time operating in a unary capacity!

@echo off (TRACE OFF)

Worth adding a word on the @echo off statement oft-seen atop many a batch file. echo off prevents commands echoing themselves irritatingly to the console; the @ prefix is a self-reference trick which applies the command to itself.

Tricky Syntax

Batch files have some syntactical tricks which seem a little awkward. Here is a summary:

:~n (colon-tilde) -> extract the first (or last if n is negative) n characters from a variable e.g. %PATH:~-10% will extract the last 10 characters from the path.

:~m,n (colon-tilde-comma) -> a small variation of colon-tilde. m characters offset, then next n characters. m=0 means extract all the characters. This is a substring function.

%~dp0 (percent-tilde-drive-path) -> %0 determines where the batch file is running from (a la %1, %2 et al) and the dp produces the drive letter and path - so this tells us which dir/path we are running from. %~dp1 will do the same for variable 1 (seems a bit weird to do that).

%~sp0 (percent-tilde-short-path) -> same as dp0 only returns "short path" (i.e. drive letter is missing so C:\WINNT becomes \WINNT)

Google groups has a post identifying even more % codes; take a gander here.

Bits and Bobs

To compose powerful batch scripts you need a good working knowledge of DOS commands that are legal under Windows. The site here gives a good Vista-focused introduction.