__QUERYINTERFACE

Der Operator ist eine Erweiterung der Norm IEC 61131-3.

Der Operator führt zur Laufzeit eine Typkonvertierung einer Schnittstellen-Referenz zu einer anderen aus. Der Operator liefert ein Ergebnis vom Typ BOOL zurück. TRUE bedeutet, dass TwinCAT die Konvertierung erfolgreich durchgeführt hat.

Syntax: __QUERYINTERFACE(<ITF_Source>,<ITF_Dest>);

1. Operand: Schnittstellenvariable oder FB-Instanz

2. Operand: Schnittstellenvariable mit gewünschten Zieltypen

Voraussetzung für die explizite Konvertierung ist, dass sowohl das ITF_Source als auch ITF_Dest eine Ableitung vom Interface __System.IQueryInterface sind. Dieses Interface steht implizit zur Verfügung und benötigt keine Bibliothek.

Beispiel:

Schnittstellen:

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 

Funktionsbausteine:

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;

Programm:

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);