Delphi Learning Skills

Source: Internet
Author: User
Address: http://hi.baidu.com/sqldebug/blog/item/58a764624a44d74eeaf8f863.html

I. How to restrict system services and desktop programs to run only one

How to restrict system services and desktop programs to run only one

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.

2. share memory between system services and desktop programs

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;
End;

For detailed source code, see source code about shared memory in report service and report COM. Note that the shared memory should be initialized in servicestart, but not in Initialization. Otherwise, the permission is insufficient because Initialization is the code executed before the application initialization.

3. Use COM components in the service

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.