COM plugins general questions (SOLVED)

Started by HeirOfNorton, Sun 20/02/2005 20:18:35

Previous topic - Next topic

HeirOfNorton

Hey, I've just started playing around with VB6 (I'm thinking of making a more useful dialog editor than what is included with AGS, we'll see) and I've come across on odd problem...
I just followed the example from the COM plugin tutorial. Basically copy and paste. The first part, just getting it initialized worked fine. When I added the code to add in a menu option, it still worked fine upon loading the Plugin, but when I DISABLED the plugin in the manager, I got this error message:

Quote
A serious error occured while communicating with plugin 'AGS_Testy.ocx' in method 'EditorShutdown'. Please ensure you have the correct plugin version for this AGS version.

It still unloaded fine, otherwise. Here's the odd part. While just trying different things, I commented out the code that goes in DisablePlugin that removes the menu option. Lo and behold, it now works fine, and even removes the option upon unloading the plugin even without specifically coding it to do so.

This happens in both AGS 2.62 and the current beta.

So, is the example wrong, and editor.Menu(0).RemoveMenuOption does NOT need to be called, or is something weird going on?

HoN

P.S. Do you have any plans of expanding the COM Plugin docs, either with more details on the available methods/members, or with a tutorial on setting one up in C++? (Not that it makes THAT big a difference, as I'm using Dev-C++ and can't really make any COM plugins with it anyway, just wondering.)
Quote

Pumaman

Quote from: HeirOfNorton on Sun 20/02/2005 20:18:35
So, is the example wrong, and editor.Menu(0).RemoveMenuOption does NOT need to be called, or is something weird going on?

Did you save the menu option ID and pass it back to RemoveMenuItem? (and you did call it RemoveMenuItem not Option, right?)

As you've discovered, you don't strictly have to call Remove because the editor will do it automatically, but it's still good practice to do so.

Quote
P.S. Do you have any plans of expanding the COM Plugin docs, either with more details on the available methods/members, or with a tutorial on setting one up in C++? (Not that it makes THAT big a difference, as I'm using Dev-C++ and can't really make any COM plugins with it anyway, just wondering.)

I don't currently have any further plans for the COM plugin support, but if it becomes popular then I may do so. Basically, there's no point putting lots of effort into something that people don't really use.

In terms of writing a C++ tutorial I don't intend to do so, since anyone who's capable of writing a plugin in C++ should be able to work out how to set it up themselves -- basically I don't want to encourage badly written C++ plugins based on a template.

HeirOfNorton

#2
Quote
Did you save the menu option ID and pass it back to RemoveMenuItem? (and you did call it RemoveMenuItem not Option, right?)

Heh, yeah I did. Like I wrote, I basically did a copy-and-paste from the tutorial (more to see if it works with my software, than to learn how to do it myself at this point). Here's the relevant code snippet from the PluginMain class:

Quote
Private menuOptionID As Long

Private Function AGSPlugin_EnablePlugin(ByVal pEditor As AGSEditor.ITheAGSEditor) As Boolean
    ' Save a reference to the editor for future use
    Set editor = pEditor
   
     Call editor.Menu(0).AddMenuItem("-", "")
     menuOptionID = editor.Menu(0).AddMenuItem("Extra Plugin Option", "Clicking this calls the plugin")
   
    ' Tell the editor everything is A-OK to continue
    AGSPlugin_EnablePlugin = True
End Function

Private Sub AGSPlugin_DisablePlugin()
    editor.Menu(0).RemoveMenuItem menuOptionID
End Sub

I've tried a couple variations with the RemoveMenuItem line, adding Call to the beginning and/or putting the menuOptionId in parenthesis, but it seems to make no difference whatsoever. I still get that error message (and it still otherwise seems to work fine).

Nevermind, I figured it out. I knew I had to be doing something n00bish. I remembered to declare menuOptionID at the top (so everything could use it) but forgot to declare the editor up there as well. When DisablePlugin got called, it tried to call editor.Menu(0).etc..., but editor was out of scope here.

Thanks for the quick reply, though.
HoN



Scorpiorus

QuoteI remembered to declare menuOptionID at the top (so everything could use it) but forgot to declare the editor up there as well. When DisablePlugin got called, it tried to call editor.Menu(0).etc..., but editor was out of scope here.
That's a bad thing with VB. It actually allows using of variables that weren't even declared which makes, for instance, bugs being connected with typos in naming of variables a real pain to trace.
So, in order to make VB always require a declaration put at the very top of a module:

Option Explicit

It can also automatically put that line on creating of a new module, if you tick a Require Variable Declaration option (Menu Tools -> Options -> Editor tab -> Code settings panel).

Pumaman

Also, if you're doing any serious work in VB, it's a good idea to wrap each function in an error handler so that you can track down problems:

Private Sub AGSPlugin_DisablePlugin()
    On Error Goto ErrHandler

    editor.Menu(0).RemoveMenuItem menuOptionID

    Exit Sub
ErrHandler:
    MsgBox "Error: " & Err.Description

End Sub

SMF spam blocked by CleanTalk