PDA

View Full Version : LuaCOM help with automation syntax



mstahl
October 12th, 2009, 08:01 PM
I'm trying to access an automation object which works great from wsh as HCA.Group.On("Test Name"). I think I have the syntax right for Lua, but I keep getting error:

attempt to index global `resultGrp' (a function value)

Can someone help with the syntax of this code? (Note Lua chokes on the last line)


HCA = luacom.GetObject("HCA.Object")
resultGrp = HCA.Group
resultGrp:On("Test Name")

harleydude
October 13th, 2009, 04:51 AM
Here is some LuaCOM code I put together awhile back.


function LoadFolderContents(folder)
print (folder)
local filelist = {}
local fs = luacom.CreateObject("Scripting.FileSystemObject")
if fs == nil then
print ('Error opening Scripting.FileSystemObject')
return nil
end
local dir = fs:GetFolder(folder)
local files = luacom.GetEnumerator(dir.Files)
local file = files:Next()
while file do
--print (file.Name)
table.insert(filelist, {Name = file.Name})
file = files:Next()
end

DirectoryList:UpdateEntries(filelist)
-- clean up after ourselves
fs = nil
collectgarbage()
end


You might try resultGrp.On("Test Name")

mstahl
October 13th, 2009, 05:19 AM
I tried every combination of "." and ":". It won't work...

I even went so far as to load oleview and explore the TLB provided with the 3rd party app (HCA). Now I'm way over my head but...

I see a disinterface IHCA, which contains a property of type IDispatch named Group. I see a dispinterface IAutoGroup with a method called "On".

Does anyone know the right syntax for calling the method On? Again, in wsh it was a simple HCA.Group.On("Param"). This syntax fails under Lua due to "attempt to index field 'Group' (a function value)."

AAhhhh...I'm pulling my hair out with this...

Ron
October 13th, 2009, 08:53 AM
Sadly I am not sure what the answer is, but the error hints that the resultGrp variable is a function, so neither : or . will work...

Did you try this:



HCA = luacom.GetObject("HCA.Object")
resultGrp = HCA.Group()
resultGrp:On("Test Name")


note the extra () after Group.

mstahl
October 13th, 2009, 12:18 PM
That worked thanks. Not sure I understand why I would Group is also a function (with no parameters) but that did it.

Ron
October 13th, 2009, 12:24 PM
Just the way this library was built, when you call that function the connection to the proper com object method is resolved and the result passed back. In WSH that is done transparently when you access the group member. Anyway if you see this again, the error led me to suggest the fix,... if it is complaining something is a function,.. call it,... unless the function is called formatc :)