Wednesday, September 30, 2009

FxCop custom dictionary

FxCop is an excellent tool for ensuring code consistency, but sometimes I need to add domain jargon to the custom dictionary. Since there is no official XML schema for CustomDictionary.xml, the following file is the most authoritative reference:

C:\Program Files\Microsoft FxCop 1.36\CustomDictionary.xml

(If installed into the default location on a 32 bit machine.)

Tuesday, September 29, 2009

Slow builds of Windows Mobile applications

Every time I install a new machine with Visual Studio and write applications for Windows Mobile, I need to turn off the Platform Verification task to get reasonable build times, but I invariably forget how to do this.

The file is %windir%\Microsoft.NET\Framework\{newest framework version}\Microsoft.CompactFramework.Common.Targets. Open this file for editing (as administrator), and add a 'false' condition to the PlatformVerificationTask.

The line you edit should look like:
Name="PlatformVerificationTask" Condition="false">

Tuesday, September 22, 2009

WinDbg with managed code

Loading the correct version of SOS is done by: .loadby sos mscorwks.

Useful commands in SOS:
List CLR threads: !threads
Dump managed stack of current thread: !CLRStack
Dump managed stack of current thread with parameters and locals: !CLRStack -a
Dump managed and native stack of current thread: !DumpStack
Break on first chance CLR exceptions: sxe clr
Get IL of method: !ip2md address, then !u result
Display last exception info: !pe (use Event Filters to run the command whenever a CLR exception is thrown)

Another useful tool is SOSEX, which supports deadlock detection, etc.

I was unable to get WinDbg to be the postmortem debugger for all crashes - installing it only seems to work for native applications.

Native commands:
List O/S threads: ~
Change thread by: ~{threadnumber}s or ~~[{threadid}]s
Handle tracing: !htrace

This cheat sheet is also useful.

WinDbg extensions

This blog entry will be updated to keep track of useful extension DLLs used with WinDbg since I don't use WinDbg very often:

SieExtPub.dll: useful for seeing what locks and critical sections are affecting your program. Download at Debugging Tools Download.

Loading the correct version of SOS is done by: .loadby sos mscorwks.

Wednesday, September 2, 2009

SafeHandle better than IntPtr for O/S HANDLEs

During my recent travels through P/Invoke, I found SafeHandle, which is preferable to IntPtr for handle types.

See: this blog entry.

Unfortunately, as with many good things, it is not available in the Compact Framework. No HandleRef either.

Now I just need to find out whether I should be using IntPtr or UIntPtr...

GCHandle

I seem to struggle to find this little class whenever I need to, and usually only find it after exhausting all the methods on Marshal before eventually finding it using Google.

GCHandle is used to get the address (IntPtr) for an object and can pin the object so it won't be moved while native code is using it.

This blog describes why GCHandle doesn't implement IDisposable.

Trap for young players: if your object is a struct or other value type, it will be boxed and that copy will be pinned, not your original structure. This boxing happens when you pass your instance to the GCHandle constructor and it is cast to the object base class.

Welcome

This blog is somewhere for me to jot down any snippets or ideas regarding programming, and isn't really intended for others to read. Anyone else finding this useful will just be a bonus!