[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]  Jscript
. . -> [Subject]  COM objects
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

COM objects

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


Pages: 1 2  

Posted by Anaristos   USA  (24 posts)  [Biography] bio
Date Fri 04 Feb 2011 01:23 AM (UTC)

Amended on Fri 04 Feb 2011 01:24 AM (UTC) by Anaristos

Message
I have written .NET applications which are exposed COM. I am able to use them in JScript scripts. What I would like to know is how can I make these COM references global to the session rather than having to re-establish the connection each time the script is invoked. From what I read the global variables are all strings which for obvious reasons don't help in this situation.

Sic itur ad astra
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Fri 04 Feb 2011 02:11 AM (UTC)
Message
I think you are confusing two sorts of variables. MUSHclient world variables are strings, but you get at these with the GetVariable and SetVariable function calls.

Normal Jscript variables could be any type, including at the global level.

- Nick Gammon

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

Posted by Anaristos   USA  (24 posts)  [Biography] bio
Date Reply #2 on Fri 04 Feb 2011 03:23 AM (UTC)

Amended on Fri 04 Feb 2011 04:05 AM (UTC) by Anaristos

Message
Oh, I understand. What I am trying to do is to have the reference to the COM object as a global variable so that other scripts may reference the same object.
Or, are you saying that what is saved with SetVariable() is the name of the variable and GetVaraiable(variable_name) retrieves the actual value?

Sic itur ad astra
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Fri 04 Feb 2011 04:29 AM (UTC)
Message
Er, no.

When you say "other scripts" what do you mean exactly? Other functions in your main script file? Or are you using plugins?

Plugins all have independent script spaces (they may even be different languages, such as Lua, VBscript, Python etc.). Thus there is no way to share a global variable like a COM object between them.

Personally I wouldn't be trying to mix .NET with MUSHclient scripts, but that's just me. :)

One approach could be to have a single plugin "talk" to the .NET COM object, and the other plugins get this single plugin to do stuff (by using CallPlugin etc.).

- Nick Gammon

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

Posted by Anaristos   USA  (24 posts)  [Biography] bio
Date Reply #4 on Fri 04 Feb 2011 06:15 AM (UTC)

Amended on Fri 04 Feb 2011 07:03 AM (UTC) by Anaristos

Message
Oh, I get the picture :) I was thinking that I could have a global variable space which could be shared by all the scripts that were called by aliases, etc. From what I can see by my few scripting experiments once the script is finished everything goes out of scope and it doesn't remember anything it did on previous calls. This is essentially what I was trying to avoid. I was really looking to persist variables, COM references in particular. I do understand the mechanism now, though. I will look at how plug-ins work. Thanks.

Sic itur ad astra
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Fri 04 Feb 2011 06:56 AM (UTC)
Message
Ah, no, that doesn't happen. I'm not sure what you mean by "once the script is finished". What script? Small scripts can be run from "send to script" for individual aliases/triggers/timers. Also they can call functions defined in the script file (if you are using one). However these share the same scripting space/engine.

This is all pretty vague, perhaps some concrete examples would help. But in principle, the main script space is not re-initialized, nor do variables go out of scope, for the entire execution of the program. Excepting, however, if you do "reload script file" from the menu.

Anaristos said:

I was thinking that I could have a global variable space which could be shared by all the scripts that were called by aliases, etc.


Yes you should be able to do that. If you are experiencing problems, please post a demonstration script.

- Nick Gammon

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

Posted by Anaristos   USA  (24 posts)  [Biography] bio
Date Reply #6 on Fri 04 Feb 2011 07:19 AM (UTC)

Amended on Fri 04 Feb 2011 07:23 AM (UTC) by Anaristos

Message
OK, let me see if I can explain:

Let's say I have an alias which is sent to script. The script resides in an external file and it looks something like this:

function cominit(name, line, wildcards)
{
    var args = VBArray(wildcards).toArray();

    var appObject = "dmsManager.Agent";

    var COMObject = new ActiveXObject(appObject);

/*
    properties are set here to guide COM the application.
*/
}


My question is: Can I refer to COMObject, or use it rather, from another script invoked by another alias?

Sic itur ad astra
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Fri 04 Feb 2011 07:22 AM (UTC)
Message
Can you post the alias?

Template:copying For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.


In any case, you have declared COMObject locally to that function. It isn't a global variable.

- Nick Gammon

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

Posted by Anaristos   USA  (24 posts)  [Biography] bio
Date Reply #8 on Fri 04 Feb 2011 07:41 AM (UTC)
Message
This is all that the alias does, call the COM initialization routine:


<aliases>
  <alias
   name="dbInit"
   script="cominit"
   match="^dbinit$"
   enabled="y"
   omit_from_log="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  </alias>
</aliases>


and yes, that was my question all along. How can I make COMObject global?

Sic itur ad astra
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #9 on Fri 04 Feb 2011 07:59 AM (UTC)
Message
You don't need to send to script if you are putting a script name in the script box. Sending to script is for a literal script in the send box.

Now that we see what you are attempting it is easier to answer. Instead of:


function cominit(name, line, wildcards)
{
    var args = VBArray(wildcards).toArray();

    var appObject = "dmsManager.Agent";

    var COMObject = new ActiveXObject(appObject);

/*
    properties are set here to guide COM the application.
*/
}


Just move the variable out of the function. Variables inside a function only persist during the function execution. eg.



var COMObject;  // this is a global variable


function cominit(name, line, wildcards)
{
    var args = VBArray(wildcards).toArray();

    var appObject = "dmsManager.Agent";

    //Note: no "var" here - this is just assigning a value
    COMObject = new ActiveXObject(appObject);

/*
    properties are set here to guide COM the application.
*/
}


- Nick Gammon

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

Posted by Anaristos   USA  (24 posts)  [Biography] bio
Date Reply #10 on Fri 04 Feb 2011 08:06 AM (UTC)
Message
Oh, I see. Just as if I were scripting a web page. Gotcha! Thanks. :)

Sic itur ad astra
[Go to top] top

Posted by Anaristos   USA  (24 posts)  [Biography] bio
Date Reply #11 on Sat 05 Feb 2011 01:02 AM (UTC)

Amended on Sat 05 Feb 2011 01:09 AM (UTC) by Anaristos

Message
OK, things are not happening yet, and here is the core problem. Please forgive my ignorance. I understand what the change you made to the script works as it is a standard way to make global variables. However, persistence still seems to be the issue. As you see, the script only creates the COM connection and nothing else. Now, some other aliases are going to attempt to use this object for their purposes. However when I try I get the "'COMObject' is null or not an object" message. This tells me that the global variable that I created is not remembered by the scripting engine, most likely because it exits after it finishes running each script. Is there a way around this?

The script that gets the error is as follows:

function dbopen(name, line, wildcards)
{
    dbName = VBArray(wildcards).toArray()[0];
	
    COMObject.open(dbName); //I must have COMObject previously initialized.
	
    if (COMObject.isOpen) world.note("\nDatabase " + COMObject.path + " is ready for operations.\n");
	
	else world.note("\nAn error occurred: " + COMObject.lasterror + "\n");
}


That is what I meant by persistence. Each time a global variable is initialized, the value persists until the session is finished so each time I run a script that requires a variable initialized by the invocation of a previous script, the value is available.

Sic itur ad astra
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Sat 05 Feb 2011 01:41 AM (UTC)
Message
You are just showing snippets here. First you show me cominit function, then this time dbopen function.

There is no indication of which functions are called, and in which order.

As far as I can see, cominit creates the COM object. Does it get called?

It would help to see all the things that are happening here. What are your aliases? What order do you call them in?

- Nick Gammon

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

Posted by Anaristos   USA  (24 posts)  [Biography] bio
Date Reply #13 on Sat 05 Feb 2011 06:20 AM (UTC)

Amended on Sat 05 Feb 2011 06:27 AM (UTC) by Anaristos

Message
OK, let me explain this another way:

A skeleton of what I want to do is a follows:

2 aliases... dbinit and dbopen (there are more but the pattern is the same):


<aliases>
  <alias
   name="dbInit"
   script="cominit"
   match="^dbinit$"
   enabled="y"
   omit_from_log="y"
   regexp="y"
   ignore_case="y"
   sequence="100"
  >
  </alias>
 <alias
   name="dbOpen"
   script="comopen"
   match="^dbopen\s+([^$]+)$"
   enabled="y"
   omit_from_log="y"
   regexp="y"
   ignore_case="y"
   sequence="100"
  >
  </alias>
</aliases>

</aliases>

The first alias just invokes the script that initializes the COM object. It is separate because the same object can spawn different instances of the application. One for each database.

var COMObject;

function cominit(label, line, wildcards)
{
    args = VBArray(wildcards).toArray();
	
    var appObject = "dmsManager.Agent";

    COMObject = new ActiveXObject(appObject);
}

This script is invoked by dbinit.

Then there is this script:


var dbObject;

function comopen(name, line, wildcards)
{
     var dbName = VBArray(wildcards).toArray()[0];

     dbObject = COMObject.start(); //spin out an instance.

     dbObject.open(dbName);
	
     if (dbObject.isOpen) world.note("\nDatabase " + dbObject.path + " is ready for operations.\n");
	
     else world.note("\nAn error occurred: " + dbObject.lasterror + "\n");
}

This script is invoked by dbopen.
However, when dbopen is called, it doesn't know anything about COMObject.
Each script is in its own file.

So what I want to happen is that both COMObject and dbObject are available anytime an alias requiring their services is executed.

Sic itur ad astra
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #14 on Sat 05 Feb 2011 08:20 PM (UTC)
Message
Anaristos said:

Each script is in its own file.


I don't understand that part. MUSHclient supports a single script file for the "main" world scripting, plus plugins. Are you saying that each alias is in a different plugin?

- 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.


19,800 views.

This is page 1, subject is 2 pages long: 1 2  [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]