SOURCE:
http://www.e-nef.com/perl/ntperl.html.en
Install a Perl Script as a NT service
A NT service answers to an interface message call, to start it, pause it and stop it. So a signle script cannot be installed. The NT Ressource Kit 3 brings INSTSRV and ANYSRV, the first one to install a service name and the second one which is the service interface.
For example we create a service called PerlService :
`c:\\peliproc\\instsrv.exe PerlService c:\\peliproc\\srvany.exe`;
This installs an entry in the Registry Database. You can then finish hand installation as specified in the documentation or modify though the Win32::Registry the entries:
$p = "SYSTEM\\CurrentControlSet\\Services\\PerlService";
$main::HKEY_LOCAL_MACHINE->Open($p,$srv)||die "open: $!";
we are to create the key Parameters containing :
Application : the full path to the script
AppParameters : the execution parameters
AppDirectory : the path where from it will be executed
what we do this way:
$srv->Create("Parameters",$param)||die "Create: $!";
$param->SetValueEx("Application",0,REG_SZ,"c:\\perlproc\\scan.cmd");
$param->SetValueEx("AppParameters",0,REG_SZ,"2>c:\\tetmp\\toto.out");
$param->SetValueEx("AppDirectory",0,REG_SZ,"c:\\perlproc\\");
So we launch scan.cmd in the c:\perlproc directory (nota bene : it is needed to repeat the \ directory separator under NT, which serves to 'escape' i.e. use the specials caracters under Perl, so to have a \ we double it: \\). the argument is 2>c:\temp\toto.out, we redirect the standard error output (StdERR under perl, device 2 under each O.S.) to a file.
Now we can start the service.
`d:\\winnt\\system32\\net.exe start PerlService`;
The only thing that example hides is that the service is started with the SYSTEM account which under NT has more priorities than other account, even Administrator. It is possible through the Registry database to specify another user, but ... a password is needed, and I don't know how to add it trough the Registry database ...
Emulate a CRONTAB under NT
The at command and its interface winat can execute only asingle task a day, and this under the SYSTEM account. For some tasks, this is too constraining, e.g. scan a directory every 10 mn.
So, the best way is to create a NT service, and in the Perl script add an endless loop, and a sleep(600) for 10 mn i.e. 10*60 seconds.
Accessing an UNC reference under NT
It is easily done using the external command NET:
`d:\\winnt\\system32\\net.exe use \\server\ressource "password"
/USER:"user_name"'
The best way to catch the return message is to redirect with 2> to a file and scann it afterward.
Hence, the current environment (CurrentSet) has memorized the acces granting to this resource, and to access it after, you only need to specify the path as a normal path :
open(FIC,"\\serveur\ressource\vol1\DD-XF-FF.080") || warn "$0 $!";
and it is all !
Map a drive under NT
To map a drive is to associate to a resource(share) on the network, a device, i.e. driver, shown as a: à z: .
Thsi is done the same way under NT, but to use it as a service this introduces the problem that the current user may already use this device (only 23 available at least). A drive mapped is only under the current user profile; connection-less or under another login, it will not be anymore, unless this new profile map it itself.
Sending mail under NT
There is two ways:
Outlook:
The NT resource Kit brings a program called MAPISEND which permits to send a mail through an existing locally installed Outlook. It then need to only use the command line arguments.
SMTP Server or SMTP gateway under Exchange:
This more standard method, thanks to the libnet library from Perl (not available with ActiveWare, you then need to use hand-made sockets...) which gives through Net::SMTP all the necessary interface to this protocol;
The call is done with use Net::SMTP; , then you need to connect a SMTP Server :
$smtp = Net::SMTP->new("$MAILHOST");
and say who emits :
$smtp->mail("epierre\@e-nef.com");
and says to whom it is destined :
$smtp->to("$EMAIL");
and then we send the data :
smtp->data();
$smtp->datasend("To: $EMAIL\n");
$smtp->datasend("Cc: $CONTACT\n");
$smtp->datasend("Subject: Perl under NT, hard ?\n");
Here, we specify again who we write to, then we define a CC (a Carbon Copy, a copy to another person), and then the subject after a keyword Subject.We then add the content of the mail by $smtp->datasend("") that we repeat as long as we have lines to send, and we close the mail by
$smtp->dataend();
$smtp->quit();
The mail is now sent !
Using an Access Database through ODBC
Three steps are needed :
Have the corresponding ODBC driver (Start/Control Panel/ODBC 32/)
Add your base to the User DSN (see ODBC Home Page
Access it
The last step is done through Perl :
the database has an alias name through the DSN which serves to call it. We create the Database Connector Object :
$db = new Win32::ODBC($dsn);
we then form the SQL request :
$sql = "SELECT * FROM directory WHERE priority <= $priority2
ORDER BY name,firstname ";
for example.
we then emit the command :
$db->Sql($sql);
we get the result (if many) :
while ($db->FetchRow()) {
and we put in the variable the content of the record :
($name,$firstname,$tel)=$db->Data("name","firstname","tel")
}
we can check the return code through :
$db->Error();
and we close the database connection :
$db->Close();
Simple, simple ... a lot, yes !
Other Links
Getting the Current TabItem when the Tab is not selected in WPF
-
[image: Banner]
This is a quick reminder to self on how to retrieve a TabItem from a WPF
TabControl *when the tab is not currently selected* because I ru...
1 month ago
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!