__QUERYINTERFACE
The operator is an extension of the IEC 61131-3 standard.
At runtime, the operator performs a type conversion of one interface reference to another. The operator returns a result of type BOOL. TRUE means that TwinCAT performed the conversion successfully.
Syntax: __QUERYINTERFACE(<ITF_Source>,<ITF_Dest>);
1. Operand: Interface variable or FB instance
2. Operand: Interface variable with desired target types
A prerequisite for explicit conversion is that both ITF_Source and ITF_Dest are derivatives of __System.IQueryInterface. This interface is implicitly available and requires no library.
Sample:
Interfaces:
INTERFACE I_Base EXTENDS __System.IQueryInterface
METHOD BaseMethod : BOOL
INTERFACE I_Sub1 EXTENDS I_Base
METHOD SubMethod1 : BOOL
INTERFACE I_Sub2 EXTENDS I_Base
METHOD SubMethod2 : BOOL
INTERFACE I_Sample EXTENDS __System.IQueryInterface
METHOD SampleMethod : BOOL
Function blocks:
FUNCTION_BLOCK FB_1 IMPLEMENTS I_Sub1
METHOD BaseMethod : BOOL
BaseMethod := TRUE;
METHOD SubMethod1 : BOOL
SubMethod1 := TRUE;
FUNCTION_BLOCK FB_2 IMPLEMENTS I_Sub2
METHOD BaseMethod : BOOL
BaseMethod := FALSE;
METHOD SubMethod2 : BOOL
SubMethod2 := TRUE;
FUNCTION_BLOCK FB_3 IMPLEMENTS I_Base, I_Sample
METHOD BaseMethod : BOOL
BaseMethod := FALSE;
METHOD SampleMethod : BOOL
SampleMethod := FALSE;
Program:
PROGRAM MAIN
VAR
fb1 : FB_1;
fb2 : FB_2;
fb3 : FB_3;
iBase1 : I_Base := fb1;
iBase2 : I_Base := fb2;
iBase3 : I_Base := fb3;
iSub1 : I_Sub1 := 0;
iSub2 : I_Sub2 := 0;
iSample : I_Sample := 0;
bResult1 : BOOL;
bResult2 : BOOL;
bResult3 : BOOL;
bResult4 : BOOL;
bResult5 : BOOL;
END_VAR
// Result: bResult1 = TRUE as the conversion is successful => iSub1 references fb1
// Explanation: iBase1 references the object fb1 of type FB_1 that implements the interface I_Sub1
bResult1 := __QUERYINTERFACE(iBase1, iSub1);
// Result: bResult2 = FALSE as the conversion is not successful => iSub2 = 0
// Explanation: iBase1 references the object fb1 of type FB_1 that does not implement the interface I_Sub2
bResult2 := __QUERYINTERFACE(iBase1, iSub2);
// Result: bResult3 = FALSE as the conversion is not successful => iSub1 = 0
// Explanation: iBase2 references the object fb2 of type FB_2 that does not implement the interface I_Sub1
bResult3 := __QUERYINTERFACE(iBase2, iSub1);
// Result: bResult4 = TRUE as the conversion is successful => iSub2 references fb2
// Explanation: iBase2 references the object fb2 of type FB_2 that implements the interface I_Sub2
bResult4 := __QUERYINTERFACE(iBase2, iSub2);
// Result: bResult5 = TRUE as the conversion is successful => iSample references fb3
// Explanation: iBase3 references the object fb3 of type FB_3 that implements the interface I_Sample
bResult5 := __QUERYINTERFACE(iBase3, iSample);