D7中編寫NT服務運算元據庫的簡單方法

2021-08-22 03:47:17 字數 4326 閱讀 6542

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 //寫日誌檔案

begin

//assignfile(f,extractfilepath(paramstr(0))+'postmessage.log');

assignfile(f,extractfilepath(paramstr(0))+'postmessage.log');

tryaconfigfile:=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;

tryadoconn.connected:=true;

adoqry:=tadoquery.create(nil);

adoqry.connection:=adoconn;

except

writelog('連線資料庫失敗。');

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);//等10分鐘先 ^_^ 。原因:如果服務是自啟動,而db此時可能還未啟動,會造成訪問異常。

coinitialize(nil); //初試化com

creatdbcon; //連線資料庫

while true do

begin

tryadoqry.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 //寫日誌檔案

writelog(messagestr);

end; //while not adoqry.eof do

except

writelog('出現異常。');

end;

sleep(10000);

end; //while true do

freedbcon; //釋放

couninitialize;

end;

var postmsg:tpostmessage;

//啟動

procedure tmain.servicestart(sender: tservice; var started: boolean);

begin

postmsg:= tpostmessage.create(false);

started := true;

end;

//停止

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;

//暫停

procedure tmain.servicepause(sender: tservice; var paused: boolean);

begin

postmsg.suspend;

paused := true;

end;

//安裝前,初試化服務

procedure tmain.servicebeforeinstall(sender: tservice);

var aconfigfile:tinifile;

begin

if not fileexists(extractfilepath(paramstr(0))+'config.ini') then

exit;

tryaconfigfile:=tinifile.create(extractfilepath(paramstr(0))+'config.ini');

servicestartname:=aconfigfile.readstring('config','sysuser','sysuser'); //os使用者

password:=aconfigfile.readstring('config','syspass','syspass'); //os密碼

finally

freeandnil(aconfigfile);

end;

end;

5.服務安裝

displayname:顯示的服務名稱

servicestartname:系統使用者

password:使用者密碼

安裝方法:在服務程式的快捷方式圖示上右擊,選擇「屬性--快捷方式」,在「目標」中加上引數" /install"。

然後雙擊快捷方式執行即可安裝。 引數"/uninstall"為解除安裝,使用方法同樣。

服務可安裝成自動啟動的方式,當系統啟動時,會自啟動執行。

如果使用者許可權有問題則可能安裝不成功。

安裝成功,如果在「服務」中啟動失敗,則需在控制面板的「服務」中右擊選擇「屬性--登入」,勾選「本地賬戶系

統」,然後重新啟動服務。

6.config.ini

[config]

db=ordb

dbuser=system

dbpass=manager

sysuser=thunis\jrq

syspass=jrq

7.ide debug

可在服務的execute中首行加上長時間sleep(),然後加入斷點。

控制面板中啟動服務後,選擇ide選單"run--attach to process...",勾選show system processes,選擇服務名稱,

單擊「attach」進入debug狀態。

8.備註

使用com 的基本原則之一,就是每個使用 com 的執行緒都應該先呼叫 coinitialize 或 coinitializeex 來初始化 com。

procedure thread.execute;

begin

coinitialize(nil); //呼叫任何com 或ole api函式之前,必須顯示呼叫coinitialize進行初始化com庫

mainfrm.adoqry.open;

mainfrm.adoqry.close;

couninitialize();

end;

[-完-]

by jrq

2006/04/27 於穗

編寫NT服務

編寫nt服務 先介紹一下什麼是nt服務,實際上就是乙個可以在系統啟動時自動在一定身份下啟動的,伴隨著系統長期存在的程序.乙個nt服務有三部分構成 1 service control manager scm 每個win nt 2k都有乙個scm,他存在於service.exe中.2 服務本身 乙個服務...

D7下FastMM的使用

fastmm 快速mm 在d2006和2007中已代替了原來的記憶體管理器。d7也可以使用,而且很方便哦。請看步驟 1.fastmm是開源專案,去她老家先拖個來.http sourceforge.net projects fastmm 2.資料夾replacement borlndmm dll pr...

D7專案公升級記錄

服務端資料 json格式然後des加密之後base64轉換 delphi post獲取資料後 由於原有解密程式為d7下使用 des astr string string string 是ansistring 現在要在xe版本中使用原有的加密方法時直接替換所有string為ansistring,chr...