(Trial Environment Delphi 6 Windows 2000)
In these days, I use Delphi to make a Windows Service module, which encounters a lot of problems. Greatly sorted up, share with you.
1. Create a Windows Service project (who can be skipped before the service program before)
Run the Delphi Menu File -> New -> Other -> Service Application Get a new project, saved.
Modify Service1's Name property for the name of the service you want to create, such as "MyFirstService".
Modify DisplayName, which is the name displayed in system service management.
2. Register a message string for the Event Viewer
Call TSERVICE.LOGMESSAGE (), it will be found that the information recorded in "Event Viewer" is always more unwanted:
The description of event ID (1) (in the resource (SMSservice) cannot be found. local
Computers may not have the necessary registration information or message DLL files to calculate from far end
Machine display message. Some events contain the following information:.
Windows uses the information registered in the .exe.dll to display information, but Borland seems to have no good tools.
Make this resource file, so you have to use the tool "mc.exe" in VC . Methods as below:
Write the following information in a text file and save it as mcmsgs.mc
[Code]
Languagenames = (Chinese = 0x0804: msg00804)
MessageID = 1
Language = Chinese
%1
.
MessageID = 2
Language = Chinese
The service has been successfully started.
.
MessageID = 3
Language = Chinese
The service has stopped.
.
MessageId = 4
Language = Chinese
Please use legitimate software!
.
MessageID = 5
Language = Chinese
Error, the system returns the following information:% 1
.
[/ Code]
Execute the command line to get the resource file that Delphi can use McMsgs.RES
> mc mcmsgs.mc
> brc32 -r -fomcmsgs.res -v -32 mcmsgs.rc
Then add {$ r mcmsgs.res} in the unit of the project.
3. Add information about the registry to fill in the registry
[Code]
// .....
VAR
MyfirstService: TMYFIRSERVICE;
IMPLEMENTATION
Uses
REGISTRY;
{$ R * .dfm}
{$ R mcmsgs.res}
Procedure TMYFIRSERVICE.SERVICEAFTERINSTALL (Sender: TService);
VAR
REG: TREGISTRY;
Begin
REG: = Tregistry.create;
Try
REG.ROOTKEY: = HKEY_LOCAL_MACHINE
// Register the service description information display in the service management of the system
Reg.openkey ('System / CurrentControlset / Services / [Red] myfirstservice [/ red]', TRUE
Reg.writestring ('Description', 'Description Information of this Service');
REG.CLOSEKEY;
// Register the event viewer message, NT4 doesn't seem to register these,
// If there is other.DLL can modify the absolute path and file name reg.openkey (EventLogmsgfileKey, true);
Reg.writeString ('EventMessagefile', paramstr (0));
Reg.writeInteger ('Typessupported', 7);
REG.CLOSEKEY;
Finally
Reg.free;
END;
END;
Procedure TMYFIRSERVICE.SERVICEAFTERUNSTALL (Sender: TService);
VAR
REG: TREGISTRY;
Begin
REG: = Tregistry.create;
Try
REG.ROOTKEY: = HKEY_LOCAL_MACHINE
Reg.deleteKey (EventLogmsgfileKey);
Finally
Reg.free;
END;
END;
[/ Code]
4. Install / uninstall service
Fill in / install and / uninstall parameters in Run-> Parameters in the Delphi menu and run
5. Use controls such as ADO in the service
When using controls such as ADO, the system will prompt "Coinitialize;" error.
Quote ActiveX unit in the service and call it.
[Code]
Procedure TMYFIRSERVICE.ServiceStart (Sender: TService; Var Started: Boolean);
Begin
Coinitialize (NIL);
IF "Start Initialization Function" THEN
LogMessage ('', EventLog_INFORMATION_TYPE, 0, 2);
ELSE BEGIN
Started: = false;
LogMessage ('"wrong reason", EventLog_ERROR_TYPE, 0, 5);
END;
END;
Procedure TMYFIRSERVICE.SERVICESTOP (Sender: TService; VAR Stopped: Boolean);
Begin
LogMessage ('', EventLog_INFORMATION_TYPE, 0, 3);
Couninitialize;
END;
[/ Code]
[Finish]