LUALoriotPro WEB site

LoriotPro scripting documentation
Extensions for LUA language

Table of contents Search function

Print the current page Mail this  link UTUBE Channel version française

LUA_COM Call a COM object

This library can be use with extrem cautious. It can be used only in direct menu access. It open an access to the Micorsoft COM interface.

WARNING : LUTEUS do not provide support on this library and all the external libraries.

Description
LuaCOM is an add-on library to the Lua language that allows Lua programs to use and implement objects that follow Microsoft's Component Object Model (COM) specification and use the ActiveX technology for property access and method calls. LuaCOM is implemented as a C++ library and runs either stand-alone or enbedded in another application. LuaCOM is provided with its source code and the makefiles required to build it.
LuaCOM has been designed and implemented by Vinicius Almendra and Renato Cerqueira, and is maintained by Fabio Mascarenhas, at TeCGraf, the Computer Graphics Technology Group of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro in Brazil). TeCGraf is a laboratory of the Department of Computer Science.
LuaCOM is available under the same terms and conditions as the Lua language, that is, it can be used at no cost for both academic and commercial purposes. Send your comments, bug reports and anything else to luacom % tecgraf.puc-rio.br
Copyright © 1998-2005 TeCGraf, PUC-Rio. All rights reserved.


Example of use with a WMI access


local lib,init= lp.LoadLibrary(lp.GetPath().."/lua_com.dll","luaopen_luacom")

if lib then init();
lp.Print("loacom loaded\n");
require("luacom")
--[[
Example Lua Object for using Windows Managment and Instrumentation via LuaCom
This object is meant to be used as a class to create other objects.
Contributed by Michael Cumming
--]]

require("luacom")

cWMI = {
New = function (self)
o = {}
setmetatable (o,self)
self.__index = self
return o
end,

Connect = function (self,computer,user,password)
computer = computer or "."

if not user then
self.oWMIService = luacom.GetObject ("winmgmts:{impersonationLevel=Impersonate}!\\\\" ..computer.. "\\root\\cimv2")
else
self.oWMIService = luacom.GetObject ( "winmgmts:\\\\" ..computer.. "\\root\\cimv2",user,password)
end

if not self.oWMIService then
return nil,"Failed to connect to computer "..computer
end

--refresher
self.oRefresher = luacom.CreateObject ("WbemScripting.SWbemRefresher")
self.oRefresher.AutoReconnect = 1

-- processor
self.refobjProcessor = self.oRefresher:AddEnum(self.oWMIService,"Win32_PerfFormattedData_PerfOS_Processor").ObjectSet

-- memory
self.refobjMemory = self.oRefresher:AddEnum(self.oWMIService,"Win32_PerfFormattedData_PerfOS_Memory").ObjectSet

--drive
self.refobjDisk = self.oRefresher:AddEnum(self.oWMIService,"Win32_PerfFormattedData_PerfDisk_LogicalDisk").ObjectSet

--network
self.refobjNetwork = self.oRefresher:AddEnum(self.oWMIService,"Win32_PerfFormattedData_Tcpip_NetworkInterface").ObjectSet
self.oRefresher:Refresh ()

local cWMISettings = self.oWMIService:ExecQuery ("Select * from Win32_WMISetting")

for index,item in luacomE.pairs (cWMISettings) do
self.ver = item:BuildVersion ()
end

return self.ver
end,

GetProcessorPercentTime = function (self)
self.oRefresher:Refresh ()
for index,item in luacomE.pairs (self.refobjProcessor) do
if item:Name () == "_Total" then
return item:PercentProcessorTime ()
end
end
return 0
end,

GetFreeMemory = function (self)
local x
self.oRefresher:Refresh ()
for index,item in luacomE.pairs (self.refobjMemory) do
x = item:AvailableMBytes ()
end
return x or 0
end,

GetFreeDiskSpace = function (self,drive)
local x,y
self.oRefresher:Refresh ()
for index,item in luacomE.pairs (self.refobjDisk) do
if item:Name () == drive then
x = item:FreeMegaBytes ()
y = item:PercentFreeSpace ()
return x,y
end
end
return 0,0
end,

GetNetworkInterfaceAll = function (self)
local item,BpsSent,BpsRec,BpsTot,bpsBandwidth
BpsSent,BpsRec,BpsTot,bpsBandwidth = 0,0,0,0
for index,item in luacomE.pairs (self.refobjNetwork) do
BpsRec = BpsRec + item:BytesReceivedPerSec()
BpsSent = BpsSent + item:BytesSentPerSec()
BpsTot = BpsTot + item:BytesTotalPerSec()
bpsBandwidth = bpsBandwidth + item:CurrentBandwidth()
end
return BpsSent,BpsRec,BpsTot,bpsBandwidth
end,

CreateProcess = function (self,Process)
local objProcess = self.oWMIService:Get("Win32_Process")
return objProcess:Create (Process,nil,nil,nil)
end,

--[[ returns the following error codes
0 Successful completion
2 Access denied
3 Insufficient privilege
8 Unknown failure
9 Path not found
21 Invalid parameter ]]

TerminateProcess = function (self,Process)
local colProcesses = self.oWMIService:ExecQuery("select * from Win32_Process where Name=\""..Process.."\"",nil,48)
local i
for index,item in luacomE.pairs (colProcesses) do
i = (i or 0) + 1
item:Terminate ()
end
return i
end,

ExistProcess = function (self,Process)
local colProcesses = self.oWMIService:ExecQuery("select * from Win32_Process where Name=\""..Process.."\"",nil,48)
local i
for index,item in luacomE.pairs (colProcesses) do
i = (i or 0) + 1
end
return i
end
}

localWMI = cWMI:New ()

localWMI:Connect (".") -- connect to local machine using current user credentials

lp.Print ("localWMI.ver ",localWMI.ver,"\n")
lp.Print ("localWMI:GetProcessorPercentTime ",localWMI:GetProcessorPercentTime (),"\n")
lp.Print ("localWMI:GetFreeMemory ",localWMI:GetFreeMemory (),"\n")
lp.Print ("localWMI:GetNetworkInterfaceAll ",localWMI:GetNetworkInterfaceAll (),"\n")
lp.Print ("localWMI:GetFreeDiskSpace ",localWMI:GetFreeDiskSpace ("C:"),"\n")
lp.Print ("localWMI:CreateProcess ",localWMI:CreateProcess ("notepad.exe"),"\n")
lp.Print ("localWMI:ExistProcess ",localWMI:ExistProcess ("notepad.exe"),"\n")
lp.Print ("localWMI:TerminateProcess ",localWMI:TerminateProcess ("notepad.exe"),"\n")

end




www.loriotpro.com