Calling events registered with a COM object

5 Ansichten (letzte 30 Tage)
kaser
kaser am 6 Sep. 2019
Kommentiert: Noah Safra am 19 Nov. 2021
I am trying to call an Event from a COM application. I'm able to run the Functions and Subroutines within the program, and I've figured out how to find the Events within the application, but can't figure out how to access them.
A little more specific detail on the Event -- it's called "ComputeMessageEvent" and outputs computation messages during the computation. For applications within excel (this is also coded in VBA, I'm trying to access it from Matlab), I have to instantiate the program "with Events" in a class module or in a sheet, at which point the subroutine ComputeMessageEvent would become available for code.
Sample Code below:
>> h = actxserver('RAS503.HECRASCONTROLLER'); %Call the application
>> h.events % output below
ComputeProgressEvent = void ComputeProgressEvent(single Progress)
ComputeMessageEvent = void ComputeMessageEvent(ustring eventMessage)
ComputeComplete = void ComputeComplete()
The events are unregistered, as
>> info = eventlisteners(h)
info =
0×0 empty cell array
I've tried to register the events, and I'm thinking I don't know how to input the event handler correctly. A lot of examples I've found on the web use a mouse click or a keystroke as the event handler, but this code would be automated and I wouldn't want to wait for human input. I tried to make the event handler the application (among other things), but it doesn't seem to work.
>> ras_file = [pth,'\filename.prj']; % name project file to run
>> h.Project_Open(ras_file); % open project file in application
>> registerevent(h,'h');
>> info = eventlisteners(h)
info =
3×2 cell array
{'ComputeProgressEvent'} {'h'}
{'ComputeMessageEvent' } {'h'}
{'ComputeComplete' } {'h'}
% I've also tried other "event handlers" but always get the same error
>> h.Compute_CurrentPlan(0,0) % Run the application
Error using comeventcallback (line 24)
Error firing event 'ComputeMessageEvent' to 'h'.
Warning: Error occurred while evaluating listener callback.
Error using comeventcallback (line 24)
Error firing event 'ComputeMessageEvent' to 'h'.
Warning: Error occurred while evaluating listener callback.
Error using comeventcallback (line 24)
Error firing event 'ComputeMessageEvent' to 'h'.
Warning: Error occurred while evaluating listener callback.
Error using comeventcallback (line 24)
Error firing event 'ComputeMessageEvent' to 'h'.
Warning: Error occurred while evaluating listener callback.
% The program runs, but the events don't work
If anyone has insight into how to appropriately run/call the events and/or how I may be coding this up incorrectly, it would be very helpful. This is the first time I'm using COM applications through Matlab so I'm still new to the jargon/inner workings of the programming.

Antworten (1)

Noah Safra
Noah Safra am 19 Nov. 2021
Hi Caser!
1st: you can register to a single event or regsiter to all events with a single cllback (what you did above)
% Regsiter to a single callback:
registerevent(h,{'ComputeComplete',@ComputeComplete_callback);
% Register to all events with a single callback:
registerevent(h,@global_callback);
%see all allocated evels:
info = eventlisteners(h)
Please feedback if OK
Noah
  3 Kommentare
Noah Safra
Noah Safra am 19 Nov. 2021
.. and an example of callback:
function ComputeComplete_callback(obj,varargin)
% See https://www.mathworks.com/help/matlab/matlab_external/com-event-handlers.html
% obj is the COM object
eventID = varargin{1};
eventArgs = varargin(2:end-2);
eventName = varargin{end};
eventStructure = varargin{end-1};
...
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Use COM Objects in MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by