Visual Studio 2005 – another day, another bug.

FIX: Error message when you use the debugger in Visual Studio 2005 to debug an MFC class library application: “Managed Debugging Assistant ‘LoaderLock’ has detected a problem in ‘‘”

I liked Visual Studio 2005 a WHOLE lot more when I wasn’t running into its bugs. Right now, I can’t debug my application. So what am I to do? Resort to pre-debugger techniques? I don’t even remember those. printf littering? log4net for no reason but debugging. This was supposed to be a trivial app to be written in one hour.

Another day, another bug.

.NET Command Line Options – Mono.GetOptions

I actually write .NET Command Line applications for Windows. I write little tools, that maybe would be usually be written in VBScript by a sysadmin, but since I was a sysadmin and AM a C# guy, I write in the latter. One thing that Microsoft seems to ignore (at least until PowerShell) is the command line and easy programming for the command line. I’ve never seen an equivalent to getopts in any MS Shell, or any API.

Mono implements a getopt library called Mono.GetOptions which works very nice on the Microsoft Platform as well. Just install Mono and copy the Mono.GetOptions.dll from C:\Program Files\Mono-1.2\lib\mono\2.0 or C:\Program Files\Mono-1.2\lib\mono\1.0 depending on which framework version you are targetting. I typically treat this dll as I do any third party dll as part of my project. In my case that means place in a lib directory and keep it under version control. I would prefer the Linux approach of no binaries under source control, but that doesn’t seem to be the standard on Windows projects.

Add the reference within your project to the lib/Mono.GetOptions.dll

The library works by extending Mono.GetOptions.Options and marking member with attributes.  The library includes many helpers, including the ability to automatically generate help messages.

class Options : Mono.GetOptions.Options {
  [Mono.GetOptions.Option(1,"Display this help message", 'h', "help")]
  public override Mono.GetOptions.WhatToDoNext DoHelp()
  {
    return base.DoHelp();
  }
}

The OptionAttribute has a number of default constructors.  In this case the arguments are maxOccurs, shortDescription, shortForm, longForm.  This allows the help to be displayed if you call Options.ProcessArgs with -h or -help.  See the source for more constructor forms: OptionAttribute.cs

To actually allow these options to be processed, you must call Options.ProcessArgs as I stated above.  You can play with options using this simple program:

    class Program
    {
        static void Main( string[] args )
        {
            Options lOptions = new Options();
            lOptions.ProcessArgs( args );

        }
    }

If you do this much and test by invoking with -h or -help, you will notice that there are some other options displayed in the output.  -help2 shows a couple of GetOptions debug and verbose options -~ and -. respectively.
  You will also see a message about using some assembly Attributes if you have not already added them.

[assembly: Mono.About( "This program does stuff." )]
[assembly: Mono.UsageComplement( "nonoptarg1 ... [nonoptargn]\nDo Stuff with nonoptarg1" )]
[assembly: Mono.Author( "Jay R. Wren" )]
[assembly: Mono.AdditionalInfo( "With no args this progam does stuff." )]
[assembly: Mono.ReportBugsTo( "jrwren@xmtp.net" )]

These assembly attributes work with the library to output usage information along with the normal System.Reflection.{AssemblyTitle,AssemblyCopyright,AssemblyDescription,AssemblyVersion} attributes.

Example:

Program 1.0.0.0 - Copyright c MyCorp - MyDiv 2006

Usage: Program [options] nonoptarg1 ... [nonoptargn]
DoStuff with nonoptarg1
Options:
  -h -help     Display this help message
     -help2    Show an additional help list
     -usage    Show usage syntax and exit
  -V -version  Display version and licensing information

With no flags, the current users known host putty registry keys will be listed.

Please report bugs to <jrwren@xmtp.net>

Depending on your application you can use Options members such as:


[Mono.GetOptions.Option("debug level",'d',null)]
public int DebugLevel = 4;
[Mono.GetOptions.Option("warning level",'w',null)]
public WhatToDoNext SetWarnngLevel(int warningLevel)
{
  this.WarningLevel=warningLevel;
  return WhatToDoNext.GoAhead;
}

And now you can simply access the Options.DebugLevel and Options.WarningLevel members. They will be set if the -d and/or the -w options were used on command invocation.

Further examples can be seen in the mcat sample from Mono subversion.

Castle had a birthday

Sometime last week Hammett posted links to a history of Castle in honor of its two year birthday.

I love history.

Reading about how castle sprung(punny?) from the Microkernel Pattern was interesting. I often forget that Castle started from Apache Avalon.

Early implementations of “Avalon Castle” could probably be found in CVS or SVN some place if you looked hard enough.

I find explainations of design decisions helpful to understanding a project.

Only 2 months later the Rails ActionPack inspired “Castle on Rails” was born. I think back to what I was doing then, and I realize I was still learning C# and messing with Web Services.

Just 2 days later(Nov 18th) the “Castle Project” was born.

And a few days later the concepts were introduced. Now that I think about it, Castle Project started much like TurboGears. Take the best of breed of some things and enhance them. ActiveRecord is just enhanced and simplified NHibernate. MonoRail’s default view engine is an enhanced NVelocity. Brail is boo, but enhanced for web views (whitespace matters less).

Thanks to everyone developing Castle, especially Hammett and Ayende. Their blog posts have helped me the most.

MonoTorrent and advanced C#

Alan wrote about profiling MonoTorrent.

He has some specific cases where GC is causing him issues. He gives some examples in the form of “I want to GC here”. I hate to be a nay sayer, but… well… NAY! 🙂 I’m a big fan of allowing GC to function. If you think you can outsmart GC, then you are wrong. If you think you need to, then there is something wrong with your model. I say don’t call GC.Collect().

Now in Allan’s first example, ignoring memoization as purhaps a better way, if the issue is that Example() is going to be called a large number of times, allocating many MyObject instances, then purhaps using an advanced C# feature is in order. C# does allow for stack based allocation. It requires the code to be marked unsafe, but it is likely that this use case is EXACTLY why this feature is included in the language. This isn’t Java that we are dealing with here! (small jab-hehe)

public unsafe static void Example() {
MyObject *pa = stackalloc MyObject[1];
MyObject a = *pa;
a.x = 15;
a.DoACalculation();
}

Of course, this requires that MyObject be a value type and not a reference type (struct, not class). And in my testing on Mono, I could not get this to perform better than simpy using new but using a struct instead of a class. So maybe refactoring a class to a struct would help performance. It maybe a good first step to see if it helps before going unsafe.

I must admit that I’m not sure I’m benchmarking this properly. I’m really just using this program. If anyone can shed some light on best practice here, I would appreciate it.


using System;
public class StackAllocExample {
public static void Main() {
Console.WriteLine("stackalloc");
DateTime start = DateTime.Now;
Console.WriteLine(start);
for(int i = 0 ; i < 100000000; i++ ){ Example(); } DateTime stop = DateTime.Now; Console.WriteLine(stop); Console.WriteLine(stop-start); start = DateTime.Now; Console.WriteLine(start); for(int i = 0 ; i < 100000000; i++ ){ SafeExample(); } GC.Collect(); stop = DateTime.Now; Console.WriteLine(stop); Console.WriteLine(stop-start); Console.WriteLine("heap alloc struct"); start = DateTime.Now; Console.WriteLine(start); for(int i = 0 ; i < 100000000; i++ ){ SafeStructExample(); } GC.Collect(); stop = DateTime.Now; Console.WriteLine(stop); Console.WriteLine(stop-start); } public unsafe static void Example() { MyObject *pa = stackalloc MyObject[1]; MyObject a = *pa; a.x = 15; a.DoACalculation(); } public static void SafeExample() { MyRObject a = new MyRObject(); a.x = 15; a.DoACalculation(); } public static void SafeStructExample() { MyObject a = new MyObject(); a.x = 15; a.DoACalculation(); } } struct MyObject { public int x; public void DoACalculation() { ResultFromCalculation = x+1; } public int ResultFromCalculation; } struct MyRObject { public int x; public void DoACalculation() { ResultFromCalculation = x+1; } public int ResultFromCalculation; }

I can do in 2 lines of C# code what take 4 lines of VB.NET

I program in .NET. It is unavoidable, at some point you will be asked about C# vs. VB.NET. Personally I could care less. I prefer C#. I’m not even sure why. I grew up BASIC (Atari Basic, Amiga Basic, GW-Basic and QBasic). Later I switched to curly braces for high school (C) and college(C++). Along the way I used a number of langauges which also use semicolons and curly braces (php, perl, and pike). So I definitely became a curly brace kind of guy. Even though I did use Visual Basic, Visual Basic for Applications, and VBScript over the same time period, I simply felt more at home in C or C++.

So, next time some VB.NET lover tries to make an argument for VB.NET over C#, I’ll be ready. I’d give an example here, but I can never understand these arguments or even the underlying statements. They are usually so nonsensical. I rarely remember things that I don’t undersand, so I’m lucky enough that their convoluted arguments go in one ear and right out the other. Next time I am ready! I guess I shall try to remember this retort from
http://worldofcoding.blogspot.com/2006/11/numericupdown-is-nice-control-eh.html

[C#]
foreach ( Control c in numericUpDown1.Controls )
tooltip.SetToolTip( c, “mytooltip” );

[Visual Basic]
Dim c As Control
For Each c In numericUpDown1.Controls
tooltip.SetToolTip(c, “mytooltip”)
Next

4 lines of VB.NET equals 2 lines of C#!

I feel much the same about the old csh vs. sh vs ksh. I just don’t care. But I don’t have any good examples for why I prefer bash.

Visual Studio tip of the century

Thanks hongmeig! Open File the fastest way

OMG I’ve been looking for this since November 2005. I often felt like everyone was in on a joke but me and that everyone knew this was built into Visual Studio except for me.

Press ctrl-/  (This take you to the ‘find in files’ textbox in the toolbar)  Now type “>of ” and start typing the name of the file which you wish to open. You will get a drop down of files which match. You can arrow down and press enter.

FINALLY a keyboard only “open file” which doesn’t require Alt-F-O (or ctrl-O) and arrow navigation of the directory heirarchy. I’m almost as happy as the time I discovered dictionary word completion in vim.

Querying Active Directory with Unix LDAP tools.

I want to run ldap queries against an AD, after all, AD is just LDAP, right?

Finding an AD server which to query can be done a number of ways. If you are on windows, an easy approach may be to use WMI or ADSI to ask AD where a domain controller is, but lets say you aren’t on windows. AD requires this information in DNS. One easy lucky way is server naming. You know that servers named Domain Controller or “DC” are probably a domain controller. I use the host command which comes with ISC BIND.


$ host -av myaddomain.myinetdomain.net

will give you a list (can be very long depending on your organization) of servers.

If that doesn’t show me what I what, I use this information How Domain Controllers are Located in Windows XP


$ host -av _ldap._tcp.myaddomain.myinetdomain.net

gives me a list of domain controllers which are listening for LDAP requests.

Now I can use ldapsearch to get the RootDSE. This doesn’t even require binding to the LDAP Directory


$ ldapsearch -x -h 192.168.199.10 -b '' -s base '(objectclass=*)'

Now the namingContexts attributes give me points off of which to start my searches. If you are familiar with AD then you should be familiar with these naming contexts. Of course one reflects my ad. e.g. dc=myaddomain,dc=myinetdomain,dc=net

If you happen to be in a large organization, take notice about some hints an helpers here. The serverName attribute often reflects the organizational structure of the AD. Is the AD organized by geography, business unit, or something else? The organizational unit in which this server is placed can give you hints.

From this point on you need to bind to the active directory. Now your account could be in any OU, so how do you know if you are cn=jsmith,cn=salespeople,cn=blah….. or if you are cn=jsmith,cn=Users,cn=blah… Well, quite cleverly, Microsoft extended the LDAP spec a bit and allows for binding with what they call a “User Principal Name”. You can login much like you would to a Windows computer.


$ ldapsearch -x -h 192.168.199.10 -b 'dc=myaddomain,dc=myinetdomain,dc=net' -s base -D 'jsmith@myaddomain.myinetdomain.net' -W

We know the domain portion of the User Principal Name from our naming contexts search before.

Now we can build all sorts of nice LDAP search expressions. Want to see all the computers in the domain?


$ ldapsearch -x -h 192.168.199.10 -b 'dc=myaddomain,dc=myinetdomain,dc=net' -s base -D 'jsmith@myaddomain.myinetdomain.net' -W '(objectclass=Computer)'

You can use AD’s Abiguous Name Resolution:


$ ldapsearch -x -h 192.168.199.10 -b 'dc=myaddomain,dc=myinetdomain,dc=net' -s base -D 'jsmith@myaddomain.myinetdomain.net' -W '(anr=smith*)'

Ultimately I want to search for computers in the Active Directory which have not been used in a while. Forgetting for a moment that lastLogon is not replicated in AD, how can we do this? or how can we even tell what that lastLogon value means. When I look at my ldap entry I get this for the lastLogon attribute:


$ ldapsearch -x -h 192.168.199.10 -b 'dc=myaddomain,dc=myinetdomain,dc=net' -s base -D 'jsmith@myaddomain.myinetdomain.net' -W '(anr=smith*)' lastLogon
...
accountExpires: 9223372036854775807
lastLogon: 128082628734460625
pwdLastSet: 128074920542439359

It turns out (and this is no joke) that this number is the time in some ANSI standard that is the number of 100 nanosecond intervals since January 1st, 1601. Ugh… There is much good information about this in the Dandelions technet article. I solved the problem of reading this using pythons AWESOME datetime module.


#!env python  from sys import argv
from datetime import datetime,timedelta
ansiTimeStart = datetime(1601,1,1)
lastLogon = timedelta(seconds= long(argv[1]) / 10000000)
#rfc822- with borked timezone 
if len(argv) and argv[2]=='-r':
print (ansiTimeStart+lastLogon).strftime("%a, %d %b %Y %H:%M:%S +0000" )
else:
#or ISO 8601
print (ansiTimeStart+lastLogon).isoformat()

So now I can send one of those crazy ansi dates and get a real date in either rfc822 format or iso8601 format.


$ ./ansidate 128082628734460625 -r
Fri, 17 Nov 2006 18:47:53 +0000
$ ./ansidate 128082628734460625
2006-11-17T18:47:53

Now what if I want to query for all computer records in a domain which have not had thier password set since August 1st, 2006. I have to find that crazy date. I just use python interactive for this.


$ python
...
>>>(datetime(2006,8,1)-datetime(1601,1,1)).days*3600*24*10000000
127988640000000000L

Now I can search.


$ ldapsearch -x -h 192.168.199.10 -b 'dc=myaddomain,dc=myinetdomain,dc=net' -s base -D 'jsmith@myaddomain.myinetdomain.net' -W '(&(lastLogon< =127988640000000000)(objectclass=computer)'

Note that ldap search filters don't have a less than operator, you must use less than or equal. a filter of '(lastLogin>1)' will fail with a Bad search filter error.

Using LDAP to query Active Directory is a natural fit, especially if you have LDAP experience in other applications. All of your LDAP knowledge should be applicable to Active Directory. I've found that much of the details on the AD schema attributes are not mentioned in most Microsoft documentation. It is best to go directly to the Windows SDK. Each attribute is well documented there.

debugging late bound code

I have a Windows Service that uses a plugin architecture for some things. I couldn’t figure out why I couldn’t step through one of the plugin modules. I checked that my plugin system wasn’t actually running in a separate app domain (not that it should matter) and it wasn’t. I have that set as a runtime configuration option, but no, only one app domain here. I scratched my head some more and took a look at my plugins directory. DUH! I stopped copying the pdb files (the debug symbol files) on build.

So if you run into the dreaded “cannot find source”, just remember that you must have the pdb files along side the dlls.

I didn’t find much when googling for this except for this link:
http://www.thescripts.com/forum/thread251214.html

No Silver Bullet Revisted

I found this “No Silver Bullet Revisted” link from Kevin Dangoor’s blog.

So what do you think of this revisitation?  I think it is poppycock.

He says “Today everyone is accustomed to buying software the way we buy burgers. We’re accustomed to acquiring the right to use software bundled with the bits.”  But this isn’t the way most software is bought.  Software today comes with that highly restrictive EULA which says that you can only do with this software, the things that we say you can.

Mr. Cox then goes on to suggest a heirarchy of charge-for services which filter up to usable applications.  My problem is that I cannot fathom what kinds of services these are.  And with all the wonderful open source software and free services available today, what services could be sold?  Now I’m aware that this type of activity is already happening in some niche markets, but as these niche markets become more mainstream and less niche, a “free” version enters.  e.g.  we now have free versions of photo hosting from flickr and picasa web.

I think that the past 11 years have showed that the industry has gone in a different direction than Mr. Cox envisioned.  I think this shows that the fundaments of Frederick P. Brooks, Jr.’s “No Silver Bullet” hold true.

Thanks for the link Kevin.  I enjoyed the reading.

powered by performancing firefox

Clarifying things I wrote in the previous post.

Eric IMed me and said he couldn’t post.

Thanks for the heads up on the ssh connection sharing, I’ll definitely be checking that out.

I can’t tell if you’re implying that interfaces are somehow the same as base classes, but they definitely are not. They’re not the same as abstract base classes either.

On semi-related notes, you should check out the “open closed” principle of OO design, if you haven’t already, and the “fragile base class” problem. It sounds like you’ve hit on half of the issues those two topics cover, so they might seem like covering ground you’ve already been over, but you might find a tidbit or two that you find interesting.

I meant to say that C++ doesn’t have “interfaces” although there is nothing preventing one from making an abstract base class and using it as interface, in fact that was/is the C++ way. That is all I meant. Which is why I mentioned COM/MIDL because MIDL was created exactly to define an interface in a language which doesn’t explicitly have interfaces. I fondly remember writing MIDL for my COM interfaces, but I prefer a language which allows me to express this though the use of an interface in the language, like C# or Java (or VB.net or any of the .net languages).

I liked looking up both open-closed and fragile base problems. The open-closed thing is easy to handle IMO. I’d like to think the things which I’ve written handle it well enough. The fragile base problem I can’t say I entirely agree with. I understand the problem, but I can’t say that interface instead of implementation inheritence is the best solution. That is what VB6 and COM had, and believe me, there are many of us who really missed the implementation inheritance.

That said, I’ve usually found myself defining interfaces, implementing an abstract class, and then inheriting some implementation from the abstract class. I prefer the lazy programmers approach here. Don’t require programmers(or yourself) to reimplment things simply because you are afraid of what they may do wrong if they implement or override a function incorrectly. I argue that you can’t really solve this much more even with interfaces (short of sealing classes). Of course I’ll have to admit that my experience in this regard is limited. My approach is to define the interface, and then implement it as an abstract class. This abstract class does all the things which I expect all inheritors of the interface to do and it does any helpful nice things that I may expect implementors to need. That is it. Am I missing some piece of the fragile base class puzzle?