beckhoff TE1000 TwinCAT 3 — 4.2.5 Implementing a COM Message Filter

Configuration | Basics { Console.WriteLine(string.Format("CoRegisterMessageFilter failed with error : {0}", test)); } } public static void Revoke() { IOleMessageFilter oldFilter = null; int test = CoRegisterMessageFilter(null, out oldFilter); } int IOleMessageFilter.HandleInComingCall(int dwCallType, System.IntPtr hTaskCaller, int dwTickCount, System.IntPtr lpInterfaceInfo) { //returns the flag SERVERCALL_ISHANDLED. return 0; } int IOleMessageFilter.RetryRejectedCall(System.IntPtr hTaskCallee, int dwTickCount, int dwRejectType) { // Thread call was refused, try again. if (dwRejectType == 2) // flag = SERVERCALL_RETRYLATER.

{ // retry thread call at once, if return value >=0 & // return 99; } return -1; } int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee, int dwTickCount, int dwPendingType) { //return flag PENDINGMSG_WAITDEFPROCESS. return 2; } // implement IOleMessageFilter interface. [DllImport("Ole32.dll")] private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter); An application engineer now only needs to call the Register() and Revoke() methods from another class to initialize and discard the MessageFilter. The result is that rejected COM calls are repeated as specified in the RetryRejectedCall() method.

The following code snippet shows how to call these methods in a console application written in C#. As mentioned above, console applications run in an MTA thread by default. For this reason, the Main() method must be configured to run in an STA apartment so that the message filter can be applied. [STAThread] static void Main(string[] args) { MessageFilter.Register(); /* ============================================================= * place COM calls for the Automation Interface here * ... * ...

* ============================================================= */ MessageFilter.Revoke(); } If you try to apply a message filter to an application running in the MTA apartment, the following error is issued when trying to execute the CoRegisterMessageFilter() method during runtime: TE1000 30 Version: 1.7.0