A Simple Method for writing an NT Service to operate a database in D7

Source: Internet
Author: User

Title: a simple method for writing an NT Service to operate a database in D7

Author: jrq

Link: http://blog.csdn.net/jrq/archive/2006/04/27/679510.aspx

1. file -- New -- Other -- Service Application

2. file -- New -- Other -- thread object

3. Thread unit (tpostmessage)

Uses ADODB, inifiles, ActiveX;

Procedure tpostmessage. writelog (const logstr: string );
VaR F: textfile;
Begin
If logstr <> ''then // write the log file
Begin
// Assignfile (F, extractfilepath (paramstr (0) + 'postmessage. log ');
Assignfile (F, extractfilepath (paramstr (0) + 'postmessage. log ');

if not fileexists (extractfilepath (paramstr (0) + 'postmessage. log') Then
begin
rewrite (f); // create a new file for the write;
writeln (F, '[' + formatdatetime ('yyyy-mm-dd hh: mm: ss', now) + ']:' + logstr);
closefile (f );
end
else
begin
append (f);
writeln (F, '[' + formatdatetime ('yyyy-mm-dd hh: mm: ss', now) + ']:' + logstr);
closefile (f );
end;

Procedure tpostmessage. creatdbcon;
VaR aconfigfile: Tinifile;
Begin
// If not fileexists (extractfilepath (application. exename) + 'config. ini ') then
If not fileexists (extractfilepath (paramstr (0) + 'config. ini ') then
Begin
Writelog ('configuration file does not exist. Please check. ');
Exit;
End;

Try
Aconfigfile: = Tinifile. Create (extractfilepath (paramstr (0) + 'config. ini ');
Adoconn: = tadoconnection. Create (NiL );
Adoconn. connectionstring: = 'provider = oraoledb. oracle.1; '+
'Password = '+ aconfigfile. readstring ('config', 'dbpass', 'dbpass') +'; '+
'Persist Security info = true; '+
'User id = '+ aconfigfile. readstring ('config', 'dbuser', 'dbuser') +'; '+
'Data source = '+ aconfigfile. readstring ('config', 'db', 'orcl') + ';' +
'Plsqlrset = 1 ';

Adoconn. loginprompt: = false;
Try
Adoconn. Connected: = true;
Adoqry: = tadoquery. Create (NiL );
Adoqry. Connection: = adoconn;
Except
Writelog ('failed to connect to the database. ');
Exit;
End;
Finally
Freeandnil (aconfigfile );
End;
End;

Procedure tpostmessage. freedbcon;
Begin
Freeandnil (adoqry );
Freeandnil (adoconn );
End;

Procedure tpostmessage. Execute;
VaR messagestr: string;
Begin
Sleep (1000*60*10); // wait for 10 minutes first ^_^. Cause: if the service is self-started and the DB is not started yet, access exceptions may occur.

Coinitialize (NiL); // get started with COM

Creatdbcon; // connect to the database

While true do
Begin
Try
Adoqry. close;
Adoqry. SQL. Clear;
Adoqry. SQL. Add ('select * from table where 1 = 1 order by fieldname ');
Adoqry. open;

While not adoqry. EOF do
Begin
Messagestr: = 'logstring ';
// Other code ......
Adoqry. Next;

If messagestr <> ''then // write a log file
Writelog (messagestr );
End; // while not adoqry. EOF do
Except
Writelog ('has an exception. ');
End;
Sleep (10000 );
End; // while true do
Freedbcon; // release
Couninitialize;
End;

 

4. Service Application (tmain)

VaR postmsg: tpostmessage;

// Start
Procedure tmain. servicestart (Sender: tservice; var started: Boolean );
Begin
Postmsg: = tpostmessage. Create (false );
Started: = true;
End;

// Stop
Procedure tmain. servicestop (Sender: tservice; var stopped: Boolean );
Begin
Postmsg. freedbcon;
Postmsg. Terminate;
Stopped: = true;
End;

Procedure tmain. servicecontinue (Sender: tservice; var continued: Boolean );
Begin
Postmsg. Resume;
Continued: = true;
End;

// Pause
Procedure tmain. servicepause (Sender: tservice; var paused: Boolean );
Begin
Postmsg. Suspend;
Paused: = true;
End;

// Initialize the service before installation
Procedure tmain. servicebeforeinstall (Sender: tservice );
VaR aconfigfile: Tinifile;
Begin
If not fileexists (extractfilepath (paramstr (0) + 'config. ini ') then
Exit;

Try
Aconfigfile: = Tinifile. Create (extractfilepath (paramstr (0) + 'config. ini ');
Servicestartname: = aconfigfile. readstring ('config', 'sysuser', 'sysuser'); // OS user
Password: = aconfigfile. readstring ('config', 'syspass', 'syspass'); // OS password
Finally
Freeandnil (aconfigfile );
End;
End;

5. Service Installation

Set in Service Application

Displayname: Service name displayed
Servicestartname: system user
Password: User Password

Installation Method:ProgramRight-click the shortcut icon, select "properties-shortcut", and add the parameter "/install" to "target ".

Double-click the shortcut to run and install the tool. The parameter "/uninstall" is uninstalled. The same method is used.

The service can be installed in the Automatic startup mode. When the system starts, it runs automatically.

If the user permission is incorrect, the installation may fail.

The installation is successful. If the startup fails in "service", You need to right-click "properties -- Logon" in "service" of the control panel and select "Local Account System ".

And then restart the service.

6. config. ini

[Config]
DB = ordb
Dbuser = System
Dbpass = Manager
Sysuser = thunis/jrq
Syspass = jrq

7. Ide debug

You can add long sleep () to the first line of the Service's execute, and then add the breakpoint.

After starting the service in the control panel, select the IDE menu "Run -- attach to process...", check show system processes, and select the service name,

Click attach to enter the debug status.

 

8. Remarks

One of the basic principles of using COM is that every thread using COM should first call coinitialize or coinitializeex to initialize com.

To operate the ADO control in the thread, you must display and execute the coinitialize () function (uses ActiveX), such:

Procedure thread. Execute;
Begin
Coinitialize (NiL); // before calling any COM or ole api function, you must call coinitialize to initialize the com library.
Mainfrm. adoqry. open;
Mainfrm. adoqry. close;
Couninitialize ();
End;

[-End-]

by jrq

2006/04/27 on Spike

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.