Calling Broker SendDataToBroker

Add-in Express™ Support Service
That's what is more important than anything else

Calling Broker SendDataToBroker
 
nwiscovitch




Posts: 5
Joined: 2019-12-11
Hi,

I have an Advanced Bar extension working on IE11. Part of the actions I need to take is to kill an existing Windows process. Not a problem if I am running the browser in Admin mode but the majority of the users will not run IE in Admin mode.

So, I have added a Broker extension per the instructions on your user guide. But I understand that I cannot set the GUID and call the SendDataToBroker method from and Advanced Bar module, hence I am resorting to using an IEModule. However it seems the Broker is not receiving the data from the method call.

On one of your posts, it was indicated that "You need to use ADXIEAdvancedBar.Module and then ADXIEModule.SendDataToBroker". The Broker Add-In is running because I can see its process in the Task Manager.

Here is my caller code from the Advanced Tab module:

        private string KillProcess(int processID)
        {
            // Kill the passed processID through the broker
            var request = new Hashtable();
            object response;
            request.Add("operation", "killDriverConsole");
            bool brokerResults;
            using (var brokerModule = new ADXIEModule())
            {
                brokerModule.BrokerGuid = "06F4C3E3-0230-4293-B5B0-280226EF43FF";
                brokerResults = brokerModule.SendDataToBroker(request, out response); //This does nothing
            }

            var results = "";
            if (brokerResults)
            {
                // Get the response
                var responseHashtable = (Hashtable)response;
                if (responseHashtable.ContainsKey("result"))
                {
                    results = String.Format("Result={0}", responseHashtable["result"]);
                }
                else
                {
                    results = String.Format("Failed killing process {0}", processID);
                }
            }
            else
            {
                results = String.Format("Failed killing process {0}", processID);
            }

            return results;
        }

    }


And the code on the Broker OnDataReceived event:

        private void IEBrokerModule_OnDataReceived(object sender, AddinExpress.IE.Broker.ADXIEBrokerDataReceivedEventArgs e)
        {
            // Broker requests
            Hashtable request = (Hashtable)e.RequestData;
            Hashtable response = new Hashtable();
            MessageBox.Show(String.Format("Message Received by Broker: {0}", request["operation"]));  //This never shows

            if (request["operation"].ToString() == "killDriverConsole")
            {
                if (request.ContainsKey("processID"))
                {
                    var processID = (int)request["processID"];
                    if (processID != 0)
                    {
                        // Kill it
                        try
                        {
                            Process.GetProcessById(processID).Kill();
                            response.Add("result", "ok");
                        }
                        catch (Exception)
                        {
                            response.Add("result", "exception");
                        }
                    }
                }
            }
            e.ResponseData = response;

        }


Am I missing anything?

Thanks.

Nelson (from Texas, USA)
Posted 11 Dec, 2019 11:00:03 Top
Andrei Smolin


Add-in Express team


Posts: 17239
Joined: 2006-05-11
Hello Nelson,

Make sure you invoke SendDataToBroker *after* the OnBrokerStarted event occurs on the module.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 12 Dec, 2019 06:42:26 Top
nwiscovitch




Posts: 5
Joined: 2019-12-11
Thanks. How do I start the Broker module from code? I have changed the code to the following, but the broker EXE does not start:

From my Advanced Tab caller, I changed it so the following occurs during Advanced Bar Load event:


       // I already initialized brokerModule in the AdvancedBar class:  public ADXIEModule brokerModule = new ADXIEModule();
       brokerModule.BrokerGuid = "06F4C3E3-0230-4293-B5B0-280226EF43FF";
       brokerModule.OnBrokerStarted += new EventHandler(IEModule_OnBrokerStarted);
       brokerModule.RunBrokerApplication();  // Doesn't do anything



And added the event handler routine:

        
        private void IEModule_OnBrokerStarted(object sender, EventArgs e)
        {
            // Broker started
            Console.WriteLine("Broker App Started");  //Never fires

        }


And then I execute my routine by calling this:


private string KillProcess(int processID) 
    { 
        // Kill the passed processID through the broker 
        var request = new Hashtable(); 
        object response; 
        request.Add("operation", "killDriverConsole"); 
        request.Add("processID", processID); 
        bool brokerResults;
 
        // How do I verify if the broker app is running?
        brokerResults = brokerModule.SendDataToBroker(request, out response); //This does nothing 
 
        var results = ""; 
        if (brokerResults) 
        { 
            // Get the response 
            var responseHashtable = (Hashtable)response; 
            if (responseHashtable.ContainsKey("result")) 
            { 
                results = String.Format("Result={0}", responseHashtable["result"]); 
            } 
            else 
            { 
                results = String.Format("Failed killing process {0}", processID); 
            } 
        } 
        else 
        { 
            results = String.Format("Failed killing process {0}", processID); 
        } 
 
        return results; 
    } 
 
} 


I have confirmed the broker app is in the same directory of the other files and it was properly registered at HKLM\SOFTWARE\Microsoft\Internet Explorer\Low Rights\ElevationPolicy\{06F4C3E3-0230-4293-B5B0-280226EF43FF} But the broker never starts.

Please advise

Nelson
Posted 12 Dec, 2019 09:25:11 Top
Andrei Smolin


Add-in Express team


Posts: 17239
Joined: 2006-05-11
Hello Nelson,

I assume you've created a broker application in the solution where the IE add-on project locates. If so, open the designer of the IE module and set the BrokerGuid property; see the Properties window.

In my case, the BrokerGuid property editor shows a drop down. I click it and it displays the guid of the broker application: "MyIEBrokerApp1: {30D71544-8CC6-4461-9CCA-8758D56F66D9}". I select this row and the guid gets written to the property. Does this work for you?

nwiscovitch writes:
How do I start the Broker module from code?


I don't think it is possible to start the broker in this way.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 12 Dec, 2019 10:17:04 Top
Nelson Wiscovitch




Posts: 5
Joined: 2019-12-11
Thank you. Yes, I did had the broker guid specified in the IEModule. And when doing so, it starts the broker app.

But then, how do I call the SendDataToBroker method from my Advanced Bar code? When I try to refer to the IEModule from the Advanced Bar code, it is not allowed as it says that "An object reference is required for the non-static method ADXIEModule.SendDataToBroker".

Please advise.

Regards,

Nelson
Posted 12 Dec, 2019 10:57:04 Top
Nelson Wiscovitch




Posts: 5
Joined: 2019-12-11
Andrei... I was able to figure it out.

For everyone's benefit, here is how I was able to call SendDataToBroker from and Advanced Bar module:

1. You need to set the Broker's app GUID at the IEModule's BrokerGuid property. This will make the Broker's app launch automatically when the extension is loaded

2. Add an "IEBrokerModule_OnDataReceived" event like described in the User's guide. For example, in my case it looked like this:

private void IEBrokerModule_OnDataReceived(object sender, AddinExpress.IE.Broker.ADXIEBrokerDataReceivedEventArgs e) 
{ 
    // Broker requests 
    Hashtable request = (Hashtable)e.RequestData; 
    Hashtable response = new Hashtable(); 
    Console.WriteLine(String.Format("Message Received by Broker: {0}", request["operation"])); 
 
    if (request["operation"].ToString() == "killDriverConsole") 
    { 
        if (request.ContainsKey("processID")) 
        { 
            var processID = (int)request["processID"]; 
            if (processID != 0) 
            { 
                // Kill it 
                try 
                { 
                    Process.GetProcessById(processID).Kill(); 
                    response.Add("result", "ok"); 
                } 
                catch (Exception) 
                { 
                    response.Add("result", "exception"); 
                } 
            } 
        } 
    } 
    e.ResponseData = response; 
 
}


3. In my Advanced Bar module, I then call the SendDataToBroker method like this:

        public string KillProcess(int processID)
        {
            string results;
            // Kill the passed processID through the broker

            // Here is the code that makes it work!
            if (IEModule.CurrentInstance.Count == 0)
            {
                results = String.Format("Could not kill process {0}", processID);
                return results;
            }
            // I get the current instance of the IE Module to use for calling SendDataToBroker
            var ieModule = (ADXIEModule)IEModule.CurrentInstance.GetByIndex(0);  

            var request = new Hashtable();
            object response;
            request.Add("operation", "killDriverConsole");
            request.Add("processID", processID);

            bool brokerResults = ieModule.SendDataToBroker(request, out response);  // Viola!  It works!

            if (brokerResults)
            {
                // Get the response
                var responseHashtable = (Hashtable)response;
                if (responseHashtable.ContainsKey("result"))
                {
                    results = String.Format("Result={0}", responseHashtable["result"]);
                }
                else
                {
                    results = String.Format("Failed killing process {0}", processID);
                }
            }
            else
            {
                results = String.Format("Failed killing process {0}", processID);
            }

            return results;
        }

    }


Thanks again Andrei for pointing me in the right direction. I do have one more question. When getting the IEModule current instance, it always returns 2 instances. So I always pick the first one (0). Does it matter?

Thanks and cheers,

Nelson (from Texas, USA)
Posted 12 Dec, 2019 12:21:30 Top
Andrei Smolin


Add-in Express team


Posts: 17239
Joined: 2006-05-11
Hello Nelson,

The description of this property says:

A SortedList that contains all instances of the add-on. The index of the list is a thread Id.


The class reference should be embedded in your VS Help; you may need to select Help | Set Help Preference | Launch in Help Viewer in the main menu of the IDE. Or, you may prefer to use the CHM version of the class reference; find it at https://www.add-in-express.com/downloads/documentation.php.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 13 Dec, 2019 02:38:18 Top