Archive for the ‘programming’ Category

The C++ riddle

Thursday, June 24th, 2010

C++ is sometimes even more cryptic than regular expressions. Can you figure out what this one means?

%016I64lx

Amazing customer feedback…

Monday, March 15th, 2010

For one of my projects I have implement a system that handles crashes, reports technical details to me and asks the customer “which program function (hotkey or name in the menu) did you just use?

So far, the answers to this question have often been amazingly useless. For example one customer just wrote: “CRASHED AGAIN! no obvious reason…“. The data shows however that the customer was right in the middle of importing a project.

Yeah, no obvious reason. Customers are only humans, I guess. And some of them more than others…

Weird problem with AfxWinMain()

Wednesday, January 27th, 2010

I spent the last hours hunting down a weird crash problem in AfxWinMain(). It’s the init function in an MFC application that gets called before anything else.

And it crashes at “if (!pThread->InitInstance())” because AfxGetThread() returned a NULL pointer.

The problem was paired with link errors that vanished when the buid was repeated. Since this is “normal” with MS C++ (I’ve seen that problem often before), I didn’t really notice it.

As it turns out the entire code was correct. I imported it into a new project and it worked. Comparing the the vcproj-Files I found the line that made all the difference. Removing this line suddenly fixed my problem:

“ObjectFile=”.\DebugD64″

(in Configuration > Tool Name=”VCCLCompilerTool”).

JavaScript MessageBeep

Tuesday, December 15th, 2009

For quite some time I tried to find something like MessageBeep in JavaScript to attract the user’s attention for a change on the screen. JavaScript doesn’t have anything similar.

But I found a small flash player called niftyplayer that can play mp3 sounds, stay invisible and be controlled from javascript. To get the MessageBeep effect, simply install the player, set width and height to 0 and call niftyplayer(‘niftyPlayer1’).play();

(Thanks to pragma for pointing me in the right direction.)

Using Google to protect your site

Sunday, November 29th, 2009

Is your website error free?

My site  has grown over the years and uses a lot of PHP code. It’s difficult to test each and every aspect of it. Here are 3 simple tricks that can help you test your website. They don’t replace a real test (that you should do after a each change), but they’re a nice addition.

Trick 1:

Use Google to search your website for “error” or “warning”. You can use the format:
site:yourdomain.com (warning or error )

This of course has a terrible lag, since Google crawls your website only every few days or weeks, but on the other hand Google may use different parameters or aspects that you use in your own tests.

The real fun comes with combination of trick 2.

Trick 2:

Use Google alerts to automatically search your website for problems. You can create a free Google alerts account and use the search mentioned above to have Google automatically check your site every day. If something breaks, Google will send you an e-mail.

Trick 3:

Same as above, but use your Google to watch your site for hacked pages. A hacker might have exploited a weakness in your server and created a new page for whatever product he promotes with his spam mails. You probably don’t want to become involved with this. So instead of using “warning or error” in the search. use a list of the usual phrases and medical products that the spam mails usually contain.

It’s a one-time work of a few minutes. The rest of the work is done automatically by Google and can save you a lot of trouble.

Using usertype.dat as spell checker

Sunday, November 15th, 2009

There are thousands of classes, structure, functions and constants in the Windows library and it’s often hard to remember the right spelling. Here’s a simple trick to use the usertype.dat as a very simple spell checker.

Usertype.dat?
First, usertype.dat is a file which can be configured in the MS IDE (see help file, because it differs with the versions). Simply activate it and use my usertype.dat as a start. It contains almost 4000 key words from the libraries.

When you now type one of these key words right, it will be displayed in a different color (I prefer gray – to emphasize that I don’t have to pay attention here). Here’s how it can look like.

Example:

Note that all the common things like HICON, LoadImage or LR_DEFAULTCOLOR are written in gray because they’re spelled correct. And other words (variables in this case) like hIcon and IconId1 a printed in white because they’re not predefined. If I had written Load_Image (instead of LoadImage), it would have been printed in white, too, and I know that it might be incorrect.

Dangerous QuickSort

Friday, May 15th, 2009

When it comes to sorting, QuickSort is an all-time-favourite (so I though) and I chose it for implementing a sortable list with data. In some cases (when there were on few changes), it didn’t perform well, to I switched to an old-fashioned bubble-sort in such cases.

What I had to learn now, is that if the data that has to be sorted, is arranged in the right / unfortunate way, then QuickSort can easily crash your program by consuming excessively stack space and finally causing a stack overflow. You won’t need much data for this. 10,000 items is enough to perform terribly slow and finally crashing the software.

I switched to IntroSort, which seams to work better (so far) and (according to Wikipedia) behaves better in worst-scenarios than QuickSort. We’ll see. Sample implementations for IntroSort are rare, but I found one linked in this blog.

Top 25 most dangerous programming errors

Tuesday, January 13th, 2009

Experts from more than 30 cyber security organizations have published a list of the 25 most dangerous programming errors.  Among then the famous “SQL injection” and other only slighty less famous ones.

Although every programmer should know most of these, it’s still a good summary and a nice reminder on how to code.

Here’s the list.

Tips for minimizing your app to the Tray

Friday, November 28th, 2008

It’s a nice feature for your software if your customer can minimize it as an icon to the system tray. From our “minimize to tray” program I learned a few things about. Here’s a rundown of the basic and not-so-basic tricks.

  1. You can add / modify / remove a tray icon with Shell_NotifyIcon. You can specify a callback message (for example WM_USER) to react to left/right clicks on the icon. To hide your program when minimized simply use ShowWindow.
  2. To minimize to the tray when your user click the minimize button, handle the WM_NCLBUTTONDOWN message and watch for the HitTest HTMINBUTTON (or HTCLOSE if you want to override the X-button).
  3. If you want to prevent Windows from hiding the icon, make a tiny chnge to the icon every now and then. (Do this only if you have good reason!)
  4. You can use DrawAnimatedRects for a minmize animation. Use FindWindow(“Shell_TrayWnd”, …) to find the location (= target reactangle for the animation)
  5. If the windows explorer crashes, the tray icons are gone and your user can’t get your application back. To detect this, use Shell_NotifyIcon(NIM_MODIFY, … every now and then and check the return value. If the function fails, then your icon is gone and you have to add in again.
  6. Don’t forget to remove the tray icon if your program closes while being minimized.

The instant before you get used to it

Wednesday, October 22nd, 2008

Some years ago I wrote a computer game that had a really nice effect for displaying the menus. It looked really cool, but it took several seconds until the menu text was displayed properly. Since I knew the menus by heart, I clicked even before it was readable and could navigate the menus efficiently. I didn’t even notice that there was a problem until I’d shown the program to friend and he’d explained to me what I was doing.

I had gotten used to it and didn’t see the problem any more.

And I think that this is a common problem when you’re developing a program. In the end you know it so well, that you’re not noticing any more when anything isn’t solved properly. This applies mostly to the user interface, but it can also apply to programming and performance. The best way for this is to have somebody elso use the program and just sit next to him. Don’t explain. Just watch. You’ll be surprised…

However, sometimes there’s an easier way.

Currently I’m developing a new software and I noticed that there is a short time span before you get used to a bad solution. I noticed several times that I clicked at the wrong location, that I thought a dialog wasn’t easy to understand or that it would be great if the user could do this or that here (for example drag a file into the dialog instead of entering the path). For my new software (for a change) I try to note these thoughts and improve the program right away, hopefully creating a better first version.

So, if you’re creating a new software, notice your own problems and thoughts. And fix things before you get used to it.