Announcement

Collapse
No announcement yet.

How to use PluginFunction?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use PluginFunction?

    Does anyone know how to use the PluginFunction returning objects that are more complex than strings/ints and arrays of these?

    I have tried to return a simple class with a string and an int, but when I call this from a script I get the error: Object reference not set to an instance of an object.
    If I return and array of strings everything works as expected.

    I have tried in both vb.net(Moskus plugin) and c# (using the HSPI extension)

  • #2
    From HSPI_SAMPLE:
    Code:
    Public Function PluginFunction(ByVal proc As String, ByVal parms() As Object) As Object Implements IPlugInAPI.PluginFunction
        Try
            Dim ty As Type = Me.GetType
            Dim mi As MethodInfo = ty.GetMethod(proc)
            If mi Is Nothing Then
                Log("Method " & proc & " does not exist in this plugin.", LogType.LOG_TYPE_ERROR)
                Return Nothing
            End If
            Return (mi.Invoke(Me, parms))
        Catch ex As Exception
            Log("Error in PluginProc: " & ex.Message, LogType.LOG_TYPE_ERROR)
        End Try
    
        Return Nothing
    End Function

    Comment


    • #3
      From HSPI_CsharpSample:
      Code:
      public object PluginFunction(string procName, object[] parms)
      {
          try
          {
              Type pluginType = this.GetType();
              MethodInfo methodInfo = pluginType.GetMethod(procName);
              if (methodInfo == null)
              {
                  _utils.Log("Method " + procName + " does not exist in this plugin.", LogType.Error);
                  return null;
              }
              return (methodInfo.Invoke(this, parms));
          }
          catch (Exception ex)
          {
              _utils.Log("Error in PluginProc: " + ex.Message, LogType.Error);
          }
          return null;
      }

      Comment


      • #4
        Did you mark the classes serializable?

        Comment


        • #5
          Alex you might want to reread the question.

          Comment


          • #6
            Originally posted by bsobel View Post
            Alex you might want to reread the question.
            Hi Bill, I did read the question. The explanation from the example I posted is - PluginFunction doesn't return a class - it returns a result of methodInfo.Invoke

            Comment


            • #7
              Originally posted by alexbk66 View Post

              Hi Bill, I did read the question. The explanation from the example I posted is - PluginFunction doesn't return a class - it returns a result of methodInfo.Invoke
              Yes it absolutely does, Object is a class. It's the basic class concept of the entire CLR. Some items the OP asked about (strings) are also classes. In fact the question is literally "returning objects that are more complex than strings" We all know Invoke returns an object, the OP is asking how to return a complex object that is not part of the framework. Also, posting code snippet with no context isn't really an explanation (IMHO). The main issue tends to be the serialization problem, as HS needs to know about the objects. One workaround is to serialize it yourself into a byte[] and return that, deserializing on the other end.

              Comment


              • #8
                Thanks for all the tips. If you have to serialize the result into a array of bytes and deserialize it on the other end it is not very "script" friendly is it? The reason for asking this question was to give users the opportunity to use a method in a plugin in scripts.

                Comment


                • #9
                  Originally posted by Guahtdim View Post
                  Thanks for all the tips. If you have to serialize the result into a array of bytes and deserialize it on the other end it is not very "script" friendly is it? The reason for asking this question was to give users the opportunity to use a method in a plugin in scripts.
                  If you want to stay script friendly then simple object work best. Many cases a Dictionary can be returned that covers many many cases. If you need a complex object, the files to access it will be required to be installed everywhere you run your plugin (which is easy if its just the HS machine, a bit more of a pain if you run remote plugins)

                  Comment


                  • #10
                    Originally posted by bsobel View Post
                    posting code snippet with no context isn't really an explanation (IMHO).
                    So Bill bsobel , what's your useful explanation? Any working code example appreciated.

                    Comment


                    • #11
                      Originally posted by alexbk66 View Post
                      So Bill bsobel , what's your useful explanation? Any working code example appreciated.
                      I answered the original posters question, for scripting he will need to stay with simply objects. Not sure why you are brining up a post from months ago when the original poster has the answer he needs. If you need to ask something, please start a new thread about it.

                      Comment


                      • #12
                        bsobel that's exactly the problem, the post is from months ago, but still no useful answer. You know the original posters question wasn't answered - he opened another post yesterday about same problem. https://forums.homeseer.com/forum/ul...ray-of-classes

                        And we spent last two days with him (externally to this forum) - and eventually we worked out how to get it working. And I tried to help him out of interest, I don't need it in my plugin. But in general I find this forum not very supportive, and generic comments and critics doesn't really help to solve the particular problem on hands.

                        Comment


                        • #13
                          If he didn’t feel it was answered he should have responded as such.

                          Comment


                          • #14
                            Originally posted by bsobel View Post
                            If he didn’t feel it was answered he should have responded as such.
                            Obviously he didn't have much hope?

                            Comment


                            • #15
                              Alex, his question on how to best interact with scripts was answered. If the OP has an issue, lets let him weigh in. You seem to have an axe to grind for god knows what reason.

                              Comment

                              Working...
                              X