Windows API的Tcl扩展包TWAPI介绍

TWAPI(Tcl Windows API) 提供了通过Tcl脚本访问Windows API的方法。这个扩展包可以支持WindowNT系列操作系统(NT 4.0, Windows 2000, Windows XP and Windows 2003) API的调用。下面是这个包提供的各API的分类目录。
具体的文档可以参考Script.NET中的Tcl帮助文档。

[@more@]
OverviewTcl Windows API extension overview.
InstallationInstallation requirements and instructions.
Win32 raw APIDirect access to the Win32 API.
Window managementCommands related to management of windows.
Desktops and Window stationsCommands related to desktops and window stations.
Windows ShellWindows shell commands.
Keyboard and mouse inputSimulation of user input and support for system-wide hotkeys.
SoundBasic sound functions.
ClipboardCommands related to accessing the clipboard.
SystemOperating system and machine configuration and control.
Processes, threads and DLLsCommands related to processes, threads and dynamic link libraries.
HandlesCommands for managing operating system handles.
ServicesCommands for configuring and monitoring Windows services.
Network configurationCommands related to network configuration.
Disks and VolumesCommands related to disks and volumes.
Network sharesCommands related to management of network shares.
Users and Groups Commands related to user and group accounts.
Security and access controlCommands related to authentication and access control.
Event logCommands relating to reading and writing the Windows event log.
Windows INI fileCommands for accessing Windows INI files.
ConsoleCommands related to Windows consoles.
SchedulerCommands related to managing scheduled jobs through the task scheduler.
InternationalizationCommands related to internationalization.
MiscellaneousVarious TWAPI ancillary commands.
PrintersPrinter management.
ExamplesTcl Windows API usage examples.
Version HistoryTcl Windows API extension version history.

Script.NET中使用TWAPI扩展包写了一个系统工具,可以查看Windows系统的进程、服务、端口信息,界面和代码如下。

winsys.jpg

#################################################################
# 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
"$STR_PAGE($page)"
}
}
$_objTextInfo SetText
"[join $lsText " |
"]"


# 进程列表
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 del.gif"
}
stopped {set svcStateText
"$svcState this.gif"
}
}

set svcCommand [lindex [twapi::get_service_configuration $svcName -command] 1
]

if {$svcPid == 0} {set svcPid
""
}


$_tableInfo AddLine2 [list
$svcPid] /
[list
"$svcDispName"
] /
[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;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/459749/viewspace-1057376/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/459749/viewspace-1057376/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值