[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Accessing COM objects from Lua

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Accessing COM objects from Lua
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please).
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Pages: 1 2  3  

Posted by Nick Gammon   Australia  (18,769 posts)  [Biography] bio   Forum Administrator
Date Fri 15 Oct 2010 09:24 PM (UTC)  quote  ]
Message
Cool. Now you have a method of getting at all your data.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by FenceWalker   USA  (8 posts)  [Biography] bio
Date Fri 15 Oct 2010 03:44 PM (UTC)  quote  ]

Amended on Fri 15 Oct 2010 04:30 PM (UTC) by FenceWalker

Message
Thanks for the help after a couple days of reading and experimentation. I managed to get it working. Adding the line
val = var.value
was needed to actually display what was contained in the variable.


require("luacom")
require("luacom5")

local data = 1

if data == 1 then
  cmud = luacom.CreateObject("cMUD.Application")
  cmuds = cmud.CurrentSession
  var = cmuds.getvar(MSDP,"MSDP")
  val = var.value
  end

print(val)
[Go to top] top

Posted by Nick Gammon   Australia  (18,769 posts)  [Biography] bio   Forum Administrator
Date Wed 13 Oct 2010 08:30 PM (UTC)  quote  ]
Message
Sorry I was a bit slow to respond - got a bit distracted by the mapper.

The easiest thing might be to do what some others have done, and just get at the Access database which I believe Cmud uses. There was someone recently doing that to grab mapper information.

If that doesn't work, you could try looking at the saved data (I think Cmud uses XML?) which you might be able to parse reasonably simply.

Another thing to try would be to follow the example earlier up this page and try to get the HTML type library from the program, like this:


require "luacom"  -- built into MUSHclient
require "luacom5" -- supplied as luacom5.lua

luacom.DumpTypeLib ("cMUD.Application", "c:\\cmud_typelib.html")


I don't have a copy of cMUD so I can't try that, but you might find from that what your next step would be.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by FenceWalker   USA  (8 posts)  [Biography] bio
Date Tue 12 Oct 2010 08:40 AM (UTC)  quote  ]
Message
Yeah I guess I was a bit non descriptive, sorry. Here is the full picture. I am trying to get the values of variables from within CMUD. *gasp* Hehe, specifically I would like to use MC and CMUD together. I have alot of scripts written in in zscript already, but there are plugins for MUSHclient that I would like to use aswell. My idea is if I could get MUSHclient to draw the info from the variables I have populated in CMUD directly I could have the best of both worlds. :)

I am using the folowing:
require("luacom")
require "tprint"
local data = 1

cmud = nil
cmuds = nil
var = nil

if data > 0 then
  cmud = luacom.CreateObject("cMUD.Application")
  cmuds = cmud.CurrentSession
  var = cmuds.getvar(test,"\test")
  data = 0
end

print(var)
tprint(var)


which spits out this:
table: 03E39D88
"_USERDATA_REF_"=userdata: 03E3B330


The variable test actually contains a string of:
This is a test

I would ideally like to have the actual contents of the test variable echoed to MUSHclient be it a string, string list or database variable.
[Go to top] top

Posted by Nick Gammon   Australia  (18,769 posts)  [Biography] bio   Forum Administrator
Date Tue 12 Oct 2010 01:48 AM (UTC)  quote  ]
Message
FenceWalker said:

Can anyone offer some insight on where I need to go from here to turn that table reference into usable information?



It might be easier to answer if you gave a specific example. COM can be obscure at the best of times. But if you say something like "I want to play song 3 in my iTunes list", or "I want to get data from Cell A13 in Excel" then that gives us something to get our teeth into.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Worstje   Netherlands  (867 posts)  [Biography] bio
Date Tue 12 Oct 2010 12:47 AM (UTC)  quote  ]

Amended on Tue 12 Oct 2010 12:50 AM (UTC) by Worstje

Message
You'll want to look into Nick's tprint function. Basically, do this:

require "tprint"
tprint(table_of_interest)


Regarding the COM object server functionality.. I am not sure that is possible. I've tried something like that once while trying to abuse the MUSHclient world object, and that basically was a big world of 'let us crash MUSHclient' since MUSHclient is not made to have its own object used outside its own process. (Technically, one probably could consider it a bug, but I think very few people know how to fix it or even need it.)
[Go to top] top

Posted by FenceWalker   USA  (8 posts)  [Biography] bio
Date Mon 11 Oct 2010 11:00 PM (UTC)  quote  ]

Amended on Mon 11 Oct 2010 11:34 PM (UTC) by FenceWalker

Message
Hello. MC looks to have come along nicely since the last time I used it. I have a question about COM objects in MC and was hoping I might get a little help with a couple of questions.

First I can get info from the program I have the object set up for, like the version of the program, printed to the screen of MC, so that part is ok. Some information that I am trying to view from the object however comes out like this:
table: 05E35C40

Can anyone offer some insight on where I need to go from here to turn that table reference into usable information?

Second I'm a bit confused about what ProgID or ClassID I need to use if I want to set up a COM object for MC to use with another program, if that is possible?

I'm a bit rusty with LUA in general, so try to keep that in mind and maybe dumb responses down a little if anyone has time to reply.
[Go to top] top

Posted by Nick Gammon   Australia  (18,769 posts)  [Biography] bio   Forum Administrator
Date Thu 02 Sep 2010 10:25 PM (UTC)  quote  ]

Amended on Fri 03 Sep 2010 01:20 AM (UTC) by Nick Gammon

Message
If you want to find more about the "type library" you can do this:


require "luacom"  -- built into MUSHclient
require "luacom5" -- supplied as luacom5.lua

-- grab raw type library
rawtlb = luacom.LoadTypeLibrary("itunes.Application")

assert (rawtlb, "Type library not found")

-- convert to table
tlb = luacomE.FillTypeLib(rawtlb)  
rawtlb = nil

require "tprint"
tprint (tlb)  --> 17,982 lines ouptput


Example of some of the output:


    71:
      "helpcontext"=0
      "type"="void"
      "description"="True if playback position is remembered."
      "prototype"="void RememberBookmark(VARIANT_BOOL p1)"
      "parameters":
        1:
          "in"=true
          "type"="VARIANT_BOOL"
          "name"="p1"
          "out"=false
          "opt"=false
      "name"="RememberBookmark"
      "num_params"=1
      "dispid"=1610874884
      "rawMethod":
        "helpcontext"=0
        "type"="void"
        "description"="True if playback position is remembered."
        "invkind"="propput"
        "memid"=1610874884
        "name"="RememberBookmark"
        "parameters"=table: 01A75228
        "ParamsOpt"=0
        "helpfile"=""
        "Params"=1
      "typeinv"="propput"



Or you can get a rather nifty HTML document like this:


require "luacom"  -- built into MUSHclient
require "luacom5" -- supplied as luacom5.lua

luacom.DumpTypeLib ("itunes.Application", "c:\\itunes_typelib.html")


(Lengthy file appears in the nominated location). Opening that with your web browser reveals stuff like this:

Quote:


  • Rewind

    void Rewind()

    Skip backwards in a playing track.

    LuaCOM examples:

    com_obj:Rewind()



- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (18,769 posts)  [Biography] bio   Forum Administrator
Date Thu 02 Sep 2010 10:14 PM (UTC)  quote  ]
Message
Just to help people get a bit more out of COM, I tried to interface with iTunes using it. This is my preliminary test:


require "luacom"

itunes = assert (luacom.CreateObject ("iTunes.Application"))

-- print version
print (itunes.Version)  --> 10.0.0.68

-- get the library
library = itunes.LibraryPlayList

-- get tracks from library
trackslist = library.Tracks

-- get count of tracks
n = trackslist.Count

print ("Number of tracks =", n)

-- enumerate each track
tracks = luacom.GetEnumerator (trackslist)

-- get first one
track = tracks:Next ()

-- play first track
track:Play ()

-- print all track names
while track do
  print (track.Name)
  track = tracks:Next ()
end -- while



Afterwards, and I'm not sure where you would put this, if anywhere, you can dispose of the COM object:


-- stop using it
itunes = nil
collectgarbage ( )


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (18,769 posts)  [Biography] bio   Forum Administrator
Date Thu 02 Sep 2010 08:12 AM (UTC)  quote  ]
Message
Sigh. That's what happens when you keep changing things. I fixed up that post.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Twisol   USA  (2,229 posts)  [Biography] bio
Date Thu 02 Sep 2010 06:47 AM (UTC)  quote  ]
Message
Nick Gammon said:

LuaCOM is now included in version 4.60 onwards of MUSHclient. So you can just use it without downloading any files. For example:


-- load Lua COM
com = require "luacom"
  
-- Instantiate a SAPI voice object
talk = assert (luacom.CreateObject ("SAPI.SpVoice"), "cannot open SAPI")
  
-- Method call
talk:Speak ("SAPI installed and is ready.", 1)


Amended so you just need to require "luacom", which is now built into the MUSHclient executable.


Your example here sets 'com' from the require call but never uses it. It doesn't affect anything, but it does look odd.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (18,769 posts)  [Biography] bio   Forum Administrator
Date Thu 02 Sep 2010 12:57 AM (UTC)  quote  ]
Message
Well based on the advice from you guys, I have modified my approach.


  • Luacom is now compiled into MUSHclient, so the issue of clashing DLLs goes away.

  • LuaSocket is not, however as the relevant DLLs (core.dll and core.dll - sigh) are in a subdirectory they are unlikely to clash with your stuff.

  • You need to require "luacom" based on Twisol's suggestion of using package.preload. This stops any possible overhead or difficulty under Wine/Parallels, because nothing will happen if you don't "require" it.

  • My tests show it works more reliably than the earlier DLL anyway.


The documentation is supplied in the distribution, plus the file luacom5.lua which adds extra functionality if you want to require that as well.

The executable is only about 90 Kb larger, and that includes the other enhancements in this version as well.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by WillFa   USA  (517 posts)  [Biography] bio
Date Wed 01 Sep 2010 11:23 PM (UTC)  quote  ]

Amended on Wed 01 Sep 2010 11:26 PM (UTC) by WillFa

Message
So that syntax doesn't work...
It's not supported syntax...
It's lexically correct, but your module doesn't support it.
Pedantic... sorry. moving on.


Depends shows that your version has a missing 'required implicit or forwarded dependency' on my system. There's an unresolved link to MSVCR80.dll.

It has 4 exports, LfW's has 7, including luaopen_luacom, which is probably what require is looking for.

[Go to top] top

Posted by Nick Gammon   Australia  (18,769 posts)  [Biography] bio   Forum Administrator
Date Wed 01 Sep 2010 10:46 PM (UTC)  quote  ]
Message
WillFa said:

require "luacom"


It fails for you and Twisol; works just dandy for me.


That's not a syntax error. It is correctly reporting that the entry point does not exist in the DLL.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by WillFa   USA  (517 posts)  [Biography] bio
Date Wed 01 Sep 2010 10:35 PM (UTC)  quote  ]
Message
Nick Gammon said:

WillFa said:

We've already seen that there's something different between the dlls you've compiled and the ones that LfW has distributed, like the Lua 5.1 syntax actually working with theirs.


Huh? What syntax? Can you give an example? I haven't changed the way Lua works except maybe turn off some of the deprecated functions they actually recommend you turn off (via the standard config file).


require "luacom"


It fails for you and Twisol; works just dandy for me.

I can require() 95% of the included libs distributed with LfW fine (and just that one funky one I asked about last year fails).



I guess I'm thinking that of all the libs to distribute, I'm wondering about a funky version of LuaCom. (btw, your version is 204,800 bytes. LfW's is 172,032 bytes.)

Lpeg? Definitely.
LuaCURL or LuaSocket? Yup I can see them being included.
A LuaCom distro that behaves weirdly? When you've always maintained that you want MC to run fine under WINE and Parallels (No real COM support)? Just seems weird.

In all of MC's history it hasn't really been needed, and the only reason it's come up now is because one person wanted a plugin ported from one language to another (I suspect the same flakiness he saw in the VB implementation will appear with Lua using the same COM interfaces.)



And you don't have to tell them to get LfW. Luaforge or LuaRocks can get them libs as well. There's multiple ways to get them...
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


17,851 views.

This is page 1, subject is 3 pages long: 1 2  3  [Next page]

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]