Ive dug up my old code from few years back when I was writing stuff like this.
Source code is in Delphi 6 so you may not be able to compile it.
However, the code is quite well commented and may interest you
All usefull code is in *.pas files. Read with notepad or similar.