Results 1 to 22 of 22

Thread: [RESOLVED] Automating Host through an AddIn

  1. #1

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Resolved [RESOLVED] Automating Host through an AddIn

    Through this blog I've learned how to extend my application. I can call methods in my Add-ins but how can I call methods in my Host Application through the Add-in?

    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    The application would have to pass a reference to some object that exposed the desired methods into the plug-in. That object would implement an interface that is defined in the same location as your plug-in interface.

    A better option would be for the plug-in to raise an event that the host can then handle and act on. These events would be declared in your plug-in interface.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    Quote Originally Posted by jmcilhinney View Post
    The application would have to pass a reference to some object that exposed the desired methods into the plug-in. That object would implement an interface that is defined in the same location as your plug-in interface.
    That is the first thing come accross in my mind but the problem is I need to create a new instance of that class in my add-ins and I won't be able to access certain variable that my host application loaded.
    Quote Originally Posted by jmcilhinney View Post
    A better option would be for the plug-in to raise an event that the host can then handle and act on. These events would be declared in your plug-in interface.
    I think, what I need is my add-in must have direct access to the host application's methods.
    Now I am trying to reverse the operation but I think I'm already stuck.

    I hope there are tutorials or samples available on how to do it.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    Quote Originally Posted by eSPiYa View Post
    That is the first thing come accross in my mind but the problem is I need to create a new instance of that class in my add-ins and I won't be able to access certain variable that my host application loaded.
    Of course you don't. If you did it would be useless. I specifically said that the application must pass the object to the plug-in, which it would do by setting a property or calling a method and passing an argument, which are the very same ways you get any data into any object.
    Quote Originally Posted by eSPiYa View Post
    I think, what I need is my add-in must have direct access to the host application's methods.
    Now I am trying to reverse the operation but I think I'm already stuck.

    I hope there are tutorials or samples available on how to do it.
    Um, how exactly can the plug-in have direct access to the application without the application passing the plug-in a reference to an object of some sort? That was my the first suggestion.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    Thanks.
    But can you give me a sample on how to do it?
    I'm still new in this.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    Define a property in your plug-in interface, implement it in your plug-in classes and set it in your application. That's it. I would assume that you're pretty much doing that already, so all you have to decide is exactly what type this object will be and how to implement it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    Thanks.

    I've already created a test Interface in the contract part.
    Maybe I can reverse the process of calling methods.

    I'll update you later.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    Quote Originally Posted by eSPiYa View Post
    Thanks.

    I've already created a test Interface in the contract part.
    Maybe I can reverse the process of calling methods.

    I'll update you later.
    As I said, I don't think that that's the best idea. Is there a specific reason that you can't just raise an event. That's going to invoke a method in the application as surely as calling it directly will because the application will attach the appropriate event handlers when it loads a plug-in. It also means that your plug-ins can remain blissfully unaware of the application that they're part of, as they should do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    I'm sorry but I thought I need to create that property on the Contract part.

    Anyway, the aim of this project is I need to build a plug-in that hosts a socket-based service. Whenever it receives data it will pass those data to the Host application through calling a method that resides on the Host.

    Thanks.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    If you want the plug-in to be able to call a method of "the application" directly then the plug-in has to be aware of the type that represents the application. That would mean defining another interface to represent the application and then your plug-in interface would have to have a property of that type. The host app would then pass an object of that type to the plug-in so the plug-in can call its methods.

    If you go the option I'm recommending then you don't need the extra interface and your plug-in interface simply adds some events and your plug-in raises those events when it needs to notify the application that something has or should happen. The application will then act accordingly. For instance, instead of the app exposing a Beep method that the plug-in would call, the plug-in would raise a Beep event,, which the app would handle and then make a Beep sound.

    It's exactly the same as having a Button on your form to perform some action. When the user clicks the Button it doesn't call a method of your form does it? No, it doesn't. It simply raises its Click event. It's up to the form to handle that event and then act accordingly.

    You should also note that, if you need to pass data from the plug-in to the app, you can do so through the event's arguments.

    I would suggest that you do a bit of reading on defining and raising events.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    I'm sorry if I got confused.

    Passing reference of an object through pipeline from host to the add-in still needs a contract because it would become a complex data object. The only supported types that can be transfer between the host application and add-in are basic data-types.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    That's exactly what I said. You would have to define an additional interface and then declare a property of that type in the plug-in interface. The application would instantiate a class that implements that interface and then set the property of the plug-in.

    I should point out that I haven't actually read that blog. I wasn't previously aware that VS provided such functionality and was speaking in the general sense.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    That's exactly what I said. You would have to define an additional interface and then declare a property of that type in the plug-in interface. The application would instantiate a class that implements that interface and then set the property of the plug-in.

    I should point out that I haven't actually read that blog. I wasn't previously aware that VS provided such functionality and was speaking in the general sense. I might have to read that and see how it works specifically.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    Thanks.

    I'll post link to another blog that teaches how to pass complex data between Host Application and Add-in.

    After trying your suggestion, I'll post if it will become successful but I've read somewhere that referencing an object across the pipeline might cause problem. It is something about the lifetime of a token. I'm not sure about it.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    I've been through that blog and implemented the code as described. I haven't tried to adapt that code myself but I think the key to what you want to do can be found in the Host Side Adapter and Add In Adapter sections. Near the top of the Host Side Adapter section it says:
    In this sample, the host calls methods on the add-in, and not the opposite. Therefore, we only need an adapter from the Contract to the Host View.
    while near the top of the Add In Adapter section it says:
    In this sample, the host calls methods on the add-in, and not the opposite. Therefore, we only need an adapter from the Add-In View to the contract.
    That suggests to me that, in order for the add-in to call methods on the host, you would need to implement a host side adapter from the Host View to the Contract and another from the Contract to the Add-In View.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    I would try that too.
    Thanks.

  17. #17

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    I have both adapters but I don't know how to make a constructor of an abstract class in the Add-in side.

    This is my test Class in the add-in side Code:
    1. public abstract class HostClassView
    2.     {
    3.         public abstract AuthData Authenticate(string UserName, string Password);
    4.     }
    I need to make a new instance of this class in-order to call its method(Authenticate).

    The implementation of the method in the host-side will just look for shared/internal lists in other class so it is fine to create the new instance in the add-in side.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    You can't instantiate an abstract class. An abstract class is like an interface except that, instead of just defining the set of functionality that implementing classes will have, an abstract class actually implements some or all of that functionality. Still, in order for an instance to be created the abstract class must be inherited by a non-abstract class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    Is it possible to create a new class base on the abstract class without overriding its methods?
    I think this should be taken in the consideration because it was already overridden in the adapter class.
    And the constructor method might become empty.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Automating Host through an AddIn

    When inheriting an abstract class you MUST override every abstract member or else your derived class must also be declared abstract, which wouldn't help you at all. Any members that are already implemented can be overridden or not as is appropriate for each one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    So, reversing the operation would be too complicated as of now.
    Passing reference is complicated if passed through the pipeline but working though I'm experiencing some problem with complex data.

  22. #22

    Thread Starter
    Fanatic Member eSPiYa's Avatar
    Join Date
    Jun 2006
    Location
    in our house
    Posts
    751

    Re: Automating Host through an AddIn

    Thanks for the help.
    I'm successfully call methods of the host application from my Add-in through passing reference of it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width