LUACOM-IDL-VBS

Source: Internet
Author: User

LUA is very useful. In Windows, COM is the boss. It would be cool to combine their functions. The LuaCOM Library (current version 1.4) is very good at playing this matchmaking role. This article does not discuss various Luacom functions. You can download the LuaCOM User Manual for a comprehensive understanding. This is just an example of HelloWorld and a lesson for implementing this example. There are not many detailed examples on the Internet (or maybe my search method is not correct). Basically, I cannot find examples of the HellowWorld class, so I feel it is necessary to write a blog, share with interested readers and take notes for themselves. Speaking Nonsense has never been my own style. Start now. Prepare necessary tools. If VS ideis installed, it is the best. Besides midl.exe, Visual Studio Command Prompt (2010) can be easily obtained. Then, upload lua.exeand luacom.dll. here, we will teach you how to use lua.exe and LuaCOM. the dll version must be compatible (LuaCOM. it is best to use the dll version 1.4. It seems that the test fails and you have not studied it carefully. It is normal to use the 1.3 version. If you can pass the following example in the 1.4 version, please post a post. Thank you ). Use midl.exe to generate a tlb file named test. idl and test. tlb. Test. idl [cpp] // VBScript and JScript do not support [in, out], use [in, out] or [out, retval] // import "unknwn. idl "; [uuid (991AB5E0-97C4-4ecc-9926-D76038C517A1), version (1.0)] library TEST {importlib (" STDOLE2.TLB "); [object, uuid (UID), dual, oleautomation, version (1.0), helpstring ("luacom interface implement test 1.0")] interface ITest: IDispatch {void Version ([ In, out] short * major, [in, out] short * minor);}; [uuid (340DFD25-59B2-49b4-A693-A7B19A13FD42)] coclass Test {[default] dispinterface ITest ;}}; I am not familiar with VBScript and JScript. At the beginning, the parameter only specifies [out], and the results will not be returned normally (no results are returned ), the [out] parameter is not supported only after searching on the Internet. If it is in LuaCOM, such a parameter can be returned normally. Prepare a Lua script for implementing COM. There is one in the LuaCOM manual. Here we just make some changes to implement the above ITest interface. Testobj. lua [cpp] -- This is the implementation of the COM object local LOON_DIR = OS. getenv ("loon"); package. cpath = LOON_DIR .. [[\.. \ lua \?. Dll] require "luacom" path_to_script = LOON_DIR .. [[\.. \ lua \ idl \] export TestObj = {} function TestObj: Version (a, B) return 123 + a, 455 + B; end consumer COM = {} function COM: startAutomation () -- print ("testobj. lua> StartAutomation "); -- Creates the object usi Ng its default interface COMAppObject, events, e = luacom. newObject (TestObj, "TEST. test "); -- print (" testobj. lua> ".. tostring (COMAppObject), events, e); -- This error will be caught by detectAutomation if COMAppObject = nil then error ("NewObject failed :".. e); end -- Exposes the object cookie = luacom. exposeObject (COMAppObject); if cookie = nil then error ("ExposeObject failed"); end -- print ("Testobj. lua> ookie = ".. tostring (cookie); end function COM: Register () -- Fills table with registration information local reginfo ={}; reginfo. versionIndependentProgID = "TEST. test "; reginfo. progID = reginfo. versionIndependentProgID .. ". 1 "; reginfo. typeLib = "test. tlb "; reginfo. coClass = "Test"; reginfo. componentName = "Test Component use luacom"; reginfo. arguments = "/Automation"; reginfo. scr IptFile = path_to_script .. "testobj. lua"; -- Stores compnent information in the registry local res = luacom. RegisterObject (reginfo); if res = nil then error ("RegisterObject failed! "); End function COM: UnRegister () -- Fills table with registration information local reginfo ={}; reginfo. versionIndependentProgID = "Test. test "; reginfo. progID = reginfo. versionIndependentProgID .. ". 1 "; reginfo. typeLib = "test. tlb "; reginfo. coClass = "Test"; -- removes compnent information from the registry local res = luacom. unRegisterObject (reginfo); if res = nil then error ("UnRegisterOb Ject failed! "); End -- starts automation server return luacom. detectAutomation (COM); -- xCom = {luacom. detectAutomation (COM) ;}; -- for k, v in pairs (xCom) do -- print (k, v); -- end -- return unpack (xCom); execute two names, one is to generate test. tlb, the other is to register COM to the Registry. After execution, you can go to the Registry to see its registration content maketlb. bat [html] @ rem set MIDLDir = "c: \ Program Files \ Microsoft SDKs \ Windows \ v7.0A \ bin" SET MIDLDir = "d: \ Program Files \ Microsoft SDKs \ Windows \ v6.0A \ bin" SET IDLFile = "% loon % \.. \ lua \ idl \ test. idl "SET TLBFile =" % loon % \.. \ lua \ idl \ test. tlb "@ set path =" % loon % \.. \ lua \ "; % PATH % chdir/d % MIDLDir % midl.exe" % IDLFile % "/tlb" % TLBFile % "/out" % loon % \.. \ lua \ "chdir/d" % loon % \.. \ lua \ idl "lua testobj. lua/register implementation (pure Lua code) and registration have been completed. Now you can use this COM interface. Here we use the LuaCOM script and the VBS script to call it. Test. lua [cpp] package. cpath = OS. getenv ("loon") .. [[\ .. \ lua \?. Dll] require "luacom" obj ={}; print ("testlua. lua> luacom = ".. tostring (luacom); x = luacom. createObject ("TEST. test "); print (" testlua. lua> comobj = ".. tostring (x); for k, v in pairs (x) do www.2cto.com print (k, v); end print (x: Version (4, 5); test. vbs [cpp] on error resume next Set x = CreateObject ("TEST. test ") Dim a, B a = 4 B = 5 x. version a, B MsgBox "Version:" + CStr (a) + ". "+ CStr (B), vbOK can see that the output result is 127 And 460, indicating that the input and output are received, and the modified result is also correctly received.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.