Exception Filter

C# does not support exception filter. However, VB and IL support it. To add exception filter to C#, we can build a function in VB or IL, then call it in C#. The example below is one way we may implement the function to support exception filter. It asks the caller to provide four delegates,…



I mentioned that we can use ThreadPool.BindHandle to implement asynchronous IO. Here are roughly the steps necessary to make it happen: 1.       Create an overlapped file handle             SafeFileHandle handle = CreateFile(                                 filename,                                 Win32.GENERIC_READ_ACCESS,                                 Win32.FILE_SHARE_READ | Win32.FILE_SHARE_WRITE | Win32.FILE_SHARE_DELETE,                                 (IntPtr)null,                                 Win32.OPEN_EXISTING,                                 Win32.FILE_FLAG_OVERLAPPED,                                 new SafeFileHandle(IntPtr.Zero, false));         [DllImport(“kernel32.dll”, CharSet…



CLR’s thread pool has two pools of threads. The first pool is used by ThreadPool.QueueUserWorkItem. The second pool is an IoCompletionPort thread pool used by ThreadPool.BindHandle and ThreadPool.UnsafeQueueNativeOverlapped. ThreadPool.BindHandle is used by CLR to implement asynchronous IO. For example, FileStream uses it to implement BeginRead/BeginWrite. Developers can take advantage of it too. We will talk…


Conversion between System.String and char *

We can convert a char * to System.String with System.String’s constructor string str = new string((char*)p); And for the reverse: fixed(char *p = str){} Why do we care about conversion between System.String and char *? From this article, this is the fastest way to marshal strings between managed and native boundary.


XXX is not a valid Win32 application

If you have used your Vista SP1-based computer for extended period, you may experience some problems starting large applications, for example, Office 2007 applications. Specifically, you may receive a message “XXX is not a valid Win32 application”. If you do experience this problem, you can install hotfix KB952709. http://support.microsoft.com/kb/952709/


Managed ThreadPool vs Win32 ThreadPool (pre-Vista)

The following is a conversation between me and a CLR dev. The conversation is very informative so I quote it here. From:  Sent: To: Subject: RE: ThreadPool.QueueUserWorkItem   There might be some confusion here around the meaning of the term “I/O Thread.”  In the Windows thread pool (the old one, not the new Vista thread…


Managed Watson Dump

From .Net Framework 2.0, Dr. Watson is able to generate dump compatible with .Net framework. This means, dumps with heap data generated by Dr. Watson contains information about managed heap so they can be analyzed with sos.dll. Some update to Dr.Watson is necessary to make this happen. Dumps generated by older Dr.Watson is not compatible with…


Inspect a 32 bit Process Dump Generated by a 64 bit Debugger

64 bit Windows can run both 32 bit process and 64 bit process. For debugging though, you want to use 32 bit debugger to debug 32 bit process, and 64 bit debugger for 64 bit process. Otherwise it won’t be pretty. Occasionally, I receive a 32 bit process dump generated by a 64 bit debugger….


Getting a Full Memory Dump for a Process

To diagnose a problem for a remote customer, sometimes the easiest way is to have the customer generate a full memory dump for the process, and share the memory dump. In Vista, task manager can generate a full memory dump from the Processes tab. In Windows XP , this functionality does not exist. However, Windows…

Event Handles “leak”

On our stress run, we saw our process’ handle count steadily increases until certain point, then it stabilizes. However the number of handles is high. Most of those handles are Event handles. We are concerned about it. So we went off and did some investigation. Turns out the Event handles are coming from the use…