Programming

DateStamp – Sending Keystrokes to Another Application

This is a utility I wrote for a department at work to stamp a date and time for an annotation in an e-mail. The e-mail client we are using does not support this feature so we have to do it manually.

The program is interesting because it shows how to send keystrokes from a C# application to another program. I was trying to make the target application active to send the keys, but the command to make it’s document active requires the document name in it’s arguments (see below). I found it simpler to set the DateStamp program so it’s always the topmost window and when you click the Stamp button, it will send an Alt-Tab keystroke to switch to the previous application. The date and time are then sent as key presses. It’s not a very elegant solution but it works.

You can make an application active and send commands with the following statements:

Microsoft.VisualBasic.Interaction.AppActivate("Untitled - Notepad");
SendKeys.Send (“This is a test.”);

If you are using C#, you need to add a reference to Microsoft.VisualBasic in order to use the above AppActivate function.

The Send command sends the text as key presses to the application. You can also use the SendWait function which does the same thing but also waits for the message to be processed. If the commands from SendWait are not being processed properly, a slight time delay may need to be set between commands. This is a problem with the .NET Framework.

You can view the source code in the sourcedot.com forums and you can also download it here. The code is very simple and it can be modified for use in Visual Basic.

Scrollable CSS Regions

Frames were an exciting addition to web design when it was released. They would allow you to have fixed regions on your web site which is good for keeping ads or menus constantly visible. The nice thing about the fixed regions is that if there was too much information to display, you can scroll to view the entire content. The only problem is that web browsers rendered the frames differently so text tended to overflow out of the frame which then had to be viewed by using the scroll bars (if they were set to be visible).

The following code defines a CSS scrollable region:

... text ...


If you set the overflow property to auto, the content is clipped to the area defined by div and scroll bars are added if they are necessary. For text, you would normally get the vertical scroll bars and not the horizontal ones.

Setting the overflow property to scroll will clip the contents to the view area and make the horizontal and vertical scroll bars appear regardless of the content overflowing it.

Below is an example of a scrollable CSS region using the above code.

 

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi sit amet eros. Pellentesque ullamcorper dignissim dolor. Proin imperdiet lacinia nunc. Nam diam sapien, gravida sit amet, tincidunt ac, ornare vel, lacus. Nam vel eros ut ipsum mollis adipiscing. Etiam rhoncus sapien quis lorem. Proin dignissim mauris convallis leo. Suspendisse et ipsum vitae libero hendrerit porta. Aliquam congue auctor ipsum. Nulla lobortis. Etiam quis eros. Vivamus scelerisque, lacus sed aliquet placerat, sem mauris sollicitudin magna, id sodales ipsum nunc a pede.

Quisque aliquam elit et ligula. Nullam orci elit, pellentesque nec, sagittis eu, congue sed, orci. In nec ligula fringilla quam tincidunt elementum. Proin lacinia. Aenean vulputate, libero sit amet porttitor sollicitudin, diam turpis accumsan massa, et condimentum lectus quam eget enim. Nullam fermentum vestibulum sem. Aenean tempor mi sit amet lacus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Quisque ultrices purus nec neque. Ut dictum orci sed ligula. In hac habitasse platea dictumst. Quisque ultrices tristique pede.

 

This is a basic example and more can be done, but it should give you an idea what is possible with CSS. You don’t need to use frames to make scrollable areas on your web site.

Google Summer of Code 2007

Get paid for writing open source software! Once again, Google is hosting the Summer of Code (GoSoC). This is a program where student developers write code for an open source project over a 3 month period. The student will be paid $4500 and the mentoring organization will receive $500. Google will sponsor about 600 individuals for GoSoC.

The purpose of the Summer of Code is to encourage more developers to work on and publish open source code. It may also commit more developers to open source projects. The student is exposed to real world software development and the business behind it. This is something which is not taught in the classroom. Not to mention, it’s more interesting having a summer job writing code as opposed to working in some minimum wage job not related to your field of study.

Google is accepting applications for the Summer of Code until March 26. You can submit your application here.

Decompiling .NET Applications

Have you ever wondered how a program worked? If your not a programmer then you probably wouldn’t think of such things. A software developer, however, might. They would be interested in knowing how a feature or an entire application functioned in order to duplicate it in their own work.

A program can be examined by decompiling it and then studying it’s source code.

What decompiling does is that it translates a program from it’s native machine language (i.e. CPU instructions) to a format easier to read, such as assembly language.

This seems kind of scary. All of the hard work you put into your award winning application can be copied by someone else. But it’s not as easy as it seems.

For example, have you ever tried reading someone else’s source code? Even with comments and meaningful variables, it’s a challenge. Now try reading code with no comments and cryptic variable names. This is what you typically get after decompiling. Trying to interpret decompiled code is a difficult and time consuming task.

However, a decompiled .NET program is easier to read than a Windows native app. Visual Studio leaves the names of identifiers alone when compiling to an assembly. The result is still difflcult to understand but you can have a good idea what’s going on.

If you want to try decompiling a .NET program, you can download Reflector by Lutz Roeder. Be prepared for the surprise when you see how close the decompiled code is to your source code.

Fortunately, Visual Studio comes with a utility to obfuscate .NET programs, the Dotfuscator Community Edition. It will change the names of identifiers so that they will no longer be meaningful. There are also other commercial applications that will do the same thing.

If you want even more security for your code, a linker can be used. This will turn your .NET app into a native Windows program. The advantage of using a linker is that there won’t be the slow JIT precompile stage due to the app already being compiled.

Even with all of these precautions, your code is still not safe. A determined hacker can still decompile your program and figure out what it does. An obfuscation or linking tool may have them looking elsewhere for easier ways to copy code.

Stopping Spam on phpBB

I have forums that get thousand’s of page views per month. While the number of readers are increasing, so also are the spammers posting their ads. It got to the point that I had to switch the user registration to require the administrator to activate a new account. This can prevent the obvious spammers, i.e. those who use obscene names or add URL’s to unethical web sites.

The problem is that it’s hard to determine who is a legitimate user. Some people have crazy user names and e-mails so you can’t tell just by looking at what they enter.

After switching to admin authorization, the spam stopped but so did the user activity on the forums. I encountered this situation before and decided to go back to user authentication and delete spam as it was posted. I would spend about a half hour a day doing this, so it wasn’t such a great idea.

This blog uses Akismet to catch spam and it does so very effectively. Not one spam has been successfully posted here. I’ve tried various Akisment mods for phpBB but none of them worked. So the search for a solution continued.

After some time, I found a way to stop spam on Nadav Samet’s blog. His solution was very simple. When a new user registers on phpBB, they are asked a question that only a human can answer. Most spammers are actually automated scripts (or bots) scanning and posting messages. If you ask a question that requires intelligence, they will be unable to answer it.

The question that is asked is, what is 5+2? A spambot couldn’t answer that question. The script requires modifications of 2 files in phpBB. Its not very difficult finding the places to enter the code. The nice thing about this is that you can easily modify the question if spambots find a way to answer it.

This wouldn’t not stop a human spammer since most spam is automated. It needs to be because thousand’s of forums are being spammed. It would be too expensive to hire someone to spend time trying to bypass these tricky user registrations.

You can download the phpBB modification here.