TWAPI(Tcl Windows API) 提供了通过Tcl脚本访问Windows API的方法。这个扩展包可以支持WindowNT系列操作系统(NT 4.0, Windows 2000, Windows XP and Windows 2003) API的调用。下面是这个包提供的各API的分类目录。
具体的文档可以参考Script.NET中的Tcl帮助文档。
Script.NET中使用TWAPI扩展包写了一个系统工具,可以查看Windows系统的进程、服务、端口信息,界面和代码如下。
#################################################################
# Script.NET 系统工具页面脚本
# 作者:blueant
# 版本:2.0
# 日期:2006-08-25
#################################################################
package require TclFace
package require twapi
#-------------------------------------------------------------
# TLFAppPage class define
#-------------------------------------------------------------
class TLFAppPage {
inherit TLFPage
constructor {} {};
### data member ###
protected
variable _objTextInfo;
protected
variable _tableInfo;
protected
variable _pageType; # 页面类型
protected
variable _svcName; # 当前服务名
### public methods ###
public
method ProcessFormValue {name value}; # 处理Form结果
public
method OnCreate {}; # 创建页面
public
method OnInitialUpdate {}; # 页面初始化
public
method OnEndAction {}; # 动作执行完后的操作
public
method OnServiceStart {}; # 启动服务
public
method OnServiceStop {}; # 停止服务
}
#-------------------------------------------------------------
# 单元构造函数
#-------------------------------------------------------------
body TLFAppPage::constructor {} {
chain;
SetFormAction;
set _pageType
"" ;
set _svcName
"" ;
}
#-------------------------------------------------------------
# 处理Form结果
#-------------------------------------------------------------
body TLFAppPage::ProcessFormValue {name value} {
if {[chain $name $value] == true} {
return true
}
switch $name {
page {set _pageType $value}
svc {set _svcName $value}
}
}
#-------------------------------------------------------------
# 创建页面
#-------------------------------------------------------------
body TLFAppPage::OnCreate {} {
set _objTextInfo [AppendText
"" ]
$_objTextInfo SetColor # 800000
set _tableInfo [CreateObject TLFTable]
}
#-------------------------------------------------------------
# 页面初始化
#-------------------------------------------------------------
body TLFAppPage::OnInitialUpdate {} {
}
#-------------------------------------------------------------
# 动作执行完后的操作
#-------------------------------------------------------------
body TLFAppPage::OnEndAction {} {
if {$_pageType == "" } {
set _pageType
"process"
}
# 页面选择
LangStringEC STR_PAGE(process) "Processes"
"进程"
LangStringEC STR_PAGE(service) "Services"
"服务"
LangStringEC STR_PAGE(connection) "Network Connections"
"网络连接"
set lsText {}
foreach page {process service connection} {
if {$page == $_pageType} {
lappend lsText $STR_PAGE($page)
} else {
lappend lsText
"<a href='tcl:<file>[info script]?page=$page'>$STR_PAGE($page)</a>"
}
}
$_objTextInfo SetText
"<b>[join $lsText " | "]</b>"
# 进程列表
if {$_pageType == "process" } {
LangStringEC STR_INFO
"PID"
"进程ID"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Name"
"进程名"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"User"
"用户名"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Path"
"进程路径"
$_tableInfo AddColumn $STR_INFO
set lsPID [lsort -integer [twapi::get_process_ids]]
foreach PID $lsPID {
set lsInfo [twapi::get_process_info $PID -name -path -user]
$_tableInfo AddLine2 [list $PID] [list [lindex $lsInfo
1 ]] /
[list [lindex $lsInfo
5]] [list [lindex $lsInfo
3 ]]
}
}
# 服务列表
if {$_pageType == "service" } {
LangStringEC STR_INFO
"PID"
"进程ID"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Name"
"服务名"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"State"
"状态"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Description"
"进程描述"
$_tableInfo AddColumn $STR_INFO
set lsService [twapi::get_multiple_service_status -win32_own_process -win32_share_process]
set index
0
foreach svc $lsService {
set svcName [twapi::kl_get $svc name]
set svcPid [lindex [twapi::get_service_status $svcName] 17 ]
set svcDispName [twapi::kl_get $svc displayname]
set svcState [twapi::kl_get $svc state]
set svcStateText $svcState
switch $svcState {
running {set svcStateText
"$svcState <a title='stop service' href='tcl:<file>[info script]?page=$_pageType&svc=$svcName&action=OnServiceStop'><img src='image/del.gif' border=0></a>" }
stopped {set svcStateText
"$svcState <a title='start service' href='tcl:<file>[info script]?page=$_pageType&svc=$svcName&action=OnServiceStart'><img src='image/this.gif' border=0></a>" }
}
set svcCommand [lindex [twapi::get_service_configuration $svcName -command] 1 ]
if {$svcPid == 0} {set svcPid
"" }
$_tableInfo AddLine2 [list $svcPid] /
[list
"<a href='#javascript:return false;' title='$svcCommand'>$svcDispName</a>" ] /
[list $svcStateText] /
[list [lindex [twapi::get_service_configuration $svcName -description] 1 ]]
# 如果服务在运行,设置行的特殊颜色
if {$svcState == "running" } {
$_tableInfo SetOneLineStyle $index #ffffd8 # 000080
}
incr index
}
}
# 网络连接列表
if {$_pageType == "connection" } {
LangStringEC STR_INFO
"Protocal"
"协议"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"State"
"状态"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Process"
"进程"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Remote Host"
"远程主机"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Remote Port"
"远程端口"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Local Host"
"本地主机"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Local Port"
"本地端口"
$_tableInfo AddColumn $STR_INFO
foreach conn [twapi::get_tcp_connections] {
set connState [twapi::kl_get $conn -state]
$_tableInfo AddLine2 [list
"TCP" ] /
[list $connState] /
[list [twapi::get_process_name [twapi::kl_get $conn -pid ]]] /
[list [twapi::kl_get $conn -remoteaddr]] /
[list [twapi::kl_get $conn -remoteport]] /
[list [twapi::kl_get $conn -localaddr]] /
[list [twapi::kl_get $conn -localport]]
}
foreach conn [twapi::get_udp_connections] {
$_tableInfo AddLine2 [list
"UDP" ] /
[list
"" ] /
[list [twapi::get_process_name [twapi::kl_get $conn -pid ]]] /
[list
"*" ] /
[list
"*" ] /
[list [twapi::kl_get $conn -localaddr]] /
[list [twapi::kl_get $conn -localport]]
}
}
CreateObject TLFVariable
"page" $_pageType
}
#-------------------------------------------------------------
# 启动服务
#-------------------------------------------------------------
body TLFAppPage::OnServiceStart {} {
twapi::start_service $_svcName -wait
10000
}
#-------------------------------------------------------------
# 停止服务
#-------------------------------------------------------------
body TLFAppPage::OnServiceStop {} {
twapi::stop_service $_svcName -wait
10000
}
#-------------------------------------------------------------
# main
#-------------------------------------------------------------
LangString STR_PAGE_TITLE chinese
"操作系统工具"
LangString STR_PAGE_TITLE english
"Windows System Tool"
set page [TLFCreatePage $STR_PAGE_TITLE TLFAppPage]
delete object $page;
具体的文档可以参考Script.NET中的Tcl帮助文档。
Overview | Tcl Windows API extension overview. |
Installation | Installation requirements and instructions. |
Win32 raw API | Direct access to the Win32 API. |
Window management | Commands related to management of windows. |
Desktops and Window stations | Commands related to desktops and window stations. |
Windows Shell | Windows shell commands. |
Keyboard and mouse input | Simulation of user input and support for system-wide hotkeys. |
Sound | Basic sound functions. |
Clipboard | Commands related to accessing the clipboard. |
System | Operating system and machine configuration and control. |
Processes, threads and DLLs | Commands related to processes, threads and dynamic link libraries. |
Handles | Commands for managing operating system handles. |
Services | Commands for configuring and monitoring Windows services. |
Network configuration | Commands related to network configuration. |
Disks and Volumes | Commands related to disks and volumes. |
Network shares | Commands related to management of network shares. |
Users and Groups | Commands related to user and group accounts. |
Security and access control | Commands related to authentication and access control. |
Event log | Commands relating to reading and writing the Windows event log. |
Windows INI file | Commands for accessing Windows INI files. |
Console | Commands related to Windows consoles. |
Scheduler | Commands related to managing scheduled jobs through the task scheduler. |
Internationalization | Commands related to internationalization. |
Miscellaneous | Various TWAPI ancillary commands. |
Printers | Printer management. |
Examples | Tcl Windows API usage examples. |
Version History | Tcl Windows API extension version history. |
Script.NET中使用TWAPI扩展包写了一个系统工具,可以查看Windows系统的进程、服务、端口信息,界面和代码如下。
#################################################################
# Script.NET 系统工具页面脚本
# 作者:blueant
# 版本:2.0
# 日期:2006-08-25
#################################################################
package require TclFace
package require twapi
#-------------------------------------------------------------
# TLFAppPage class define
#-------------------------------------------------------------
class TLFAppPage {
inherit TLFPage
constructor {} {};
### data member ###
protected
variable _objTextInfo;
protected
variable _tableInfo;
protected
variable _pageType; # 页面类型
protected
variable _svcName; # 当前服务名
### public methods ###
public
method ProcessFormValue {name value}; # 处理Form结果
public
method OnCreate {}; # 创建页面
public
method OnInitialUpdate {}; # 页面初始化
public
method OnEndAction {}; # 动作执行完后的操作
public
method OnServiceStart {}; # 启动服务
public
method OnServiceStop {}; # 停止服务
}
#-------------------------------------------------------------
# 单元构造函数
#-------------------------------------------------------------
body TLFAppPage::constructor {} {
chain;
SetFormAction;
set _pageType
"" ;
set _svcName
"" ;
}
#-------------------------------------------------------------
# 处理Form结果
#-------------------------------------------------------------
body TLFAppPage::ProcessFormValue {name value} {
if {[chain $name $value] == true} {
return true
}
switch $name {
page {set _pageType $value}
svc {set _svcName $value}
}
}
#-------------------------------------------------------------
# 创建页面
#-------------------------------------------------------------
body TLFAppPage::OnCreate {} {
set _objTextInfo [AppendText
"" ]
$_objTextInfo SetColor # 800000
set _tableInfo [CreateObject TLFTable]
}
#-------------------------------------------------------------
# 页面初始化
#-------------------------------------------------------------
body TLFAppPage::OnInitialUpdate {} {
}
#-------------------------------------------------------------
# 动作执行完后的操作
#-------------------------------------------------------------
body TLFAppPage::OnEndAction {} {
if {$_pageType == "" } {
set _pageType
"process"
}
# 页面选择
LangStringEC STR_PAGE(process) "Processes"
"进程"
LangStringEC STR_PAGE(service) "Services"
"服务"
LangStringEC STR_PAGE(connection) "Network Connections"
"网络连接"
set lsText {}
foreach page {process service connection} {
if {$page == $_pageType} {
lappend lsText $STR_PAGE($page)
} else {
lappend lsText
"<a href='tcl:<file>[info script]?page=$page'>$STR_PAGE($page)</a>"
}
}
$_objTextInfo SetText
"<b>[join $lsText " | "]</b>"
# 进程列表
if {$_pageType == "process" } {
LangStringEC STR_INFO
"PID"
"进程ID"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Name"
"进程名"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"User"
"用户名"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Path"
"进程路径"
$_tableInfo AddColumn $STR_INFO
set lsPID [lsort -integer [twapi::get_process_ids]]
foreach PID $lsPID {
set lsInfo [twapi::get_process_info $PID -name -path -user]
$_tableInfo AddLine2 [list $PID] [list [lindex $lsInfo
1 ]] /
[list [lindex $lsInfo
5]] [list [lindex $lsInfo
3 ]]
}
}
# 服务列表
if {$_pageType == "service" } {
LangStringEC STR_INFO
"PID"
"进程ID"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Name"
"服务名"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"State"
"状态"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Description"
"进程描述"
$_tableInfo AddColumn $STR_INFO
set lsService [twapi::get_multiple_service_status -win32_own_process -win32_share_process]
set index
0
foreach svc $lsService {
set svcName [twapi::kl_get $svc name]
set svcPid [lindex [twapi::get_service_status $svcName] 17 ]
set svcDispName [twapi::kl_get $svc displayname]
set svcState [twapi::kl_get $svc state]
set svcStateText $svcState
switch $svcState {
running {set svcStateText
"$svcState <a title='stop service' href='tcl:<file>[info script]?page=$_pageType&svc=$svcName&action=OnServiceStop'><img src='image/del.gif' border=0></a>" }
stopped {set svcStateText
"$svcState <a title='start service' href='tcl:<file>[info script]?page=$_pageType&svc=$svcName&action=OnServiceStart'><img src='image/this.gif' border=0></a>" }
}
set svcCommand [lindex [twapi::get_service_configuration $svcName -command] 1 ]
if {$svcPid == 0} {set svcPid
"" }
$_tableInfo AddLine2 [list $svcPid] /
[list
"<a href='#javascript:return false;' title='$svcCommand'>$svcDispName</a>" ] /
[list $svcStateText] /
[list [lindex [twapi::get_service_configuration $svcName -description] 1 ]]
# 如果服务在运行,设置行的特殊颜色
if {$svcState == "running" } {
$_tableInfo SetOneLineStyle $index #ffffd8 # 000080
}
incr index
}
}
# 网络连接列表
if {$_pageType == "connection" } {
LangStringEC STR_INFO
"Protocal"
"协议"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"State"
"状态"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Process"
"进程"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Remote Host"
"远程主机"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Remote Port"
"远程端口"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Local Host"
"本地主机"
$_tableInfo AddColumn $STR_INFO
LangStringEC STR_INFO
"Local Port"
"本地端口"
$_tableInfo AddColumn $STR_INFO
foreach conn [twapi::get_tcp_connections] {
set connState [twapi::kl_get $conn -state]
$_tableInfo AddLine2 [list
"TCP" ] /
[list $connState] /
[list [twapi::get_process_name [twapi::kl_get $conn -pid ]]] /
[list [twapi::kl_get $conn -remoteaddr]] /
[list [twapi::kl_get $conn -remoteport]] /
[list [twapi::kl_get $conn -localaddr]] /
[list [twapi::kl_get $conn -localport]]
}
foreach conn [twapi::get_udp_connections] {
$_tableInfo AddLine2 [list
"UDP" ] /
[list
"" ] /
[list [twapi::get_process_name [twapi::kl_get $conn -pid ]]] /
[list
"*" ] /
[list
"*" ] /
[list [twapi::kl_get $conn -localaddr]] /
[list [twapi::kl_get $conn -localport]]
}
}
CreateObject TLFVariable
"page" $_pageType
}
#-------------------------------------------------------------
# 启动服务
#-------------------------------------------------------------
body TLFAppPage::OnServiceStart {} {
twapi::start_service $_svcName -wait
10000
}
#-------------------------------------------------------------
# 停止服务
#-------------------------------------------------------------
body TLFAppPage::OnServiceStop {} {
twapi::stop_service $_svcName -wait
10000
}
#-------------------------------------------------------------
# main
#-------------------------------------------------------------
LangString STR_PAGE_TITLE chinese
"操作系统工具"
LangString STR_PAGE_TITLE english
"Windows System Tool"
set page [TLFCreatePage $STR_PAGE_TITLE TLFAppPage]
delete object $page;