Dan Byström’s Bwain

Blog without an interesting name

Archive for September, 2004

Choices Rule (pt I)

Posted by Dan Byström on September 20, 2004


I will pick up the thread from where my previous blog ended, but not by continuing with more C++ or .NET. Last time, I tackled a problem in a somewhat disturbing way – by solving it according to familiar rules instead of a new and probably more interesting way. Not at all the usual me, I hope.

I’m not sure if you’re familiar with the concept of Choice-based vs. Rule-based people. It was a complete mindblower when I first read it some 15 years ago. It was pinned on our notice board at work so I kept going through it in my mind whenever I saw it. While the article text may take some time to settle, it will then, among other things, explain the strange fact that some people have absolutely no interest in knowing why (you know the "we have always done it in this way!" reply). You may also have wondered why your users don’t always jump up and down in excitement over your latest radical improvements of your software. I’ll refrain from extending this list of examples and from telling you about real life happenings although it’s tempting. Discover this on your own. The gentleman hosting this article has, by the way, done his own thinking. Someone taking the time to explain to people the beauty of Fibonacci numbers in nature may be worth reading.

Looking at my quotation of Jimmy Nilsson in my previous blog, you can immediately tell that he’s a choice-based person. In fact, all decent programmers MUST be, right?

I’ve been thinking long and hard about this topic and I’ve now begun to question that people can be divided into just being rule-based or choice-based. I’m convinced there is a whole spectrum in between. Not only that; I also think that the same person can behave differently depending on a given situation: being rule-based in some circumstances as well as choice-based in others. I’ll outline my reasoning in a later blog where I will argue that this spectrum can be written as a function of three variables ("personal abilities") for a given situation.

  In this world, there are two kinds of people:
Those who divide people into two categories and those who don’t.
— unknown

It was once said to me that if a scientist and an engineer both walk a path and encounter a large boulder blocking their way, then the engineer will try to find a way around the boulder while the scientist will try to find a way through the boulder.

While not a striking analogy, it is still easy to understand. While my sympathies go to the scientist, I must confess myself to the engineering group. I have discovered that I actually work best under constraints – trying to find ways to bend them to my own advantage. That’s what most programmers are up to each day, I guess. We try to find ways around limitations like too little memory, too little screen estate, algorithms that won’t run fast enough and most of all, limitations in the tools and languages we use. The better we can bend these rules, the better programs we may write. That’s an engineer’s point of view. The scientist on the other hand is busy removing the constraints altogether.

Is it rule-based or choice-based to play by the given rules, when you come up with ingenious new solutions to trick your way around those constraints/rules?

(to be continued…)

Posted in Uncategorized | Leave a Comment »

SID and Minimum Amount of Work

Posted by Dan Byström on September 6, 2004

SID means Security IDentifier, although this blog really is about integrating C/C++ code into your .NET project, as well as the pros and cons of being lazy.

My old and dear friend Lars Wirsén is struggling with a Visual Basic .NET program to install a database on a Microsoft SQL Server, or if no one is present, install a Microsoft Desktop Engine (MSDE) on the local machine first. He is the one who taught me the art of computer programming some 22 years ago. While I was struggling with 6502 assembly language for my VIC-20, trying to understand how the carry flag worked, he on the other hand was able to build his own 8kb memory cartridge with a switch on it so that its content could be moved around in memory even as the computer was running. This made it possible for him to make copies of the game ROM cartridges you could buy for that machine. I still haven’t figured out how he did it.

Time changes and software has become as complex as hardware. Apparently, he has discovered that the user needs certain access privileges in order to run his program and wants to check if the user is member of “BUILTIN\Users”. Fine.

The problem is that group names are localized! On a Swedish Windows, it is called “BUILTIN\Användare” instead. Of course, if he just supported English, Swedish and Klingon, then all major languages would be covered for.

I’ve told him 1024 times that once you start to use a database you’re committed to a never ending series of trouble and that you shouldn’t use them at all. Databases – just say NO!

Nevertheless – I thought to myself, how hard can it be to dig up the localized string from Windows if we just ask politely? I know almost nothing of security issues. I, as most developers, run my machines with full administrator privileges. Philip Nelson has wise things to say about this, but this is a story about laziness (the power that makes the world go around) so I’ll continue to run as admin that for a little while longer.

I browsed the Net as well as MSDN for a little while and got confused. There were some huge code examples demonstrating things I thought would be useful. However, I just didn’t feel like digging into this field right then. I had neither the time nor the interest at the moment. I just wanted to get the whole thing solved as quickly as possible. The LookupAccountSid Win32 API function seemed like a good start. But how do you construct a SID to pass to it? The answer seemed to be buried in just too much code. Then I found a nice MSDN article from 1996 with full C code that seemed quite promising (My. Eight years old stuff which I knew null of!). I pasted the whole thing into an empty Visual C++ project and… Voila! It worked like a charm! It used something called RID instead of SID and apparently uses this RID (which is a 32 bit integer by the way) to find the desired SID. Fine by me – I didn’t care what made it tick.

Well then, since the code was running just fine – why not use it just as it was? I created an empty Managed C++ project in Visual Studio .NET, added an unmanaged class and pasted the C code in there. That’s should be almost it I thought. Well, it almost was. But I was surprised to discover just how many small pitfalls there were along the way! Before I had a running solution with a Visual Basic .NET front-end to this Managed C++ DLL, over two hours had passed!

So, I thought I post the whole solution here so you can take a peek in case you need to do the same one day. It is both a Managed C++ DLL with source code as well as a Visual Basic .NET demo project utilizing the DLL. Feel free to use my code as a boilerplate but don’t ask me anything about “SID.cpp”. That file is from MSDN and I still don’t know what a SID really is. But the job got done. Not as fast as I had hoped for, but the next time I write a Managed C++ wrapper I may still remember some of the things that went wrong this time!

So, did I get the job done with a minimal amount of work? Well, if I had converted the whole thing to Visual Basic .NET or C# directly, and written DLLImports for the API functions instead of using Managed C++ I would probably have made it in less than half the time.

One the other hand, I didn’t have to think much. All was familiar playing ground. If I had converted the code, then I would have had to actually understand it. It is just as when you wait for the slow, slow elevator to come down instead of taking the stairs and get up faster as well as gain a little exercise along the way. I must confess myself guilty of sloth – and try to make up for this for the rest of my life. Starting right now.

I sometimes have the privilege to get previews of articles by Jimmy Nilsson. In one exciting forthcoming text I found this:

Do you recognize the “I have a new tool, let’s really use it” syndrome? I do. I’ve been known to suffer from it from time to time. It can be problematic, but I like to think about it not just negatively, but actually a bit positively too. (Do you do that too? I mean, squeeze advantages into your bad habits?) After all, it could be a sign of productive curiosity, healthy progressive thinking and a never-ending appetite for improvements.

I really must pay more attention to what he says – before I forget why I began to write programs in the first place!

Here is the vdSID code sample along with a Visual Basic .NET test project. I have also added two totally irrelevant sample functions showing how to use inline assembly code in a .NET project as well as how you return an array of managed objects (Strings, in this case) from Managed C++ code.

(to be continued…)

Posted in Programming | Leave a Comment »