Some service item applications need to be compiled for the latest project. Because I haven't written any code for a long time, I forgot something. I had to spend some time to retrieve it again. Now we can paste some useful information for future convenience. Some of them are found online. Some are my own experiences.
Tservice
Attribute Introduction
Allowpause indicates whether the service can be paused. True: The suspend button on SCP (Service Control Panel) is available, and false is unavailable.
Allowstop indicates whether the service can be stopped. True: the stop button on SCP (Service Control Panel) is available, and false is unavailable.
Dependecies is used to list all services to be dependent on.
Displayname indicates the service name displayed on SCP.
Errcode specifies an error code. This code is returned when an error occurs or status information is provided. If the errcode value is 0, the win32errorcode attribute is used.
Errorseverity indicates how to handle errors when starting the service
Interactive indicates whether a dialog box can be displayed. Applicable only to Win32 services
The name of the service, that is, the name of the Service in SCM. If you want to use SC .exeor net.exe to control a service, you must specify the service name instead of the name specified by displayname.
The parameter list when Param is started. After SC .exe is used to start the service, you can specify parameters in SCP or set parameters in the command line.
Paramcount: number of parameters passed for a service
Password is used to set the password. Only applicable to services that do not use loaclsystem accounts
Servicestartname is used to set the service account name. Format: domain name/User Name
Servicethread is the internal thread of the service and is used to process commands and requests.
Servicetype service type, which can be set to: stwin32 (Win32 service), stdevice (Device Driver), or stfilesystem (File System Service)
Status Service's current status (running, stopped, paused, stop pending, etc)
Terminated indicates whether the internal thread is terminated.
The time when the waithint service waits for control command or status request. If no response is received within the specified time, SCM considers the service to be faulty.
When an error occurs in win32errcode or the errcode attribute value is 0, it contains a system-defined error code.
Event introduction:
Afterinstall: The method called after the service is installed;
Afteruninstall: The method called after the service is uninstalled;
Beforeinstall: method called before the service is installed;
Beforeuninstall: method called before the service is uninstalled;
Oncontinue: Method for suspending the Service to continue calling;
Onexecute: method used to start calling the service;
Onpause: the method used to suspend a service call;
Onshutdown: The method called when it is disabled;
Onstart: Method for starting a service call;
Onstop: Method for stopping a service call;
The logmessage () function is used to send an event log message to NT.
The reportstatus () function is used to send service status information to SCM.
How to restrict system services and desktop programs to run only one
Http://hi.baidu.com/sqldebug/blog/item/58a764624a44d74eeaf8f863.html
View code
Add the following code to the project to set only one System Service and one desktop program to run.
Program fleetreportsvr;
Uses
Svcmgr, forms, sysutils, windows,
Svrmain in 'svrmain. pa' {fleetreportservice: tservice },
Appmain in 'appmain. pa' {fmfleetreport };
{$ R *. Res}
Const
Csmutexname = 'Global \ services_application_mutex ';
VaR
Oneinstancemutex: thandle;
Secmem: security_attributes;
ASD: security_descriptor;
Begin
Initializesecuritydescriptor (@ ASD, security_descriptor_revision );
Setsecuritydescriptordacl (@ ASD, true, nil, false );
Secmem. nlength: = sizeof (security_attributes );
Secmem. lpsecuritydescriptor: = @ ASD;
Secmem. binherithandle: = false;
Oneinstancemutex: = createmutex (@ secmem, false, csmutexname );
If (getlasterror = error_already_exists) then
Begin
Dlgerror ('error, program or service already running! ');
Exit;
End;
If findcmdlineswitch ('svc', true) or
Findcmdlineswitch ('install', true) or
Findcmdlineswitch ('uninstall', true) then
Begin
Svcmgr. application. initialize;
Svcmgr. application. createform (tsvsvrmain, svsvrmain );
Svcmgr. application. Run;
End
Else
Begin
Forms. application. initialize;
Forms. application. createform (tfmfmmain, fmmain );
Forms. application. Run;
End;
End.
Share memory between system services and desktop programs
Http://hi.baidu.com/sqldebug/blog/item/58a764624a44d74eeaf8f863.html
Almost all functions used to create kernel objects have a pointer to the security_attributes structure as their parameters. When using the createfilemapping function, null is usually passed for this parameter, in this way, you can create kernel objects with default security.
By default, security means that any member of the Management Group of the object and the creator of the object have full access to the object, while others have no access to the object. You can specify an ecurity_attributes structure, initialize it, and pass the address of this structure to this parameter.
It contains only one security-related member, namely, lpsecuritydescriptor. When you want to obtain access to a corresponding Kernel Object (instead of creating a new object), you must set what operations to perform on the object. If you want to access an existing file ing kernel object to read its data, call the openfilemapping function to pass file_map_read to openfilemapping as the first parameter, indicates that you want to read the file after obtaining access to the file image. Before the function returns a valid handle value
Perform a security check. If (logged-on user) is allowed to access the existing file ing kernel object, a valid handle is returned. However, if the object is denied access, null is returned.
Core code of the system server:
Constructor tpublicvars. Create (anew: Boolean );
VaR
Secmem: security_attributes;
ASD: security_descriptor;
Begin
Inherited create;
{Create a kernel object access permission that any user can access}
Initializesecuritydescriptor (@ ASD, security_descriptor_revision );
Setsecuritydescriptordacl (@ ASD, true, nil, false );
Secmem. nlength: = sizeof (security_attributes );
Secmem. lpsecuritydescriptor: = @ ASD;
Secmem. binherithandle: = false;
Fmapfile: = createfilemapping ($ ffffffff, @ secmem, page_readwrite, 0, csharedmemsize, csharedmemname );
Fmapfile: = openfilemapping (file_map_all_access, false, csharedmemname );
If (fmapfile = 0) then
Begin
Raise exception. Create (syserrormessage (getlasterror ));
Outputdebugstring (pchar (syserrormessage (getlasterror )));
End
Else
Begin // succeeded
Fsharemem: = mapviewoffile (fmapfile, file_map_all_access, 0, 0, csharedmemsize );
Outputdebugstring (pchar (syserrormessage (getlasterror) + ', handle =' + inttostr (handle )));
End;
End;
Destructor tpublicvars. Destroy;
Begin
Unmapviewoffile (fsharemem );
Closehandle (fmapfile );
Inherited;
End; core source code of the desktop program:
Constructor tpublicvars. Create (anew: Boolean );
VaR
Secmem: security_attributes;
ASD: security_descriptor;
Begin
Inherited create;
{Create a kernel object access permission that any user can access}
Initializesecuritydescriptor (@ ASD, security_descriptor_revision );
Setsecuritydescriptordacl (@ ASD, true, nil, false );
Secmem. nlength: = sizeof (security_attributes );
Secmem. lpsecuritydescriptor: = @ ASD;
Secmem. binherithandle: = false;
Fmapfile: = createfilemapping ($ ffffffff, @ secmem, page_readwrite, 0, csharedmemsize, csharedmemname );
Fmapfile: = openfilemapping (file_map_all_access, false, csharedmemname );
If (fmapfile = 0) then
Begin
Raise exception. Create (syserrormessage (getlasterror ));
Outputdebugstring (pchar (syserrormessage (getlasterror )));
End
Else
Begin // succeeded
Fsharemem: = mapviewoffile (fmapfile, file_map_all_access, 0, 0, csharedmemsize );
Outputdebugstring (pchar (syserrormessage (getlasterror) + ', handle =' + inttostr (handle )));
End;
End;
Destructor tpublicvars. Destroy;
Begin
Unmapviewoffile (fsharemem );
Closehandle (fmapfile );
Inherited
Use COM components in services
Calling COM components in a service cannot be directly created like in a desktop program. coinitialize (NiL) is called before each creation and couninitialize is called upon release. For example, call the ADO component.
VaR
Qry: tadoquery;
Begin
Coinitialize (NiL );
Qry: = tadoquery. Create (NiL );
Try
...
Finally
Qry. Free;
Couninitialize;
End;
End;