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

Gammon 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?

Accessing COM objects from Lua

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1 2  3  

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Thu 27 Oct 2005 12:55 AM (UTC)

Amended on Fri 10 Sep 2010 10:41 PM (UTC) by Nick Gammon

Message
Although Lua is not implemented using the COM (Component Object Model) interface, sometimes it is useful to be able to use COM from within your scripts.

[EDIT] (September 2010) The module luacom will let you access COM objects. This is now built into version 4.60 onwards of MUSHclient. Instead of following the instructions below about downloading luacom, simply do this:


require "luacom"





This is possible if you download luacom from LuaForge, and use it in your Lua scripts.

I obtained it from:


http://luaforge.net/


Specifically this section:


http://luaforge.net/projects/luacom/


For this demonstration I only needed the file luacom-1.3-bin.tgz, although the file luacom-1.3-doc.pdf is also useful, being the documentation.

I extracted the archive and got the file luacom-lua5-1.3.dll which I placed in the same directory as the MUSHclient executable.

Then, running the following invoked the Microsoft Calendar program:




-- load luacom
assert (loadlib ("luacom-lua5-1.3.dll","luacom_openlib")) ()

-- Instantiate a Microsoft(R) Calendar Object
calendar = luacom.CreateObject("MSCAL.Calendar")

-- Error check
if calendar == nil then
  error ("Error creating object")
end

-- Method call
calendar:AboutBox()

-- Property Get
current_day = calendar.Day

-- Property Put
calendar.Month = calendar.Month + 1

print(current_day)
print(calendar.Month)


[EDIT] Updated example, using MUSHclient 4.60 onwards:


require "luacom"

-- Instantiate a Microsoft(R) Calendar Object
calendar = luacom.CreateObject("MSCAL.Calendar")

-- Error check
if calendar == nil then
  error ("Error creating object")
end

-- Method call
calendar:AboutBox()

-- Property Get
current_day = calendar.Day

-- Property Put
calendar.Month = calendar.Month + 1

print(current_day)
print(calendar.Month)

- Nick Gammon

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sat 27 May 2006 09:52 PM (UTC)

Amended on Fri 10 Sep 2010 10:43 PM (UTC) by Nick Gammon

Message
The method described above does not seem to work with recent versions of MUSHclient that use the lua50.dll, because the earlier LuaCom implementations required different DLLs.

The latest download from their site works with slightly different code. This time I will use the SAPI object to "speak" something:


assert (loadlib ("luacom.dll","luaopen_luacom")) ()

-- Instantiate a SAPI voice obejct
talk = assert (luacom.CreateObject ("SAPI.SpVoice"), "cannot open SAPI")

-- Method call
talk:Speak ("hi there")



The link I used to download the luacom.dll was:


http://luaforge.net/frs/download.php/922/luacom-1.3-luabinaries-bin.zip


[EDIT] (September 2010) Updated example using MUSHclient 4.60 onwards:


require "luacom"

-- Instantiate a SAPI voice obejct
talk = assert (luacom.CreateObject ("SAPI.SpVoice"), "cannot open SAPI")

-- Method call
talk:Speak ("hi there")


- Nick Gammon

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Mon 26 Mar 2007 06:41 AM (UTC)
Message
For more recent versions of MUSHclient (3.80 onwards) you need to use package.loadlib, like this:


assert (package.loadlib ("luacom.dll","luacom_open")) ()


Also, the version of luacom that has been compiled for Lua 5.1 is available here:

http://www.gammon.com.au/files/mushclient/lua5.1_extras/luacom.zip


See this thread for more discussion:

http://www.gammon.com.au/forum/bbshowpost.php?id=6593

- Nick Gammon

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Wed 01 Sep 2010 01:44 AM (UTC)

Amended on Thu 02 Sep 2010 08:11 AM (UTC) by Nick Gammon

Message
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
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)


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

- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #4 on Wed 01 Sep 2010 01:47 AM (UTC)
Message
Might I suggest adding a wrapper script to the lua/ directory, so you can just do require("com")?

'Soludra' on Achaea

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Wed 01 Sep 2010 01:50 AM (UTC)
Message
What would that look like?

- Nick Gammon

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Wed 01 Sep 2010 01:51 AM (UTC)

Amended on Wed 01 Sep 2010 06:03 AM (UTC) by Nick Gammon

Message
You mean, com.lua that has this in it?


-- load Lua COM
assert (package.loadlib ("luacom.dll", "luacom_open")) ()
return luacom


- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #7 on Wed 01 Sep 2010 01:52 AM (UTC)
Message
Nick Gammon said:
You mean, com.lua that has this in it?


-- load Lua COM
assert (package.loadlib ("luacom.dll", "luacom_open")) ()


Yeah, pretty much. :)

'Soludra' on Achaea

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #8 on Wed 01 Sep 2010 03:00 AM (UTC)
Message
OK, good idea. Added com.lua to the distribution.

- Nick Gammon

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

Posted by WillFa   USA  (525 posts)  [Biography] bio
Date Reply #9 on Wed 01 Sep 2010 06:21 AM (UTC)

Amended on Wed 01 Sep 2010 06:24 AM (UTC) by WillFa

Message
Our Illustrious Leader Nick said:

Amended so you just need to require "com" rather than use the rather convoluted package.loadlib.


assert (package.loadlib ("luacom.dll", "luacom_open")) ()


Is the syntax for Lua 4.0. In Lua 5.1, require does this for you.


require "luacom"


Is sufficient as long as "Allow DLLs to be loaded" is checked, and luacom.dll is in the current directory or the path specified by the LUA_CPATH environment variable.

[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #10 on Wed 01 Sep 2010 06:35 AM (UTC)

Amended on Wed 01 Sep 2010 06:41 AM (UTC) by Twisol

Message
That doesn't work for me. Here's the error that require("luacom") produces:

Run-time error
Plugin: TTS_jFW_Lua (called from world: Soludra - Achaea)
Immediate execution
error loading module 'luacom' from file 'C:\Users\Jonathan\Documents\MUSHclient\worlds\plugins\TTS_JFW.plugin\libraries\luacom.dll':
        The specified procedure could not be found.

stack traceback:
        [C]: ?
        [C]: in function 'require'
        ...lient\worlds\plugins\TTS_JFW.plugin\scripts\main.lua:4: in main chunk
        [C]: in function 'require'
        [string "Plugin"]:41: in main chunk

'Soludra' on Achaea

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

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #11 on Wed 01 Sep 2010 06:38 AM (UTC)
Message
The problem is that the Lua 5.1 entry-point format is luaopen_<libname>, whereas the luacom.dll file specifies luacom_open.

'Soludra' on Achaea

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

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Wed 01 Sep 2010 06:43 AM (UTC)
Message
Actually it doesn't, although I tried it because I recall you said something about that before.


Run-time error
World: SmaugFUSS
Immediate execution
error loading module 'luacom' from file '.\luacom.dll':
        The specified procedure could not be found.

stack traceback:
        [C]: ?
        [C]: in function 'require'
        [string "Command line"]:1: in main chunk


The entry point is wrong for the automatic require to work.

- Nick Gammon

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

Posted by WillFa   USA  (525 posts)  [Biography] bio
Date Reply #13 on Wed 01 Sep 2010 07:33 AM (UTC)
Message
Odd.


print(package.cpath) ->
C:\Lua\5.1\clibs\?.dll;


Timestamp of C:\Lua\5.1\clibs\luacom.dll:

5/16/2008 1:27:32


require "luacom"
print (luacom)
table: 04D641B0

tprint(luacom) ->

"FillTypeLib"=function: 04D40FC8
"addConnection"=function: 04D654E8
"Connect"=function: 04D65920
"GetCurrentDirectory"=function: 048A1AF8
"ExportConstants"=function: 04D368E8
"UnRegisterObject"=function: 04D66488
"NewLibrary"=function: 04D41568
"ProgIDfromCLSID"=function: 04D65588
"RegisterObject"=function: 04D66438
"RevokeObject"=function: 04D60678
"NewObject"=function: 04D65FB0
"ExposeObject"=function: 04D66000
"DetectAutomation"=function: 04D0B730
"CreateObject"=function: 04D64188
"ViewTypeLib"=function: 04D43CA0
"CLSIDfromProgID"=function: 04D653F8
"RoundTrip"=function: 04D10190
"NewControl"=function: 04D65FD8
"StartMessageLoop"=function: 04D0C928
"FillTypeInfo"=function: 04D38EB8
"_copyFields"=function: 04D3BB90
"DumpTypeInfo"=function: 04D66898
"GetIUnknown"=function: 04D664B0
"GetType"=function: 04D35178
"LoadTypeLibrary"=function: 04D66C08
"pairs"=function: 04D363E8
"CreateInprocObject"=function: 04D3B758
"ImplInterfaceFromTypelib"=function: 04D654C0
"CreateLuaCOM"=function: 04D644D0
"EndLog"=function: 04D61F28
"releaseConnection"=function: 04D656A0
"config":
  "__ENUMERATOR_LUACOM_TYPE":
    "__index"=function: 0507B8A0
    "type"="__ENUMERATOR_LUACOM_TYPE"
  "__ENUMERATOR_POINTER_LUACOM_TYPE":
    "__gc"=function: 0507B648
    "type"="__ENUMERATOR_POINTER_LUACOM_TYPE"
  "IConnectionPoint":
    "__index"=function: 0507AE78
    "type"="IConnectionPoint"
  "LuaCOM":
    "__index"=function: 05077110
    "type"="LuaCOM"
    "__newindex"=function: 05077160
    "__call"=function: 05078308
  "abort_on_API_error"=false
  "ITypeInfo":
    "type"="ITypeInfo"
    "__index"=function: 0507B670
    "__newindex"=function: 0507C0E8
  "ITypeInfo_pointer":
    "__gc"=function: 0507A4A0
    "type"="ITypeInfo_pointer"
  "abort_on_error"=true
  "__luacom_instances_cache":
  "ITypeLib_pointer":
    "__gc"=function: 0507BA30
    "type"="ITypeLib_pointer"
  "LuaCOM_UDTAG":
    "__gc"=function: 0507A8D8
    "type"="LuaCOM_UDTAG"
  "ITypeLib":
    "type"="ITypeLib"
    "__index"=function: 05079618
    "__newindex"=function: 0507B0F8
  "IUnknown":
    "type"="IUnknown"
    "__gc"=function: 0507AEA0
    "__eq"=function: 0507ACE8
"ShowHelp"=function: 04D65948
"ImplInterface"=function: 04D65470
"DumpTypeLib"=function: 04D43C00
"GetObject"=function: 04D64160
"CreateLocalObject"=function: 04D3AC40
"ImportIUnknown"=function: 04D670B8
"isMember"=function: 04D65538
"GetTypeInfo"=function: 04D66910
"StartLog"=function: 04D66550
"GetEnumerator"=function: 04D66AC8



Perhaps you have an old dll on your webserver?
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #14 on Wed 01 Sep 2010 08:21 AM (UTC)
Message
WillFa said:

Odd.


print(package.cpath) ->
C:\Lua\5.1\clibs\?.dll;

Perhaps you have an old dll on your webserver?



Old is not the word.

In any case, where did this file you have on your C drive come from, exactly? The link, I mean.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[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.


42,877 views.

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

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

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

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]