« August 2008 | Main | November 2008 »

October 31, 2008

Sloppy sizeof Programming (Variation #2)

This is another variation on my previous sloppy programming post.

When something potentially dangerous is displayed on TV then we hear them say "Do try this at home".

Well for this kind of sloppy programming I say "Don't try this at work!".
Only try it at home, where you can merrily shoot yourself (and just yourself) in the foot as much as your heart desires, leaving the rest of the world in blissful peace.

Continue reading "Sloppy sizeof Programming (Variation #2)" »

October 30, 2008

Digital Misfortunes: Remote desktop messes up keyboard layout of logon screen

It seems that lately I have been having a peak of digital troubles, which I have come to call "Digital Misfortunes". These are situations where programs or computers do not behave in the expected way and I have to utilize the "supernatural" powers that device driver development has given me in order to solve these problems.

So, since these things seem to happen more and more often I have created a new category in my blog, with that name exactly.

It really makes me sad to see that more and more problems keep coming up. What would a normal user do? Format and reinstall the OS? Most likely...

Continue reading "Digital Misfortunes: Remote desktop messes up keyboard layout of logon screen" »

October 28, 2008

Common Sense Quiz: Traffic patterns on my blog

Now this is a really tough one...

I was checking my blog statistics as usual and all of a sudden noticed that there was a pattern that I just couldn't explain... No way...

Why is it that my stats skyrocket (double the average value) every Monday and Friday?

Really mindblogging :-D

Have fun,

Dimitris Staikos

October 24, 2008

How violating KISS can end up in 100% CPU usage by your antivirus

Hi all,

I have been using an antivirus/firewall product on 4 of my computers, 2 desktops and 2 laptops, for more than a year without any troubles and was more than happy for the money I paid for it.

However at some moment I noticed that the my IBM ThinkPad X41 was extremely slow. Process Explorer would show CPU utilization being constantly at 100% and the culprit was the executable of the antivirus software.

If I disabled the antivirus real-time protection then CPU went down to normal. When I reenabled it then back to 100%.

To start with I left the laptop open for a couple of days so maybe the antivirus would get done with something it was doing but of course that was a silly thought without results. Then I tried to uninstall and reinstall it but this was just another silly thought and of course the problem remained.

So I decided to use the artillery. I opened up Process Monitor by SysInternals just to take a look at what the antivirus was doing.

Suprise!

It was scanning over and over a 9MB html file!

The location of this file?
C:\Program Files\ThinkPad\ConnectUtilities and the name of it AddConnAdvanced.html

So I added this file to the exclusion list and my laptop became normal again.

I thought maybe my laptop downloaded some software update that screwed things up, so I decided to look at what could a 9MB html file possibly hold...

Surprise (again)!

It was the IBM diagmostics utility debug log...

Then I remembered... I had a problem with the wifi on the laptop about two months ago. So I turned on the debug diagnostics and of course forgot it on even after I solved the problem.

It seems that the IBM developers wanted to appear slick I guess, so a .TXT file was not good enough for them but instead they output an HTML file which eventually gets huge, it is an HTML file so it gets scanned by adivirus software because it might be malicious, ending up in a frustrated user >:( .
Bravo! Way to go!

I do device driver development for a living and as a result I have a "lean and mean" mentality, some also call it KISS (Keep It Simple Stupid). I just hate it when people use HTML/XML for something that could be done with a plain txt, or use .NET for a config utility because they want to add a silly jpg on the dialog and don't know how to do this in plain Win32.

Things are simple: When you do more than you absolutely have to, then you increase your application's "problem surface". More and more things can go wrong and you DON'T want things to go wrong with low level stuff like debug logs and config utilities.

To understand the extent of this problem, that is doing more than you have to just because it is cool, be amused to know that some PhD guys that obviously hadn't do much programming in their lives, designed a camera specification called Genicam, and designed a feature by which the camera could send to the application a file that describes in a standard format all of its settings. This "standard" format of course was XML and everybody was so happy and cool. Of course the time came to write a device driver for such a camera and guess what? How on earth do you fit an XML parser in kernel mode code? Only if you want to play cowboy and device driver developers usually don't have much free time to play cowboy. Using XML, which is user mode crap, for some simple text information that NEVER changes is against KISS.

Dude, do I hear someone saying "but what about schema validation etc"??? We are not talking about a file that got created by a user. We are talking about a file that gets stored inside a camera by the manufacturer. IT BETTER BE IN THE CORRECT FORMAT.

Anyway, although the antivirus is not to blame for my troublesome situation, but it would help if they provided some statistics screen/report with the most scanned files so we can solve such problems in an easier way, without using super natural powers.


Tip: If a similar thing happens to your PC and it is so slow that it is almost unusable because a process is taking 100% CPU and you don't want to kill the process (so that you can study what is going on) then try lowering the process priority of the offending process. This will permit you to use your computer again so that you can solve the problem.

Have fun,
Dimitris Staikos

October 16, 2008

Superb Vista Tip

Well, although my overall opinion for Vista is kind of on the positive side, there are far too many things that I consider just BROKEN. One of those areas is the silly USB stick.

Before Vista, I used to plug it in, do my file copying with explorer, then do a safe removal and sure enough all explorer windows that were on that drive would close and the drive jet removed. PLUS the lights went OFF :-)

For some reason, God knows what, but I am sure that it has to be seriously valid at least technically, they decided to ruin the user experience and have us first close ALL explorer windows and then remove the drive.

Cool so far. And I am pretty certain many of you have seen the following dreaded dialog:

Image1_2

I am bloody sure that they could make it a BIT more useful and tell us WHICH programs are using the device, but then I guess this would probably require admin priviledges, so they didn't do it.
Anyway, on the message I particularly enjoy the "try again LATER" part. LATER? How much later? Like in a minute or so? I have actually seen in some other program an error message saying "try again in 1 minute" (I think it was Google Earth) and I found that even scarier, so I guess "later" is not that bad. Of course, they could just say "try again".

Anyway, I surely am not as wise as the Vista designers, so I should better keep my big mouth shut before someone sues me or my wife divorces me :-D

So here is the ULTRA tip, which comes straight from the horse's mouth! An MS developer in the Vista/Windows 7 "Devices Team".

When the dreaded dialog appears, then Vista does us all a big favour and adds entries to the event log, reporting which apps are using the device and preventing it from getting safely removed!!!
Oh yes...

All you have to do is open up event log viewer (Run ==> eventvwr.msc), go to Custom Views | Administrative Events and here's what you get (click on image to view in full size):

Image2

Yeap, it's explorer.exe that is preventing the device removal. In my case I also have a command prompt on that drive, so I had a second message as well for CMD.EXE. You will get one message for every process.

Note that the message even lists the process id, so that if you have many of the kind open you can easily locate which ones is causing the trouble. Just locate the pid in Task Manager and do a "switch to...". All really user friendly now, isn't it?

However, sometimes the event log message will report that the SYSTEM process is preventing device removal. In that case, there's nothing you can do other than:
(a) Try Again LATER and LATER and LATER until it gets removed (it eventually does...) OR
(b) To hell with the error message, just unplug the stick and hope for the best!

Now for those of you who like to do things the hardcore way, there is also a command line way to figure things out. First you must download the HANDLE tool from SYSINTERNALS.

Then open up a command shell and enter: HANDLE -a HardDiskVolume

You will get an output that looks like this (in my case I have two USB sticks attached):

Image4

Here I can see that the stick I am insterested in is actually HardDiskVolume7, so I can refine my search a little more to clean up the messy output.

If you feel kind of adventurous the you can try the -c option of HANDLE.EXE and close some handles in a really friendly way.

Have fun!
Dimitris Staikos

Vista Troubleshooting Tip (Windows Mail + SPA + SSL)

You might find this Vista tip useful from George Adamopoulos' blog.

Have Fun!
Dimitris Staikos

October 10, 2008

Batch file tips and horrors

Dude, it seems that some things are made by sadists...
It can't be explained otherwise...

Before explaining the horrors I will give you a tip that I think you will find useful.
Typing the following line in a BAT file:

    SET /p VAR=Please enter value:

Will give you a sweet prompt so that you can enter the value to be used in the batch file!!!
Wow64!!!
I've been looking for this for years! And it has been there in the friggin' help for SET who knows for how long.

Moreover SET has the /a switch that can evaluate expressions which means that you can use it to create simplistic loops in a batch file, with a statement like this:

    SET /a VAR=%VAR%+1

And as a bonus if you do SET /a 5*4 on the command line you get to use the shell as a quick calculator for integer arithmetic :-)


Now let's go to the horrors...
Take a look at this trivial batch file:
@ECHO OFF
SET /p VAR=Give me VAR value:
echo VAR=%VAR%
if EXIST C:\nul (
    SET VAR=C:\%VAR%
    echo VAR=%VAR%
)
echo VAR=%VAR%

The if condition is made so that it succeeds (for most people).
If you enter 123 at the prompt, what do you expect to get printed?

VAR=123
VAR=C:\123
VAR=C:\123

Right?

Wrong... And here comes the horror!
The batch file outputs the following:

VAR=123
VAR=123
VAR=C:\123

What the heck???
To cut a long story short, the SET statement inside the IF actually takes place, but its results are NOT visible inside the IF!!! Instead the previous value is used throught the statements within the if.
I tried several things to make a SET statement that takes place within an IF to make its results visible to the rest of the statements. Be my guest. It seems however that the interpreter is reading and translating all the statements inside the IF in a single operation, so any updated variables do not get reflected to the code.

For the time being right the code like this:

@ECHO OFF
SET /p VAR=Give me VAR value:
echo VAR=%VAR%
if not EXIST C:\nul goto AFTER_IF
    SET VAR=C:\%VAR%
    echo VAR=%VAR%

:AFTER_IF
echo VAR=%VAR%

Enjoy!
Dimitris Staikos