Saturday, August 30, 2008

Writing game in Java

I was curious about what it takes to write a game program. I came across the following website which provides me enough information to get started. The website contains a complete draft of Killer Game Programming in Java. It contains enough information to understand what it takes to write a game. A few key things it mentioned was:
  • Controlling the image refresh (that's why you need to write your own graphic refresh mechanism instead of using the built in mechanism) - this is also known as sprite.
  • Difference between writing in full screen mode vs windows mode.
  • Discussion on animation, 3D etc.

I found the draft enough to sooth my appetite but if you want a more updated info then you can get the book latest revision of the book. It has been revised several times since the draft. Here is the link to the book in Amazon:
Killer Game Programming in Java

Tuesday, August 26, 2008

Batch File command script

One of the main strength of Windows is its GUI interface but we sometimes overlook the command line scripting capability. Microsoft has added many utilities. To get the list, just type help at the command prompt:
For more information on a specific command, type HELP command-name
ASSOC Displays or modifies file extension associations.
AT Schedules commands and programs to run on a computer.
ATTRIB Displays or changes file attributes.
BREAK Sets or clears extended CTRL+C checking.
CACLS Displays or modifies access control lists (ACLs) of files.
CALL Calls one batch program from another.
CD Displays the name of or changes the current directory.
CHCP Displays or sets the active code page number.
CHDIR Displays the name of or changes the current directory.
CHKDSK Checks a disk and displays a status report.
CHKNTFS Displays or modifies the checking of disk at boot time.
CLS Clears the screen.
CMD Starts a new instance of the Windows command interpreter.
COLOR Sets the default console foreground and background colors.
COMP Compares the contents of two files or sets of files.
COMPACT Displays or alters the compression of files on NTFS partitions.
CONVERT Converts FAT volumes to NTFS. You cannot convert the
current drive.
COPY Copies one or more files to another location.
DATE Displays or sets the date.
DEL Deletes one or more files.
DIR Displays a list of files and subdirectories in a directory.
DISKCOMP Compares the contents of two floppy disks.
DISKCOPY Copies the contents of one floppy disk to another.
DOSKEY Edits command lines, recalls Windows commands, and creates macros.
ECHO Displays messages, or turns command echoing on or off.
ENDLOCAL Ends localization of environment changes in a batch file.
ERASE Deletes one or more files.
EXIT Quits the CMD.EXE program (command interpreter).
FC Compares two files or sets of files, and displays the differences
between them.
FIND Searches for a text string in a file or files.
FINDSTR Searches for strings in files.
FOR Runs a specified command for each file in a set of files.
FORMAT Formats a disk for use with Windows.
FTYPE Displays or modifies file types used in file extension associations.
GOTO Directs the Windows command interpreter to a labeled line in a
batch program.
GRAFTABL Enables Windows to display an extended character set in graphics
HELP Provides Help information for Windows commands.
IF Performs conditional processing in batch programs.
LABEL Creates, changes, or deletes the volume label of a disk.
MD Creates a directory.
MKDIR Creates a directory.
MODE Configures a system device.
MORE Displays output one screen at a time.
MOVE Moves one or more files from one directory to another directory.
PATH Displays or sets a search path for executable files.
PAUSE Suspends processing of a batch file and displays a message.
POPD Restores the previous value of the current directory saved by PUSHD.
PRINT Prints a text file.
PROMPT Changes the Windows command prompt.
PUSHD Saves the current directory then changes it.
RD Removes a directory.
RECOVER Recovers readable information from a bad or defective disk.
REM Records comments (remarks) in batch files or CONFIG.SYS.
REN Renames a file or files.
RENAME Renames a file or files.
REPLACE Replaces files.
RMDIR Removes a directory.
SET Displays, sets, or removes Windows environment variables.
SETLOCAL Begins localization of environment changes in a batch file.
SHIFT Shifts the position of replaceable parameters in batch files.
SORT Sorts input.
START Starts a separate window to run a specified program or command.
SUBST Associates a path with a drive letter.
TIME Displays or sets the system time.
TITLE Sets the window title for a CMD.EXE session.
TREE Graphically displays the directory structure of a drive or path.
TYPE Displays the contents of a text file.
VER Displays the Windows version.
VERIFY Tells Windows whether to verify that your files are written
correctly to a disk.
VOL Displays a disk volume label and serial number.
XCOPY Copies files and directory trees.

In addition to these commands, you can also use vbscript to add to things that you cannot automate. Batch commands will need to reside in files with either .cmd (for either NT, or newer OS) or .bat extension and vbscript will need to reside in files with .vbs extension.

An example of the power of this is a script written by one of my colleague:

FOR /F "TOKENS=2-4 DELIMS=/- " %%F IN ('DATE/T') DO (

wzzip -yb -m "C:\archive\" C:\Data\*.txt

In the above example, she uses DATE/T to get the date and then extracts the Month, Day and year and concatenates to make the filename used as a parameter for the command line of winzip to zip out text files in the data folder.

Similarly, you can download blat and use it together with some other scripts to send out mails. Or you can use the ftp command to perform ftp.

One nice thing of scripting is you can use windows scheduler run it at fixed intervals.

Friday, August 22, 2008

JDBC Connection string for Oracle RAC

Oracle RAC is Oracle's clustering solution to provide high availability for the database. When using JDBC to connect, you need to change the jdbc connection string - found this here. This is a snippet from the article.

If you have to connect to oracle RAC (Real Application Cluster) using JDBC with thin driver, the classic url:
doesn’t work and you get the error ORA – 12505.
Instead, you must use this url:
(RETRIES = 180)
(DELAY = 5)

There is also an article discussing this here.

Thursday, August 21, 2008

Looping from A to Z

There are times we may want to loop by alphabet instead of number, this is how you can do it.

Public Sub loopAtoZ()
Dim ASCIINum, loopCnt As Integer

ASCIINum = AscW("A")

For loopCnt = 0 To 25
ASCIINum += 1

Catch ex As Exception
Throw New Exception("[loopAtoZ01]" & ex.Message)
End Try
End Sub

Tuesday, August 19, 2008

Ensuring all the threads in Lotus NOTES are terminated

Many times when Lotus NOTES terminates abnormally, I am unable to restart it and try to use the task manager and guess which are the processes to kill. This sometimes works but most of the time I end up having to reboot my windows.

A more efficient way is to just use the NSD (Notes System Diagnostic) utility. NSD.exe can be located in the lotus notes program folder. To ensure all the just type nsd -kill. This will kill all the processes related to Lotus NOTES and you should be able to restart Lotus NOTES without rebooting your PC.

Thursday, August 14, 2008

Querying LDAP using .Net

To query LDAP using .Net, use System.DirectoryServices library.

Quickest way is as follows:

Dim LDAPEntry As DirectoryEntry
Dim LDAPStr as String = "ldap://,,o=SDS"

LDAPStr = "LDAP://" & LDAPSrv & LDAPPostFix
LDAPEntry = New DirectoryEntry(LDAPStr)

Dim srch As New DirectorySearcher(LDAPEntry)
srch.Filter = "uid=1234"

Dim rslt As SearchResult
rslt = srch.FindOne
Dim tmpStr as String
Dim tmpObj as Object

Dim rsltPropCol As ResultPropertyCollection = rslt.Properties
For Each keyStr In rsltPropCol.PropertyNames
For Each tmpObj In rslt.Properties(keyStr)
'This loop is there because in LDAP multiple entry is
'permitted for the same key.
'Also any object are permitted some of which cannot be
'converted into string
keyVal = Replace(CStr(tmpObj), "'", "")
Catch ex As System.Exception
keyVal = ""
End Try
tmpStr = String.Format("key is {0}, value is {1}", keyStr, keyVal)

In the example above, the LDAPStr is made up, you need to refer to the one use by your company. The same is the filter.

One of the key things that is important is that the values can be object of any kind so you need to be careful when retrieving it and then assigning to the variables.

If you don't specify properties to load, it will return all the fields same as you do a select * in SQL. If you want to restrict the fields it returns then specify it before you call the FindOne method:

It is possible to use wildcard. In LDAP, "*" is the wildcard.

FindOne will return only one result. If you want more than one, then use FindAll as in:

Dim rsltList As SearchResultCollection
rsltList = srch.FindAll

You can then loop thru the collection as in

For each rslt in rsltList


You can also use LDAP for authentication. When you set the Directory.AuthenticationType.

For example, when you set the following:

LDAPEntry.AuthenticationType = AuthenticationTypes.ReadonlyServer

Then you need to have the username and password prior to calling FindOne or FindAll as in:

LDAPEntry.Username = "uid=" & uid & ",ou=people,,o=sds"
LDAPEntry.Password = pwd

If you just want to query, just set the authenticationType to none. You can see all the other enumerations here.

Saturday, August 9, 2008

Specifying how the enter key will act on your page

When writing application, we normally will write handlers for buttons events or other events but not for the enter or submit event. This becomes an issue because when your user hits enter when visiting your page, two things could happen:
  • Nothing
  • It will trigger the first button the page encounters (which may not be the button event you want to trigger).
One way is to register the __EVENTTARGET to point to the submit button as shown in the following article. Unfortunately, this approach does not work with web control button.

Another approach is to use javascript to do that.

The java script is as follows:

 function clickButton(e, buttonid){ 
var bt = document.getElementById(buttonid);
if (typeof bt == 'object'){
if (e.keyCode == 13){;
return false;
if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){
if (event.keyCode == 13){;
return false;

Then register the textbox you want to link to the javascript as follows in the codebehind:

txtLotID.Attributes.Add("onkeypress", "return clickButton(event,'" + btnLotID.ClientID + "')")

This is useful for a web page for performing stock take where you use a scanner to scan the lots or serial number into the textbox and the scanner automatically performs enter.

Wednesday, August 6, 2008

Implementing Security in .Net

The best guide I found to help explain how to secure your data in .Net is found in the C# cookbook printed by O'Reilly. There is one whole chapter on this topic and it goes into details on the encryption library that is built into .Net and going through step by step on addressing some of the possible hacking that is done and tricks to avoid them.

The chapter in PDF form from Release 2 can be found here. I found several other sources but each of them require you to register.

Tuesday, August 5, 2008

Enabling SMTP on Lotus NOTES server

By enabling SMTP on the Lotus NOTES, you allow the Lotus NOTES to route SMTP mail from your application such as any of your .Net application.

First put in a fully qualified internet name on the server document. Assuming the server name is mr and the mail domain we want to route is, you can put as shown below:

Next enable mail routing (if it is not already done). Enable TCP/IP (it may differ if your Lotus NOTES network is named differently).

Then just get your router to refresh its config by typing the following command at the console:

Tell Router Config Update

Now your notes server can act as a SMTP server for your application

Friday, August 1, 2008

.NET Application Updater Component

For those who are writing Java application, you can use Java WebStart to create rich client which allows automatic update of the application. The equivalent of this for .Net Framework is called .NET Application Updater Component.

Details of the component can be found here.

Prior to .Net all deployment needs to update registry, the following articles talk about simplifying the deployment. In fact, you can use both concepts together.

I tried the Application Updater and found it to be very easy with one caution:

Don't use DownloadOnDetection. I found that when you do that it keeps detecting and downloading the code even if I did not change the application version.

I used Method 3 for the deployment and it works well with our users. Here is a snippet describing it.

The final approach used to perform the application update was inspired by the side-by-side assembly model of the .NET Framework. Instead of trying to update the application itself, create a new upto-date version of the application next to the existing version. The new version can be created by merging the existing application directory with the downloaded update. When the new version is complete, the user will automatically use that version the next time they launch the application. The original copy of the application can then be removed. This tricky part is figuring out which version to launch at any given time. To do that, a program named Appstart is introduced. Appstart is the entry point into your application. With this model, your application directory will look something like this:

Program Files




V1 Folder


V1.1 Folder


To run your application, you always run Appstart.exe. If you want a shortcut on the desktop, the shortcut should point at Appstart, not at the application directly (Note that you can rename AppStart.exe to be whatever you want, ex. YourApp.exe). Appstart.exe is a very simple program that reads the Appstart.config file and launches the specified application. A valid Appstart.config file looks like the following:


<AppFolderName>V1 Folder</AppFolderName>




  • Whenever the user starts using the program, it will go to the webserver to see if there is a new version of my application. If there is, it will download and then he will start using the new version.
  • My application contains multiple assemblies (.dll files). If the user deletes them, the updater component will automatically get a new copy from the webserver.
I found the easiest way to deploy it is to first go to the users workstation to set it up. This is because I want to make sure the user launches the application from the launcher and not the assembly (.exe file) directly so that the updater will work. The application needs to be launched through the AppStart.exe. (Just as you use JavaWebStart to start your Java Web Start applications).