Inside and Out…

An attempt to understand technology better…

[Download] PocketMaps 2.0 – with GPS support!

Posted by Gaurav Khanna on July 4, 2007

Last week I updated PocketMaps to support GPS. Once the GPS receiver is connected to your Windows Mobile device, PocketMaps can:

  • Map your current location on the map
  • Auto-refresh the map as you move to plot your move
  • Display details regarding your current location, like altitude, wind speed, etc.

You can download it from Below are some screenshots:

Displaying current location’s details

Support for GPS – mapping current location, autorefresh to plot your move, etc.


Posted in .NET Compact Framework, Downloads, Windows CE/Windows Mobile | 1 Comment »

[Download] Pocket Maps – mapping addresses on the Pocket PC

Posted by Gaurav Khanna on June 25, 2007

I just finished authoring PocketMaps, a .NET CF 2.0 based application for Windows Mobile 5.0 based Pocket PC devices that does address mapping on the device using the Yahoo! Map Image API. You can enter the address as free text, or with city/state/zipcode details or as latitude/longitude combination and PocketMaps will map it for you. Particularly useful when you are on the move and want to know the location of an address. It also lets you save the map image that you can send via email/bluetooth, etc.

Below are some screenshots of the application:

PocketMaps in the Programs Folder

An address mapped by PocketMaps

Support for zooming in/out on the map.

It’s free for download and use – download it from

Posted in .NET Compact Framework, Downloads, Windows CE/Windows Mobile | 4 Comments »

Enumerating AppDomains – I

Posted by Gaurav Khanna on June 9, 2007

Surprisingly, enumerating AppDomains is not that straightforward. The System.AppDomain type does not expose functionality to enumerate them at all. The way to enumerate them will be to use the V1 Hosting interface, ICorRuntimeHost and then invoke the EnumDomains and NextDomain methods against it to get the list. If you are using CLR 2.0, you needn’t worry as CLR 2.0 does implement the V1 interfaces as well.


Below is the snippet that exemplifies the concept (note: AppDomains will only be enumerated in the current process – I will discuss AppDomain enumeration in remote processes later). You can download the source code (VS Solution) from


// EnumAppDomains.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <windows.h>
#include "mscoree.h"
#import "C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\mscorlib.tlb" raw_interfaces_only
using namespace mscorlib; 
#define EXITONERROR(hr, mesg) \
if (FAILED(hr)) \
{ \
printf("%s\n", mesg); \
ExitProcess(1); \
#define SUCCESSFUL(hr) (SUCCEEDED(hr) && hr != S_FALSE) 
int _tmain(int argc, _TCHAR* argv[])
ICorRuntimeHost * pV1HostPtr = NULL;
HRESULT hr = CorBindToRuntimeEx(NULL,
NULL, 0, 
(LPVOID *)&pV1HostPtr);
EXITONERROR(hr, "Unable to get ICorRuntimeHost*"); 
// Start the runtime
hr = pV1HostPtr->Start();
EXITONERROR(hr, "Unable to start the runtime"); 
// Enumerate the AppDomains
hr = pV1HostPtr->EnumDomains(&adEnum);
EXITONERROR(hr, "Unable to enumerate AppDomains");
// Loop thru the domains
IUnknown * pDomainUnk = NULL;
hr = pV1HostPtr->NextDomain(adEnum, &pDomainUnk);
// Got the IUnknown* to the AppDomain - convert it to AppDomain pointer
_AppDomain * pCurDomain = NULL;
hr = pDomainUnk->QueryInterface(__uuidof(_AppDomain), (VOID**)&pCurDomain);
// Display the name of the AppDomain
BSTR str;
if (SUCCESSFUL(pCurDomain->get_FriendlyName(&str)))
wprintf(L"AppDomain: %s\n",str);
printf("AppDomain: unable to get the name!\n");
// Loop onto the next Domain
hr = pV1HostPtr->NextDomain(adEnum, &pDomainUnk);
// Stop the runtime
if (pV1HostPtr)
return 0;

Posted in .NET Framework, CLR, Development | Leave a Comment »

Lessons in Exception Handling

Posted by Gaurav Khanna on May 13, 2007

Working on the CLR’s exception handling subsystem is not just challenging but also gives unique opportunities to understand how the various exception handling subsystems (e.g. SEH, C++ EH, etc) work, and in particular, how they interoperate with one another. It was such case that had me discuss one such issue with Kevin, development lead of the JIT, that he mentions here.

That discussion got me exploring the interoperability aspects of EH further and resulted in the bullets listed below. Incase you are interested, download the PDF version of this article from here and the accompanying source code from here. For those uninitiated to exception handling, Matt Pietrek’s must read article is here.

Now, onto the interoperability aspects of EH.

Note: the following discussion is in context of the x86 platform

  • Catching an exception using __try/__except SEH handler will result in RtlUnwind being called without exception record

    Structured Exception Handling (SEH)
    is built into the Windows OS and it is how the OS processes the exceptions. Compilers offer their own specific ways of setting up a SEH handler. To setup one using Microsoft Visual C++, we use __try/__except keywords. For example:

    printf(“Calling the /EHSc function\n“);
    __except (IsForUs(GetExceptionInformation()))
    printf(“In __except\n“);

    If the InvokeCPPFunction throws an exception, the OS will invoke the filter function (the IsForUs function referenced in the braces of __except) and check its return value. The filter function can be passed exception details that can be retrieved using the GetExceptionInformation function and using that information, it can decide whether it wants to process the exception or not.

    If the filter function returns EXCEPTION_CONTINUE_EXECUTION, the OS retries the instruction that created the exception. However, if the return value is EXCEPTION_CONTINUE_SEARCH, it tells the OS that the filter function has declined to process the exception and the OS continues its search for the handler in the SEH handler chain (that is available from FS:0 on x86 machines for each OS thread).

The filter function can also return EXCEPTION_EXECUTE_HANDLER to tell the OS that it wants to handle the exception and the OS proceeds to invoke the funclet within the __except block. But before invoking the funclet, the OS has to unwind the call stack. On x86, the code generated by VC++ compiler does this by invoking RtlUnwind API, with NULL for the third argument – this argument is a pointer to the exception record for the exception being processed. This is done by the compiler by invoking GlobalUnwind (an internal function) after doing some processing. GlobalUnwind makes the call to RtlUnwind as shown below:


70508FB2 push ebp

70508FB3 mov ebp,esp

70508FB5 push ebx

70508FB6 push esi

70508FB7 push edi

70508FB8 push 0

70508FBA push 0

70508FBC push offset ReturnPoint (70508FC7h)

70508FC1 push ecx

70508FC2 call RtlUnwind (70526620h)

The highlighted red line is the one where NULL is pushed for exception record.

What this implies that all SEH handlers that are in the x86 FS:0 chain, prior to the one that agreed to process the exception, will be called by the OS once again to give them a chance to do some cleanup (e.g. release resources) – this is typically known as unwinding of the stack. And since the unwind was initiated with a NULL for the exception record, if your SEH handler relies on checking the exception record for details before deciding to do the cleanup, it can potentially fail (e.g. not end up doing the cleanup)!

  • Corollary Lesson: Don’t throw (and expect to catch correctly) exception across environment boundaries. E.g. throwing a C++ exception and trying to catch it using a SEH handler

    Since a C++ application can have a mix of both C++ exception handling constructs and SEH exception handing constructs, it is easy to commit the mistake of writing code that throws an exception from C++ EH and excepts that to be correctly caught and processed in SEH EH constructs – the focus is on correct processing.

    Let’s extend the previous example. Assume that InvokeCPPFunction is a function in a DLL that was compiled to use C++ EH (using the /EHSc switch) and implemented as shown below:

    class CPPClass2
    printf(“CPPClass2 constructor\n“);
    printf(“CPPClass2 destructor\n“);

    class CPPClass
    printf(“CPPClass constructor\n“);
    void ThrowException()
    CPPClass2 cpp2;
    throw 1;
    printf(“CPPClass destructor\n“);

    __declspec (dllexport) void InvokeCPPFunction()
    CPPClass cpp;

We have two classes, CPPClass & CPPClass2 that have destructors each. InvokeCPPFunction instantiates CPPClass object, cpp, and invokes the ThrowException method, which in turn, instantiates CPPClass2 object, cpp2, and throws an exception.

As per the C++ semantics, when this exception is caught, the destructors should be invoked as they are expected to do the cleanup for the respective class instance. However, this depends upon who catches the exception.

In our previous example, InvokeCPPFunction was invoked from within __try/__except SEH mechanism. Hence, when ThrowException throws an exception, the OS walks the FS:0 chain to look for a handler that will handle the exception. When the OS comes to our __except block, if the filter funclet returns EXECEPTION_EXECUTE_HANDLER, as per our last discussion, RtlUnwind is invoked with a NULL for the exception record pointer.

When this happens and the unwind call comes to C++ exception handler, the C++ EH does not process the unwind since the exception record is NULL. This is because C++ EH only processes the unwind when the exception code in the exception record has the C++ exception code (0Xe06d7363). Thus, no destructors are invoked and that is not something you want. Below is the output this example:

Hence, it’s important to catch the exception in the environment/EH-context it was thrown in, since that EH-context will know how to process it correctly.

Posted in Development, Exception Handling, Windows, Windows API | Leave a Comment »

[Download] TransNTFS v1.0.0.1 – support for folders, symbolic and hard links and more

Posted by Gaurav Khanna on March 18, 2007

I have updated TransNTFS, my managed implementation for the transactional NTFS APIs that have been introduced with Windows Vista. TransNTFS now also supports the following operations under a transaction:

It comes with complete documentation (as always) and can be downloaded from

Posted in .NET Framework, Development, Downloads, Managed C++, Windows API | Leave a Comment »

[Download] TransNTFS – Managed implementation of Transactional NTFS (TxF) APIs

Posted by Gaurav Khanna on March 15, 2007

Sometime back, I had made a post regarding a native C++ class I had authored for using some of the Transactional NTFS (TxF)APIs introduced in Windows Vista.

Almost two weeks from that post, I just finished my managed implementation, TransNTFS, that enables you to use the TxF APIs from managed code. As always, Managed C++ enabled the implementation with excellent ease. It comes with complete documentation and can be downloaded from

Below is an example usage of how to copy a file under a transaction:

using System;
using System.Collections.Generic;
using System.Text;
using WinToolZone;
namespace ManagedTransactionCopyDemo
class Program
static void Main(string[] args)
TransNTFS refTC = new TransNTFS();
// Init the transaction
bool fSuccess = refTC.BeginTransaction();
if (fSuccess)
fSuccess = refTC.CopyFile("d:\\transcopy.pdb", "d:\\t.pdb", false);
if (fSuccess)
fSuccess = refTC.Commit();
if (fSuccess)
Console.WriteLine("Copy successful!");
ShowErrorAndExit("Commit failed!", refTC.LastError);
ShowErrorAndExit("Copy failed!", refTC.LastError);
ShowErrorAndExit("Unable to start the transaction!", refTC.LastError);
private static void ShowErrorAndExit(string p, int p_2)
Console.WriteLine("ERROR: {0}", p);
Console.WriteLine("CODE: {0}", p_2);

Posted in .NET Framework, Development, Downloads, Windows, Windows API | 1 Comment »

Programming the Transactional NTFS (TxF)

Posted by Gaurav Khanna on March 1, 2007

One of the key new features of Windows Vista is a component called the Kernel Transaction Manager (KTM) that brings inherent support for transactional development in not just the kernel-mode but also for user mode. Infact, NTFS has been enhanced to use and support transactions such that couple of new APIs (e.g. CopyFileTransacted, MoveFileTransacted just to name a few) have surfaced up. You can get more details on Transactional NTFS here.

To demonstrate the power of the new APIs, I wrote a C++ class library (unmanaged), CTransCopy, that allows you to:

  • Copy files under a transaction
  • Move files under a transaction
  • Commit or Rollback the transaction
  • Let you wire up a callback handler for copy/move progress

You can download it from The zipped archive also contains a sample client source code. BTW, since the .LIB file containing the class was compiled using VC++ 2005 compiler, you will need the same to link against it and write an application. You can use VC++ 2005 Express Edition. Below is an example usage of the same:

CTransCopy tcopy;

if (tcopy.IsOSSupported() == FALSE)
printf(“This application requires Windows Vista or later.”);
return -1;

if (tcopy.Init() == FALSE)
printf(“Init failed with error %08X\n”,tcopy.LastError());
return -1;

// Setup a callback for progress

if (tcopy.CopyFileW(L”D:\\KGK\\Development\\VS\\Windows Vista\\VC\\TransCopy\\debug\\transcopy.pdb”,
L”D:\\transcopy.pdb”,FALSE) == FALSE)
printf(“Copy/move failed with error %08X\n”,tcopy.LastError());
return -1;

if (tcopy.Rollback() == FALSE)
printf(“Rollback failed with error %08X\n”,tcopy.LastError());
return -1;

if (tcopy.Commit() == FALSE)
printf(“Commit failed with error %08X\n”,tcopy.LastError());
return -1;

printf(“Copy/move successful!\n”);

The ProgressCallback function is implemented as shown below:

void ProgressCallback(LARGE_INTEGER total, LARGE_INTEGER transferred)
double percent = (transferred.QuadPart*100.00)/total.QuadPart;
printf(“%f%% over\n”,percent);

Posted in .NET Framework, Development, Downloads, Windows, Windows API | 1 Comment »

Article: Customizing AppDomain Creation

Posted by Gaurav Khanna on February 26, 2007

Did you have a scenario where you wished you could customize the creation of AppDomains? Or, you could control how many AppDomains any code that runs in your application context (e.g. if you are writing a plugin load framework) could create? Or, how about reusing AppDomains for various assemblies?

I have just finished an article on how any of the above scenarios can be accomplished using the System.AppDomainManager type, introduced in .NET Framework 2.0, by intercept AppDomain creation requests.

You can read the article at and download associated source code from

Posted in .NET Framework, Articles, CLR, Development, Downloads | Leave a Comment »

How the SSCLI [a.k.a. Rotor] managed &quot;new&quot; works

Posted by Gaurav Khanna on February 12, 2007

One of the best ways to understand how the CLR works internally is to have a look at the SSCLI [a.k.a Rotor] source code.

In How the SSCLI [a.k.a. Rotor] managed “new” works, I have used Rotor source code to discuss what happens behind the scenes when you use the managed new operator to instantiate a managed type, discussing implications of object size, how requests from multiple threads are handled, the different validations that are done, when out-of-memory exception is thrown, etc. If you are a managed code developer, you may learn a thing or two that can help you write better code besides understanding how the managed allocation works internally.

Posted in Articles, CLR, Development, Rotor | Leave a Comment »

Joined the Common Language Runtime (CLR) Team

Posted by Gaurav Khanna on September 13, 2006

Its been almost two months since I last blogged. This was majorly due to a change in my professional life.

After having a great time (travelling across the country, meeting developers at stretch, presenting at TechED and MEDC just to name few things) in my role as Developer Evangelist with Microsoft India, I relocated to Microsoft Corporation at Redmond to join the Common Language Runtime (CLR) team as a Software Development Engineer (SDE). In my new role, I would be working on a new type of CLR – and look forward to share my learnings about the runtime via this blog and my website,


Posted in Personal | 1 Comment »