Mega Code Archive

 
Categories / Delphi / Examples
 

Win9x services

This article demonstrates how to write a service for Win9X Services One of the most beneficial features on WinNT and Win2000 is the ability to run applications in the background, serving requests behind the scenes. It is a shame that Windows95/98 does not have this same feature, obviously it is because it was not meant to be a network server of any kind. However, sometimes it is necessary to have a task running as a service on Windows95/98, so the nice people at Microsoft decided to implement what I would consider "a hack". Write your application, in the project source do this. begin Application.Initialize; Application.ShowMainForm := False; //<-- Add this !! This will stop your main form appearing when your application is run. Now in the registry <HKEY_LOCAL_MACHINE> \Software\MicroSoft\Windows\CurrentVersion\Run Make a string entry pointing to your application. Finally, in the OnCreate of your form call InternalRegisterServiceProcess(True) The code for InternalRegisterServiceProcess is as follows. type TServiceFunc = function (aProcessID: DWord; aType: DWord): WordBool; stdcall; function InternalRegisterServiceProcess( const StartService : Boolean) : Boolean; var HKernel32: THandle; P: TServiceFunc; dwProcess: DWord; dwType: DWord; begin Result := False; HKernel32 := LoadLibrary('Kernel32.dll'); if HKernel32 > 0 then try P := GetProcAddress(HKernel32,'RegisterServiceProcess'); if Assigned(P) then begin dwProcess := GetCurrentProcessID; dwType := 0; case StartService of True : dwType := 1; False : dwType := 0; end; Result := P(dwProcess, dwType); end; finally FreeLibrary(HKernel32); end; end