SAP KERNEL METHODS SAP Info












ARTICLE ( Version: 0056)

Kernel Methods
For internal use, kernel methods can be implemented in the kernel
instead of in ABAP.

Introduction
As of Release 6.20, kernel methods allow you to directly call kernel
functions implemented in C or C++. Kernel methods replace the previous concepts of C Calls and System Calls. After Release 6.20, no new C calls or system calls should be introduced.
Kernel methods offer the same checks and securities as normal ABAP
methods. Except for the Constructors and the C Destructor, all ABAP methods can be implemented as kernel methods. An ABAP method can still be redefined as a kernel method and a kernel method can still be redefined as an ABAP method within a path of the inheritance hierarchy.
For the C developer, who wants to implement a kernel method, an API
is available that allows simple, high-performing, and secure access to arguments. Class-based exceptions also continue to be supported.

Defining Kernel Methods

Declaration in ABAP
A kernel method is declared like a normal ABAP method, in the
Class Builder or in the declaration section of a local class. It is completely irrelevant fro the declaration that a method is declared as a kernel method. In ABAP, a kernel method can therefore be used just as a normal ABAP method.

Implementation in ABAP
You define a method as a kernel method by using the optional addition
KERNEL MODULE kmod1 kmod2 ... to the METHOD statement in the implementation section of the class.kmod1, kmod2, ... are the names of the kernel modules that implement the method. The ABAP implementation of a kernel method must be empty, that is there cannot be any ABAP statements between METHOD and ENDMETHOD :
METHOD meth BY Kernel MODULE kmod1 kmod2 ...

ENDMETHOD.
Constructors and the C
Destructor cannot be implemented as kernel methods. There is a separate mechanism for the C destructor.
After ABAP_KEY kernel MODULE, you can specify a list of kernel
kmod1, kmod2, ... You can currently only specify C functions of the kernel for kmod1, kmod2, ... The list after kernel MODULE is evaluated by the compiler from left to right. The first kernel module in the list that is registered in the kernel (see below) is used in the generation,
If no valid kernel module is found in the list, a syntax error occurs.
There are still two standard C functions that can appear at the end of the list: FAIL and IGNORE. If one of these functions is specified at the end of the list, a syntax error occurs if the previous list does not contain a valid module. With IGNORE the call of such a kernel method is ignored (behavior as with empty ABAP implementation) and in the case of FAIL the exception that can be handled of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised.

Examples
METHOD meth BY Kernel MODULE xx_impl_630 xx_impl_620 xx_impl_610.

First the system searches in the kernel for l xx_impl_630.
Lastly, the system searches for xx_impl_610. If none of the functions are found, a syntax error occurs.
METHOD meth BY Kernel MODULE xx_impl_630 xx_impl_620 FAIL.

First the system searches in the kernel for xx_impl_630. Lastly,
the system searches for xx_impl_620. If none of the functions are found, a syntax error occurs and when the method is called, the exception that can be handled of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised.
METHOD meth BY Kernel MODULE xx_impl_620 xx_impl_610 IGNORE.

First the system searches in the kernel for xx_impl_620. Lastly,
the system searches for xx_impl_610. If none of the functions are found, no syntax error occurs and when the method is called, the empty ABAP implementation is called.

Implementation in the kernel
Currently, only C functions can be used as kernel modules of kernel
methods. The C functions can have any position in the kernel. No special includes of the ABAP runtime environment are required for implementing the C function. The C functions must have a specific interface. The interface itself is wrapped by the macro ARGUMENTS All necessary definitions and prototypes are in the include //src/include/abkmeth.h. This is the only include needed for defining C functions for kernel methods.
Since C functions can be defined in C and C++ , you must use externC
in C++:
#include #abkmeth.h#

...
externC void name_of_cmodule( ARGUMENTS )
{
...
}
A C function that implements a kernel method must be registered for the
kernel method. If, after METHOD meth BY Kernel MODULE, you specify the name of a C function that was not registered for the kernel method a syntax error occurs (as mentioned above). You can register several C functions for a kernel method. The sequence of the kernel modules kmod1, kmod2, ... specified in the list after METHOD meth BY Kernel MODULE defines which of the registered C functions is used. This allows downward-compatible further development of kernel methods.
So that changes to the registration become active, you must recompile
the destination lib of the project krn/runt and relink the kernel.

Registration
C functions are registered in the signature file
//src/krn/runt/abkmeth.sig using the following syntax for kernel methods (all ABAP IDs must be specified in capital letters):
Kernel_METHOD('CLASS','METH',cfunc,argcnt)

This definition registers the C function cfunc for the kernel
method meth of a global class class. The C function expects a number of argcnt arguments.
Kernel methods of local classes in class pools or other ABAP programs
are registered using the following macros:
Kernel_METHOD_CLASS_LOCAL('GCLASS','CLASS','METH',cmodule,argcnt)

Kernel_METHOD_PROGRAM_LOCAL('PROG','CLASS','METH',cmodule,argcnt)

The technique is the same as with Kernel_METHOD, except that you
must specify the global class gclass for local classes is class pools and the program prog for program-local classes.

Registering Arguments
All ABAP data objects (such as parameters, attributes, or global data)
that are to be accessed in C functions for kernel methods, are treated as arguments of the C function.
The argument list of a C function for a kernel method is not limited to
the interface parameters of the ABAP method and does not have to contain these completely. Before you access arguments within C functions for kernel methods, these arguments must be registered.
The argcnt arguments must be registered immediately after the C
functions are registered using Kernel_METHOD. A single argument is defined (registered) using one of the following macros:
ARGUMENT_basetype(index,'name',type_kind,'type',read_write)

ARGUMENT_[C|N|X](index,'name',type_kind,'type',read_write,length)

ARGUMENT_P(index,'name',type_kind,'type',read_write,length,decimals)

ARGUMENT_STRUCT(index,'name',type_kind,'type',read_write,ctype)

With these macros an argument is defined with
the name name and an index index.
With basetype you must assign the type of the ABAP data object
according to the following table. If the basetype is C, N, X, P, or STRUCT, you must specify more parameters than for other types. basetypeABAP data typeType in C Cc with length specificationSAP_CHAR (*) [Length] C_GENERICc without length specificationSAP_CHAR* Xx with length specificationSAP_RAW (*) [Length] X_GENERICx without length specificationSAP_RAW* Nn with length specificationSAP_CHAR (*) [Length] N_GENERICn without length specificationSAP_CHAR* Pp with length and decimal specification SAP_BCD (*) [Length] P_GENERICp without length and decimal specification SAP_BCD* DdSAP_DATE* TtSAP_TIME* IiSAP_INT* FfSAP_DOUBLE* STRINGstringStrRef* XSTRINGxstringStrRef* INT2sSAP_SHORT* INT1bSAP_INT1* TABLEAll table typesTABH_REF* OBJ_REFAll object referencesObjRef* DATA_REFAll data referencesFldRef* STRUCTAll structure typesRegistered type ctype* ANYANYVoid* DATADATAVoid* SIMPLESIMPLEVoid* CSEQUENCECSEQUENCEVoid* XSEQUENCEXSEQUENCEVoid* NUMERICNUMERICVoid* CLIKECLIKESAP_CHAR* C_POINTER%_C_POINTERVoid**
The following applies for the macro parameters:

  name is the ID for any ABAP data object in uppercase letters that could also be used in an ABAP implementation of the kernel method. In particular, the ID can contain links with component selectors, for example ME->ATTR or STRUC-COMP.
  index is a continuous number from 1 to argcnt. The arguments are accessed using this index.
  For type_kind you can specify either TYPE or TYPE_REF_TO.
  type is the ID (in upper-case) for any data type in ABAP that could also be used in an ABAP implementation of the kernel method. type_kind and type are used to check the interface of the kernel method in ABAP.
  For read_write you can specify either READ or WRITE . This defines whether you can read or write access the argument and is evaluated in the access macros.
  For ARGUMENT_[C|N|X|P] you use length to specify the length of all ABAP data types with generic length (for C and n in characters, for x and p in bytes).
  For ARGUMENT_P you use decimals to specify the number of decimal places.
  For ARGUMENT_STRUCT you use ctype to specify a suitable C type. This type should be generated from an ABAP type definition using saphfile.

Accessing Arguments
After registering the arguments, you can use the following macros to
access them within the C function. With exception of the direct access to the data control block, the access macros do not require any includes of the ABAP runtime environment.
ARGUMENT_basetype_READ(index,'name');

This macro returns the read address of an
argument with the type const ctype, where ctype is defined by basetype according to the above table. The index and name of the argument must be passed. For the generic types you must specify additional parameters (see below). You only need the index to access the argument. However, to make the C function more legible and ensure that additional consistency checks can be executed, you must also specify the name. If the kernel is compiled in debugging mode, the system executes a consistency check between index and name; the specified C type and ABAP type of the argument are also checked. In the case of an error, a corresponding ABAP runtime error is triggered ( KMETH_INVALID_ARGUMENT_ID, KMETH_INVALID_ARGUMENT_NAME, or KMETH_INVALID_CTYPE_LENG). No checks are executed in the optimized kernel.
ARGUMENT_basetype_WRITE(index,'name');

This macro has the same semantics as ARGUMENT_basetype_READ.
However, the system returns the write address. The system also checks whether the argument was defined as a write argument. If you try to write access a write-protected argument (for example, a constant), this triggers the ABAP runtime error KMETH_ARGUMENT_READ_ONLY.
ARGUMENT_[C|N]_READ(index,'name',lengthU);

ARGUMENT_[C|N]_WRITE(index,'name',lengthU);
ARGUMENT_X_READ(index,'name',lengthR);
ARGUMENT_X_WRITE(index,'name',lengthR);
With these macros you must specify the expected length in bytes
lengthR or in characters lengthU for the generic types C, X, and N.
ARGUMENT_P_READ(index,'name',lengthR,decimals);

ARGUMENT_P_WRITE(index,'name',lengthR,decimals);
With these macros you must specify the expected length in bytes (
lengthR) and the number of decimal places (decimals) for the generic type P.
ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_READ(index,'name',size_tU);

ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_WRITE(index,'name',size_tU);
ARGUMENT_X_GENRIC_READ(index,'name',size_tR);
ARGUMENT_X_GENERIC_WRITE(index,'name',size_tR);
With these macros you must specify a variable of the type size_tU
or size_tR, containing the length in bytes or characters, for the types C_GENERIC, X_GENERIC, N_GENERIC, and CLIKE.
ARGUMENT_P_GENERIC_READ(index,'name',size_tR,decimals);

ARGUMENT_P_GENERIC_WRITE(index,'name',size_tR,decimals);
With these macros you must specify a variable decimals (for the
decimal places) as well as the length size_tR for the type P_GENERIC.
ARGUMENT_STRUCT_READ(index,'name',ctype);

ARGUMENT_STRUCT_WRITE(index,'name',ctype);
With these macros you must specify a suitable C type ctype for
all structured types STRUCT.
ARGUMENT_C_POINTER(index,'name');

This macro is available specifically for the
type %_C_POINTER. This type is a special internal ABAP type that has exactly the byte length of a C pointer (4, 8, or 16 bytes, depending on platform). The type is always mapped to the predefined ABAP type X. The macros for the type X or X_GENERIC are not used due to the variable length and platform-dependency.
ARGUMENT_IS_SUPPLIED(index,'name');

This macro has the same semantics as the logical expression IS
SUPPLIED in ABAP. The same consistency checks are executed as for ARGUMENT_READ.
ARGUMENT_DATA(index,'name',ctype);

This macro returns the data control block with the C type const DATA
*. The same consistency checks are executed as for ARGUMENT_READ . The macro is only active if the include //src/include/abdata.h of the ABAP runtime environment was included.

Raising Exceptions
C functions that implement kernel method can raise
class-based exceptions.

Registering Exceptions
The relevant global exception classes must be registered with an
extension of //src/include/abexcpc.h. Local exception classes cannot be registered.
In //src/include/abexcpc.h, the exception class is declared and
any text IDs are defined:
//src/include/abexcpc.h

...
CX_ABSTR (CX_..., 'CX_...')
CX_TXTID (CX_..._bar, CX_..., 'BAR') /* special text for class */
...
Classes can only be declared with their standard text:

//src/include/abexcpc.h

...
CX_CLASS (CX_..., 'CX_...') /* class with standard text */
...
The exact documentation is in the file //src/include/abexcpc.h.

You must extend the file //src/include/abexcpa.h so that any
attributes of an exception class in a C function can be populated; you must specify the name, internal type (according to //src/include/abtypes.h), and the byte length:
//src/include/abexcpa.h

...
CX_ATTR (CX_..._attr1, CX_..., 'ATTR1', TYPCSTRING, sizeofR(StrRef))
CX_ATTR (CX_..._attr2, CX_..., 'ATTR2', TYPC, LEN_UC2RAW(30))
...
Finally, you must register exceptions as well as arguments in the file
//src/krn/runt/abkmeth.sig. This is not forced but, during the syntax check, only registered exceptions are checked for their existence:
//src/krn/runt/abkmeth.sig

...
EXCEPTION(CX_...)
...

Raising Exceptions
A C function can raise an exception by calling the following macros
consecutively:
EXCEPTION_CREATE(CX_..._bar);

EXCEPTION_SET_CSTRING(CX_..._attr1, value, valueLength);
EXCEPTION_SET_C (CX_..._attr2, value, valueLength);
EXCEPTION_RAISE();
Within the macros EXCEPTION_CREATE or EXCEPTION_RAISE, a
long jump to Extri always takes place, that is the C function that implements the kernel method is exited in a long jump and the ABAP runtime environment takes control. Therefore, the C function should release its temporary memory before raising an exception. If the exception is caught in ABAP using CATCH without the INTO addition, the long jump takes place in EXCEPTION_CREATE. If the exception is caught with the INTO addition (the exception object is used) or not at all, the long jump takes place in EXCEPTION_RAISE.
The exceptions are processed in the runtime environment, as if they were
raised in ABAP and the same dynamic checks are executed.
Currently, the following macros, which can be
extended if necessary, are available for setting exception attributes. Strings, integer and C fields are supported. See the above sequence for use.
EXCEPTION_SET_CSTRING_UC

EXCEPTION_SET_C
Value with length specification

EXCEPTION_SET_C_UC

EXCEPTION_SET_INT
Value with null termination


Auxiliary Program for Kernel Methods
The executable ABAP program
RSKMETH serves as a browser for the registration of kernel modules. You can use it to ascertain which C functions are registered for which kernel methods and which arguments/exceptions are registered for these functions. This is helpful when analyzing syntax errors, because with kernel methods information is processed that only exists in the kernel modules.

Example
The following example is a simplified calculation class for floating
point numbers. The class has an instance attribute in which the last result of each calculation is stored. A method executes a division and is implemented as a kernel method. If the divisor equals zero, the method triggers a class-based exception.

Declaration Section of the Class in ABAP
CLASS cl_my_calculation DEFINITION ...

...
DATA last_result TYPE F.
...
METHODS div
IMPORTING p_dividend TYPE F p_divisor TYPE F
RETURNING VALUE(p_result) TYPE F.
...
ENDCLASS.

Signature File //src/krn/runt/abkmeth.sig in the Kernel
...

Kernel_METHOD(CL_MY_CALCULATION, DIV, xx_myDiv,4)
ARGUMENT_F(1, #P_DIVIDEND#, TYPE, #F#, READ)
ARGUMENT_F(2, #P_DIVISOR#, TYPE, #F#, READ)
ARGUMENT_F(3, #P_RESULT#, TYPE, #F#, WRITE)
ARGUMENT_F(4, #ME->LAST_RESULT#,TYPE, #F#, WRITE)
EXCEPTION('CX_MY_DIV_BY_ZERO')
...

C++ Source Code //src/krn/.../mycalc.cpp in the Kernel
#include 'abkmeth.h#

...
externC void xx_myDiv( ARGUMENTS ){

const SAP_DOUBLE *const dividend = ARGUMENT_F_READ(1 ,#P_DIVIDEND#);
const SAP_DOUBLE *const divisor = ARGUMENT_F_READ(2,#P_DIVISOR#);
SAP_DOUBLE *result = ARGUMENT_F_WRITE(3,#P_RESULT#);
SAP_DOUBLE *last_result = ARGUMENT_F_WRITE(4 ,#ME->LAST_RESULT#);

if( 0 == *divisor )
{
EXCEPTION_CREATE(CX_MY_DIV_BY_ZERO);
EXCEPTION_RAISE();
}

*result = *dividend / *divisor;
*last_result = *result;

}

Implementation Section of the Class in ABAP
CLASS cl_my_calculation IMPLEMENTATION.

...
METHOD div BY Kernel MODULE xx_myDiv.
ENDMETHOD.
...
ENDCLASS.

ARTICLE ( Version: 0109)

Kernel Methods
For internal use, kernel methods can be implemented in the
kernel instead of in ABAP.

Introduction
Kernel methods allow you to directly call kernel functions implemented
in C or C++. Kernel methods replace the previous concepts of C-Calls and System-Calls. After Release 6.20, no new C-Calls or System-Calls should be introduced.
Kernel methods offer the same checks and securities as normal ABAP
methods. Except for the Constructors and the C Destructor, all ABAP methods can be implemented as kernel methods. An ABAP method can still be redefined as a kernel method and a kernel method can still be redefined as an ABAP method within a path of the inheritance hierarchy.
For the C developer, who wants to implement a kernel method, an API
is available that allows simple, high-performing, and secure access to arguments. Class-based exceptions also continue to be supported.

Defining Kernel Methods

Declaration in ABAP
A kernel method is declared like a normal ABAP method, in the
Class Builder or in the declaration section of a local class. It is completely irrelevant for the declaration that a method is declared as a kernel method. In ABAP, a kernel method can therefore be used just as a normal ABAP method.

Implementation in ABAP
You define a method as a kernel method by using the optional addition
KERNEL MODULE kmod1 kmod2 ... to the METHOD statement in the implementation section of the class.kmod1, kmod2, ... are the names of the kernel modules that implement the method. The ABAP implementation of a kernel method must be empty, that is there cannot be any ABAP statements between METHOD and ENDMETHOD :
METHOD meth BY Kernel MODULE kmod1 kmod2 ...

ENDMETHOD.
Constructors and the C
Destructor cannot be implemented as kernel methods. There is a separate mechanism for the C destructor.
After ABAP_KEY kernel MODULE, you can specify a list of kernel
kmod1, kmod2, ... You can currently only specify C functions of the kernel for kmod1, kmod2, ... The list after kernel MODULE is evaluated by the compiler from left to right. The first kernel module in the list that is registered in the kernel (see below) is used in the generation,
If no valid kernel module is found in the list, a syntax error occurs.
There are still two standard C functions that can appear at the end of the list: FAIL and IGNORE. If one of these functions is specified at the end of the list, a syntax error does not occur if the previous list does not contain a valid module. With IGNORE the call of such a kernel method is ignored (behavior as with empty ABAP implementation) and in the case of FAIL the exception that can be handled of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised.

Examples METHOD meth BY Kernel MODULE xx_impl_630 xx_impl_620 xx_impl_610.
First the system searches in the kernel for <(>xx_impl_630<)>, then for
<(>xx_impl_620<)>, and then for <(>xx_impl_610<)>. If none of the functions are found, a syntax error occurs.
METHOD meth BY Kernel MODULE xx_impl_630 xx_impl_620 FAIL.

First the system searches in the kernel for xx_impl_630. Lastly, the
system searches for xx_impl_620. If none of the functions are found, no syntax error occurs and when the method is called, the exception that can be handled of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised. METHOD meth BY Kernel MODULE xx_impl_620 xx_impl_610 IGNORE.
First the system searches in the kernel for xx_impl_620. Lastly, the
system searches for xx_impl_610. If none of the functions are found, no syntax error occurs and when the method is called, the empty ABAP implementation is called.

Implementation in the kernel
Currently, only C functions can be used as kernel modules of kernel
methods. The C functions can have any position in the kernel. No special includes of the ABAP runtime environment are required for implementing the C function. The C functions must have a specific interface. The interface itself is wrapped by the macro ARGUMENTS All necessary definitions and prototypes are in the include //src/include/abkmeth.h. This is the only include needed for defining C functions for kernel methods.
Since C functions can be defined in C and C++ , you must use externC
in C++: #include 'abkmeth.h'
...
externC void name_of_cmodule( ARGUMENTS )
{
...
}
A C function that implements a kernel method must be registered for the
kernel method. If, after METHOD meth BY Kernel MODULE, you specify the name of a C function that was not registered for the kernel method a syntax error occurs (as mentioned above). You can register several C functions for a kernel method. The sequence of the kernel modules kmod1, kmod2, ... specified in the list after METHOD meth BY Kernel MODULE defines which of the registered C functions is used. This allows downward-compatible further development of kernel methods.
So that changes to the registration become active, you must recompile
the destination lib of the project krn/runt and relink the kernel.

Registration
C functions are registered in the signature file
//src/krn/runt/abkmeth.sig using the following syntax for kernel methods (all ABAP IDs must be specified in capital letters): KERNEL_METHOD('CLASS','METH', cfunc,argcnt)
This definition registers the C function cfunc for the kernel
method meth of a global class class. The C function expects a number of argcnt arguments.
Kernel methods of local classes in class pools or other ABAP programs
are registered using the following macros: KERNEL_METHOD_CLASS_LOCAL('GCLASS','CLASS','METH',cmodule,argcnt) KERNEL_METHOD_PROGRAM_LOCAL('PROG','CLASS','METH',cmodule,argcnt)
The technique is the same as with Kernel_METHOD, except that you
must specify the global class gclass for local classes is class pools and the program prog for program-local classes.

Registering Arguments
All ABAP data objects (such as parameters, attributes, or global data)
that are to be accessed in C functions for kernel methods, are treated as arguments of the C function.
The argument list of a C function for a kernel method is not limited to
the interface parameters of the ABAP method and does not have to contain these completely. Before you access arguments within C functions for kernel methods, these arguments must be registered.
The argcnt arguments must be registered immediately after the C
functions are registered using KERNEL_METHOD. A single argument is defined (registered) using one of the following macros: ARGUMENT_basetype(index,'name',type_kind,'type',read_write) ARGUMENT_[C|N|X](index,'name',type_kind,'type',read_write,length) ARGUMENT_P(index,'name',type_kind,'type',read_write,length,decimals) ARGUMENT_STRUCT(index,'name',type_kind,'type',read_write,ctype)
With these macros an argument is defined with
the name name and an index index.
With basetype you must assign the type of the ABAP data object
according to the following table. If the basetype is C, N, X, P, or STRUCT, you must specify more parameters than for other types. basetypeABAP data typeType in C Cc with length specificationSAP_CHAR (*) [Length] C_GENERICc without length specificationSAP_CHAR* Xx with length specificationSAP_RAW (*) [Length] X_GENERICx without length specificationSAP_RAW* Nn with length specificationSAP_CHAR (*) [Length] N_GENERICn without length specificationSAP_CHAR* Pp with length and decimal specification SAP_BCD (*) [Length] P_GENERICp without length and decimal specification SAP_BCD* DdSAP_DATE* TtSAP_TIME* IiSAP_INT* FfSAP_DOUBLE* STRINGstringStrRef* XSTRINGxstringStrRef* INT2sSAP_SHORT* INT1bSAP_INT1* TABLEAll table typesTABH_REF* OBJ_REFAll object referencesObjRef* DATA_REFAll data referencesFldRef* STRUCTAll structure typesRegistered type ctype* ANYanyvoid* DATAdatavoid* SIMPLEsimplevoid* CSEQUENCEcsequencevoid* XSEQUENCExsequencevoid* NUMERICnumericvoid* CLIKEclikeSAP_CHAR* C_POINTER%_c_pointervoid**
The following applies for the macro parameters:

  name is the ID for any ABAP data object in uppercase letters that could also be used in an ABAP implementation of the kernel method. In particular, the ID can contain links with component selectors, for example me->ATTR or struc-comp.
  index is a continuous number from 1 to argcnt. The arguments are accessed using this index.
  For type_kind you can specify either TYPE or TYPE_REF_TO.
  type is the ID (in upper-case) for any data type in ABAP that could also be used in an ABAP implementation of the kernel method. type_kind and type are used to check the interface of the kernel method in ABAP.
  For read_write you can specify either READ or WRITE . This defines whether you can read or write access the argument and is evaluated in the access macros.
  For ARGUMENT_[C|N|X|P] you use length to specify the length of all ABAP data types with generic length (for c and n in characters, for x and p in bytes).
  For ARGUMENT_P you use decimals to specify the number of decimal places.
  For ARGUMENT_STRUCT you use ctype to specify a suitable C type. This type should be generated from an ABAP type definition using saphfile.

Accessing Arguments
After registering the arguments, you can use the following macros to
access them within the C function. With exception of the direct access to the data control block, the access macros do not require any includes of the ABAP runtime environment. ARGUMENT_basetype_READ(index,'name');
This macro returns the read address of an
argument with the type const ctype, where ctype is defined by basetype according to the above table. The index and name of the argument must be passed. For the generic types you must specify additional parameters (see below). You only need the index to access the argument. However, to make the C function more legible and ensure that additional consistency checks can be executed, you must also specify the name. If the kernel is compiled in debugging mode, the system executes a consistency check between index and name; the specified C type and ABAP type of the argument are also checked. In the case of an error, a corresponding ABAP runtime error is triggered ( KMETH_INVALID_ARGUMENT_ID, KMETH_INVALID_ARGUMENT_NAME, or KMETH_INVALID_CTYPE_LENG). No checks are executed in the optimized kernel. ARGUMENT_basetype_WRITE(index,'name');
This macro has the same semantics as ARGUMENT_basetype_READ.
However, the system returns the write address. The system also checks whether the argument was defined as a write argument. If you try to write access a write-protected argument (for example, a constant), this triggers the ABAP runtime error KMETH_ARGUMENT_READ_ONLY. ARGUMENT_[C|N]_READ(index,'name',lengthU);
ARGUMENT_[C|N]_WRITE(index,'name',lengthU);
ARGUMENT_X_READ(index,'name',lengthR);
ARGUMENT_X_WRITE(index,'name',lengthR);
With these macros you must specify the expected length in bytes
lengthR or in characters lengthU for the generic types c , x, and n ARGUMENT_P_READ(index,'name',lengthR,decimals);
ARGUMENT_P_WRITE(index,'name',lengthR,decimals);
With these macros you must specify the expected length in bytes (
lengthR) and the number of decimal places (decimals) for the generic type p. ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_READ(index,'name',size_tU);
ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_WRITE(index,'name',size_tU);
ARGUMENT_X_GENRIC_READ(index,'name',size_tR);
ARGUMENT_X_GENERIC_WRITE(index,'name',size_tR);
With these macros you must specify a variable of the type size_tU
or size_tR, containing the length in bytes or characters, for the types C_GENERIC, X_GENERIC, N_GENERIC, and CLIKE. ARGUMENT_P_GENERIC_READ(index,'name',size_tR,decimals);
ARGUMENT_P_GENERIC_WRITE(index,'name',size_tR,decimals);
With these macros you must specify a variable decimals (for the
decimal places) as well as the length size_tR for the type P_GENERIC. ARGUMENT_STRUCT_READ(index,'name',ctype);
ARGUMENT_STRUCT_WRITE(index,'name',ctype);
With these macros you must specify a suitable C typectype
for all structured types STRUCT. ARGUMENT_C_POINTER(index,'name');
This macro is available specifically for the
type %_c_pointer. This type is a special internal ABAP type that has exactly the byte length of a C pointer (4, 8, or 16 bytes, depending on platform). The type is always mapped to the predefined ABAP type x. The macros for the type X or X_GENERIC are not used due to the variable length and platform-dependency. ARGUMENT_IS_SUPPLIED(index,'name');
This macro has the same semantics as the logical expression IS
SUPPLIED in ABAP. The same consistency checks are executed as for ARGUMENT_READ. ARGUMENT_DATA(index,'name',ctype);
This macro returns the data control block with the C type
const DATA *. The same consistency checks are executed as for ARGUMENT_READ. The macro is only active if the include //src/include/abdata.h of the ABAP runtime environment was included.

Raising Exceptions
C functions that implement kernel method can raise
class-based exceptions.

Registering Exceptions
The relevant global exception classes must be registered with an
extension of //src/include/abexcpc.h. Local exception classes cannot be registered.
In //src/include/abexcpc.h, the exception class is declared and
any text IDs are defined: //src/include/abexcpc.h
...
CX_ABSTR (CX_..., 'CX_...')
CX_TXTID (CX_..._bar, CX_..., 'BAR') /* special text for class */
...
Classes can also be declared with their standard text only:
//src/include/abexcpc.h
...
CX_CLASS (CX_..., 'CX_...') /* class with standard text */
...
The exact documentation is in the file //src/include/abexcpc.h.

You must extend the file //src/include/abexcpa.hso that any
attributes of an exception class in a C function can be populated; you must specify the name, internal type (according to //src/include/abtypes.h), and the byte length: //src/include/abexcpa.h
...
CX_ATTR (CX_..._attr1, CX_..., 'ATTR1', TYPCSTRING, sizeofR(StrRef))
CX_ATTR (CX_..._attr2, CX_..., 'ATTR2', TYPC, LEN_UC2RAW(30))
...
Finally, you must register exceptions as well as arguments in the file
//src/krn/runt/abkmeth.sig. This is not forced but, during the syntax check, only registered exceptions are checked for their existence: //src/krn/runt/abkmeth.sig
...
EXCEPTION(CX_...)
...

Raising Exceptions
A C function can raise an exception by calling the following macros
consecutively: EXCEPTION_CREATE(CX_..._bar);
EXCEPTION_SET_CSTRING(CX_..._attr1, value, valueLength);
EXCEPTION_SET_C (CX_..._attr2, value, valueLength);
EXCEPTION_RAISE();
Within the macros EXCEPTION_CREATE or EXCEPTION_RAISE, a
long jump to Extri always takes place, that is the C function that implements the kernel method is exited in a long jump and the ABAP runtime environment takes control. Therefore, the C function should release its temporary memory before raising an exception. If the exception is caught in ABAP using CATCH without the INTO addition, the long jump takes place in EXCEPTION_CREATE. If the exception is caught with the INTO addition (the exception object is used) or not at all, the long jump takes place in EXCEPTION_RAISE.
The exceptions are processed in the runtime environment, as if they were
raised in ABAP and the same dynamic checks are executed.
Currently, the following macros, which can be
extended if necessary, are available for setting exception attributes. Strings, integer and C fields are supported. See the above sequence for use. EXCEPTION_SET_CSTRING_UC
EXCEPTION_SET_C
Value with length specification
EXCEPTION_SET_C_UC
EXCEPTION_SET_INT
Value with null termination


Auxiliary Program for Kernel Methods
The ABAP program RSKMETH serves as a browser for the registration
of kernel modules. You can use it to ascertain which C functions are registered for which kernel methods and which arguments/exceptions are registered for these functions. This is helpful when analyzing syntax errors, because with kernel methods information is processed that only exists in the kernel modules.

Example
The following example is a simplified calculation class for floating
point numbers. The class has an instance attribute in which the last result of each calculation is stored. A method executes a division and is implemented as a kernel method. If the divisor equals zero, the method triggers a class-based exception.

Declaration Section of the Class in ABAP CLASS cl_my_calculation DEFINITION ...
...
DATA last_result TYPE decfloat16.
...
METHODS div
IMPORTING p_dividend TYPE decfloat16 p_divisor TYPE decfloat16
RETURNING VALUE(p_result) TYPE decfloat16.
...
ENDCLASS.

Signature file //src/krn/runt/abkmeth.sig in the kernel ...
KERNEL_METHOD(CL_MY_CALCULATION, DIV, xx_myDiv,4)
ARGUMENT_F(1, 'P_DIVIDEND', TYPE, 'F', READ)
ARGUMENT_F(2, 'P_DIVISOR', TYPE, 'F', READ)
ARGUMENT_F(3, 'P_RESULT', TYPE, 'F', WRITE)
ARGUMENT_F(4, 'ME->LAST_RESULT',TYPE, 'F', WRITE)
EXCEPTION('CX_MY_DIV_BY_ZERO')
...

C++ source code //src/krn/.../mycalc.cpp in the kernel #include 'abkmeth.h'
...
externC void xx_myDiv( ARGUMENTS ){

const SAP_DOUBLE *const dividend = ARGUMENT_F_READ(1,'P_DIVIDEND');
const SAP_DOUBLE *const divisor = ARGUMENT_F_READ(2,'P_DIVISOR');
SAP_DOUBLE *result = ARGUMENT_F_WRITE(3,'P_RESULT');
SAP_DOUBLE *last_result = ARGUMENT_F_WRITE(4 ,'ME->LAST_RESULT');

if( 0 == *divisor )
{
EXCEPTION_CREATE(CX_MY_DIV_BY_ZERO);
EXCEPTION_RAISE();
}

*result = *dividend / *divisor;
*last_result = *result;

}

Implementation section of the class in ABAP CLASS cl_my_calculation IMPLEMENTATION.
...
METHOD div BY KERNEL MODULE xx_myDiv.
ENDMETHOD.
...
ENDCLASS.

ARTICLE ( Version: 0111)

Kernel Methods
For internal use, kernel methods can be implemented in the
ABAP kernel instead of in the ABAP language.

Introduction
Kernel methods allow you to directly call ABAP kernel functions
implemented in C or C++. Kernel methods replace the previous concepts of C calls and system calls. No new C calls or system calls need to be introduced.
Kernel methods offer the same checks and security features as normal
ABAP methods. Except for the Constructors and the C Destructor, all ABAP methods can be implemented as kernel methods. An ABAP method can still be redefined as a kernel method and a kernel method can still be redefined as an ABAP method within a path of the inheritance hierarchy.
For C developers who want to implement a kernel method, an API is
available that allows simple, high-performance, and secure access to arguments. Class-based exceptions also continue to be supported.

Defining Kernel Methods

Declaration in ABAP
A kernel method is declared in the same way as a normal ABAP method, in
Class Builder or in the declaration section of a local class. Whether a method is implemented as a kernel method is not important for the declaration. In ABAP, this means that a kernel method can be used just like a normal ABAP method.

Implementation in ABAP
A method is specified as a kernel method in the implementation part of
the class using the optional addition BY KERNEL MODULE kmod1 kmod2 ... of the statement METHOD. kmod1, kmod2, ... are the names of kernel modules that implement the method. The ABAP implementation of a kernel method must be empty, which means that there cannot be any ABAP statements between METHOD and ENDMETHOD: METHOD meth BY KERNEL MODULE kmod1 kmod2 ...
ENDMETHOD.
Constructors and the C
Destructor cannot be implemented as kernel methods. There is a separate mechanism for the C Destructor.
After ABAP_KEY KERNEL MODULE, you can specify a list of kernel
modules kmod1, kmod2, ... Currently, you can only specify C functions of the kernel for kmod1, kmod2, ... The list after KERNEL MODULE is evaluated by the compiler from left to right. The first kernel module in the list that is registered in the kernel (see below) is used in the generation.
If no valid kernel module is found in the list, a syntax error occurs.
There are still two standard C functions that can appear at the end of the list: FAIL and IGNORE. If one of these functions is specified at the end of the list, then a syntax error is avoided if the previous list does not contain a valid module. IGNORE is used to ignore the call of a kernel method of this type (behavior as in an empty ABAP implementation) and, in the case of FAIL, a handleable exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised.

Examples METHOD meth BY KERNEL MODULE xx_impl_630 xx_impl_620 xx_impl_610.
First, the kernel is searched for xx_impl_630. The kernel is then
searched for nach xx_impl_620, and finally for xx_impl_610. If none of these functions are found, a syntax error is raised. METHOD meth BY KERNEL MODULE xx_impl_630 xx_impl_620 FAIL.
First, the kernel is searched for xx_impl_630. Then the kernel is
searched for xx_impl_620. If neither of these functions are found, a syntax error is not raised; a handleable exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised instead, when the method is called. METHOD meth BY KERNEL MODULE xx_impl_620 xx_impl_610 IGNORE.
First, the kernel is searched for xx_impl_620. Then the kernel is
searched for xx_impl_620. If none of the functions are found, a syntax error is not raised; the empty ABAP implementation is called instead, when the method is called.

Implementation in the Kernel
Currently, only C functions can be used as kernel modules of kernel
methods. The C functions can have any position in the kernel. No special includes from the ABAP runtime environment are required for implementing the C function. The C functions must have a specific interface. The interface itself is wrapped by a macro called ARGUMENTS. All required definitions and prototypes are in the include //src/include/abkmeth.h. This is the only include needed for defining C functions for kernel methods.
Since C functions can be defined in C and C++ , you must use externC
in C++: #include 'abkmeth.h'
...
externC void name_of_cmodule( ARGUMENTS )
{
...
}
A C function that implements a kernel method must be registered for the
kernel method. If, after METHOD meth BY KERNEL MODULE, you specify the name of a C function that was not registered for the kernel method, a syntax error occurs (as mentioned above). You can register several C functions for a kernel method. The sequence of the kernel modules kmod1, kmod2, ... specified in the list after METHOD meth BY KERNEL MODULE defines which of the registered C functions is used. This allows downward-compatible further development of kernel methods.
To make changes to the registration active, you must recompile the
destination lib of the project krn/runt and relink the kernel.

Registration
C functions are registered in the signature file
//src/krn/runt/abkmeth.sig using the following syntax for kernel methods (all ABAP IDs must be specified in uppercase letters): KERNEL_METHOD('CLASS','METH', cfunc,argcnt)
This definition registers the C function cfunc for the kernel
method meth of a global class class. The C function expects a number of argcnt arguments.
Kernel methods of local classes in class pools or other ABAP programs
are registered using the following macros: KERNEL_METHOD_CLASS_LOCAL('GCLASS','CLASS','METH',cmodule,argcnt) KERNEL_METHOD_PROGRAM_LOCAL('PROG','CLASS','METH',cmodule,argcnt)
The technique is the same as with KERNEL_METHOD, except that you
must specify the global class gclass for local classes in class pools and the program prog for program-local classes.

Registering Arguments
All ABAP data objects (such as parameters, attributes, or global data)
that are to be accessed in C functions for kernel methods, are treated as arguments of the C function.
The argument list of a C function for a kernel method is not limited to
the interface parameters of the ABAP method and does not have to contain these completely. Before you access arguments within C functions for kernel methods, these arguments must be registered.
The argcnt arguments must be registered immediately after the C
function is registered using KERNEL_METHOD. A single argument is defined (registered) using one of the following macros: ARGUMENT_basetype(index,'name',type_kind,'type',read_write) ARGUMENT_[C|N|X](index,'name',type_kind,'type',read_write,length) ARGUMENT_P(index,'name',type_kind,'type',read_write,length,decimals) ARGUMENT_STRUCT(index,'name',type_kind,'type',read_write,ctype)
These macros define an argument with the name
name and an index index.
You must use basetype to assign the type of the ABAP data object
according to the following table. If the basetype is C, N, X, P, or STRUCT, you must specify more parameters than for other types. basetypeABAP Data TypeType in C Cc with specified lengthSAP_CHAR (*) [Length] C_GENERICc without specified lengthSAP_CHAR* Xx with specified lengthSAP_RAW (*) [Length] X_GENERICx without specified lengthSAP_RAW* Nn with specified lengthSAP_CHAR (*) [Length] N_GENERICn without specified lengthSAP_CHAR* Pp with specified length and decimalsSAP_BCD (*) [Length] P_GENERICp without specified length and decimals SAP_BCD* DdSAP_DATE* TtSAP_TIME* INT1bSAP_INT1* INT2sSAP_SHORT* IiSAP_INT* FfSAP_DOUBLE* DECFLOAT16decfloat16DecFloat16 DECFLOAT34decfloat34DecFloat34 STRINGstringStrRef* XSTRINGxstringStrRef* TABLEAll table typesTABH_REF* OBJ_REFAll object referencesObjRef* DATA_REFAll data referencesFldRef* STRUCTAll structure typesRegistered type ctype* ANYanyvoid* DATAdatavoid* SIMPLEsimplevoid* CSEQUENCEcsequencevoid* XSEQUENCExsequencevoid* NUMERICnumericvoid* CLIKEclikeSAP_CHAR* C_POINTER%_c_pointervoid**
The macro parameters have the following meanings:

  name is the ID for any ABAP data object in uppercase letters that could also be used in an ABAP implementation of the kernel method. In particular, the ID can contain links with component selectors, for example me->attr or struc-comp.
  index is a sequential number from 1 to argcnt. The arguments are accessed using this index.
  For type_kind you can specify either TYPE or TYPE_REF_TO.
  type is the ID for any ABAP data type in uppercase letters that could also be used in an ABAP implementation of the kernel method. type_kind and type are used to check the interface of the kernel method in ABAP.
  For read_write you can specify either READ or WRITE . This defines whether you have read or write access to the argument and is evaluated in the access macros.
  length is used to specify the length of all ABAP data types with a generic length for ARGUMENT_[C|N|X|P]. In characters for c and n and in bytes for x and p.
  For ARGUMENT_P you use decimals to specify the number of decimal places.
  For ARGUMENT_STRUCT you use ctype to specify a suitable C type. This type should be generated from an ABAP type definition using saphfile.

Accessing Arguments
After registering the arguments, you can use the following macros to
access them within the C function. With the exception of the direct access to the data control block, the access macros do not require any includes from the ABAP runtime environment. ARGUMENT_basetype_READ(index,'name');
This macro returns the read address of an
argument with the type const ctype, where ctype is defined by basetype according to the above table. The index and name of the argument must be passed. You must specify additional parameters for the generic types (see below). You only need the index to access the argument. However, to make the C function more legible and ensure that additional consistency checks can be executed, you must also specify the name. If the kernel is compiled in debugging mode, the system executes a consistency check between index and name; the specified C type and ABAP type of the argument are also checked. In the case of an error, an appropriate ABAP runtime error is triggered ( KMETH_INVALID_ARGUMENT_ID, KMETH_INVALID_ARGUMENT_NAME, or KMETH_INVALID_CTYPE_LENG). No checks are made in the optimized kernel. ARGUMENT_basetype_WRITE(index,'name');
This macro has the same semantics as ARGUMENT_basetype_READ.
However, the system returns the write address. The system also checks whether the argument was defined as a write argument. If you try to write access a read-only argument (for example, a constant), this triggers the ABAP runtime error KMETH_ARGUMENT_READ_ONLY. ARGUMENT_[C|N]_READ(index,'name',lengthU);
ARGUMENT_[C|N]_WRITE(index,'name',lengthU);
ARGUMENT_X_READ(index,'name',lengthR);
ARGUMENT_X_WRITE(index,'name',lengthR);
With these macros you must specify the expected length in bytes
lengthR or in characters lengthU for the generic types c , x, and n ARGUMENT_P_READ(index,'name',lengthR,decimals);
ARGUMENT_P_WRITE(index,'name',lengthR,decimals);
With these macros you must specify the expected length in bytes (
lengthR) and the number of decimal places (decimals) for the generic type p. ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_READ(index,'name',size_tU);
ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_WRITE(index,'name',size_tU);
ARGUMENT_X_GENRIC_READ(index,'name',size_tR);
ARGUMENT_X_GENERIC_WRITE(index,'name',size_tR);
With these macros you must specify a variable of the type size_tU
or size_tR, containing the length in bytes or characters, for the types C_GENERIC, X_GENERIC, N_GENERIC, and CLIKE. ARGUMENT_P_GENERIC_READ(index,'name',size_tR,decimals);
ARGUMENT_P_GENERIC_WRITE(index,'name',size_tR,decimals);
With these macros you must specify a variable decimals (for the
decimal places) as well as the length size_tR for the type P_GENERIC. ARGUMENT_STRUCT_READ(index,'name',ctype);
ARGUMENT_STRUCT_READ(index,'name',ctype);
With these macros you must specify a suitable C typectype
for all structured types STRUCT. ARGUMENT_C_POINTER(index,'name');
This macro is available specifically for the
type %_c_pointer. This type is a special internal ABAP type that has exactly the byte length of a C pointer (4, 8, or 16 bytes, depending on platform). The type is always mapped to the predefined ABAP type x. The macros for the type X or X_GENERIC are not used due to the variable length and platform-dependency. ARGUMENT_IS_SUPPLIED(index,'name');
This macro has the same semantics as the logical expression IS
SUPPLIED in ABAP. The same consistency checks are executed as for ARGUMENT_READ. ARGUMENT_DATA(index,'name',ctype);
This macro returns the data control block with the C type
const DATA *. The same consistency checks are executed as for ARGUMENT_READ. The macro is only active if the include //src/include/abdata.h of the ABAP runtime environment was included.

Raising Exceptions
C functions that implement kernel method can raise
class-based exceptions.

Registering Exceptions
The relevant global exception classes must be registered with an
extension of //src/include/abexcpc.h. Local exception classes cannot be registered.
The exception class is declared in //src/include/abexcpc.h and
any text IDs are defined: //src/include/abexcpc.h
...
CX_ABSTR (CX_..., 'CX_...')
CX_TXTID (CX_..._bar, CX_..., 'BAR') /* special text for class */
...
Classes can also be declared with their standard text only:
//src/include/abexcpc.h
...
CX_CLASS (CX_..., 'CX_...') /* class with standard text */
...
The exact documentation is in the file //src/include/abexcpc.h.

You must extend the file //src/include/abexcpa.h so that any
attributes of an exception class in a C function can be populated; you must specify the name, internal type (according to //src/include/abtypes.h) and the byte length: //src/include/abexcpa.h
...
CX_ATTR (CX_..._attr1, CX_..., 'ATTR1', TYPCSTRING, sizeofR(StrRef))
CX_ATTR (CX_..._attr2, CX_..., 'ATTR2', TYPC, LEN_UC2RAW(30))
...
Finally, you must register exceptions as well as arguments in the file
//src/krn/runt/abkmeth.sig. This is not forced but, during the syntax check, only registered exceptions are checked for their existence: //src/krn/runt/abkmeth.sig
...
EXCEPTION(CX_...)
...

Raising Exceptions
A C function can raise an exception by calling the following macros
consecutively: EXCEPTION_CREATE(CX_..._bar);
EXCEPTION_SET_CSTRING(CX_..._attr1, value, valueLength);
EXCEPTION_SET_C (CX_..._attr2, value, valueLength);
EXCEPTION_RAISE();
Within the macros EXCEPTION_CREATE or EXCEPTION_RAISE, a
long jump to Extri always takes place, which means that the C function that implements the kernel method is exited in a long jump and the ABAP runtime environment takes control. Therefore, the C function should release its temporary memory before raising an exception. If the exception is caught in ABAP using CATCH without the INTO addition, the long jump takes place in EXCEPTION_CREATE. If the exception is caught with the INTO addition (the exception object is used) or not at all, the long jump takes place in EXCEPTION_RAISE.
The exceptions are processed in the runtime environment, as if they were
raised in ABAP and the same dynamic checks are executed.
Currently, the following macros, which can be
extended if necessary, are available for setting exception attributes. Strings, integer and C fields are supported. See the above sequence for use. EXCEPTION_SET_CSTRING_UC
EXCEPTION_SET_C
Value with length specified
EXCEPTION_SET_C_UC
EXCEPTION_SET_INT
Value with null termination


Auxiliary Program for Kernel Methods
The ABAP program RSKMETH serves as a browser
for the registration of kernel modules. You can use it to ascertain which C functions are registered for which kernel methods and which arguments/exceptions are registered for these functions. This is helpful when analyzing syntax errors, since kernel methods process information that only exists in the kernel modules.

Example
The following example is a simplified calculation class for floating
decimal place numbers. The class has an instance attribute in which the last result of each calculation is stored. A method executes a division and is implemented as a kernel method. If the divisor is zero, the method triggers a class-based exception.

Declaration Section of the Class in ABAP CLASS cl_my_calculation DEFINITION ...
...
DATA last_result TYPE decfloat16.
...
METHODS div
IMPORTING p_dividend TYPE decfloat16 p_divisor TYPE decfloat16
RETURNING VALUE(p_result) TYPE decfloat16.
...
ENDCLASS.

Signature File //src/krn/runt/abkmeth.sig in the kernel ...
KERNEL_METHOD(CL_MY_CALCULATION, DIV, xx_myDiv,4)
ARGUMENT_F(1, 'P_DIVIDEND', TYPE, 'F', READ)
ARGUMENT_F(2, 'P_DIVISOR', TYPE, 'F', READ)
ARGUMENT_F(3, 'P_RESULT', TYPE, 'F', WRITE)
ARGUMENT_F(4, 'ME->LAST_RESULT',TYPE, 'F', WRITE)
EXCEPTION('CX_MY_DIV_BY_ZERO')
...

C++ source code //src/krn/.../mycalc.cpp in the kernel #include 'abkmeth.h'
...
externC void xx_myDiv( ARGUMENTS ){

const SAP_DOUBLE *const dividend = ARGUMENT_F_READ(1,'P_DIVIDEND');
const SAP_DOUBLE *const divisor = ARGUMENT_F_READ(2,'P_DIVISOR');
SAP_DOUBLE *result = ARGUMENT_F_WRITE(3,'P_RESULT');
SAP_DOUBLE *last_result = ARGUMENT_F_WRITE(4 ,'ME->LAST_RESULT');

if( 0 == *divisor )
{
EXCEPTION_CREATE(CX_MY_DIV_BY_ZERO);
EXCEPTION_RAISE();
}

*result = *dividend / *divisor;
*last_result = *result;

}

Implementation Section of the Class in ABAP CLASS cl_my_calculation IMPLEMENTATION.
...
METHOD div BY KERNEL MODULE xx_myDiv.
ENDMETHOD.
...
ENDCLASS.

ARTICLE ( Version: 0112)

Kernel Methods
For internal use, kernel methods can be implemented in the
ABAP kernel instead of in the ABAP language.

Introduction
Kernel methods allow you to directly call ABAP kernel functions
implemented in C or C++. Kernel methods replace the previous concepts of C calls and system calls. No new C calls or system calls need to be introduced.
Kernel methods offer the same checks and security features as normal
ABAP methods. Except for the Constructors and the C Destructor, all ABAP methods can be implemented as kernel methods. An ABAP method can still be redefined as a kernel method and a kernel method can still be redefined as an ABAP method within a path of the inheritance hierarchy.
For C developers who want to implement a kernel method, an API is
available that allows simple, high-performance, and secure access to arguments. Class-based exceptions also continue to be supported.

Defining Kernel Methods

Declaration in ABAP
A kernel method is declared in the same way as a normal ABAP method, in
Class Builder or in the declaration section of a local class. Whether a method is implemented as a kernel method is not important for the declaration. In ABAP, this means that a kernel method can be used just like a normal ABAP method.

Implementation in ABAP
A method is specified as a kernel method in the implementation part of
the class using the optional addition BY KERNEL MODULE kmod1 kmod2 ... of the statement METHOD. kmod1, kmod2, ... are the names of kernel modules that implement the method. The ABAP implementation of a kernel method must be empty, which means that there cannot be any ABAP statements between METHOD and ENDMETHOD: METHOD meth BY KERNEL MODULE kmod1 kmod2 ...
ENDMETHOD.
Constructors and the C
Destructor cannot be implemented as kernel methods. There is a separate mechanism for the C Destructor.
After ABAP_KEY KERNEL MODULE, you can specify a list of kernel
modules kmod1, kmod2, ... Currently, you can only specify C functions of the kernel for kmod1, kmod2, ... The list after KERNEL MODULE is evaluated by the compiler from left to right. The first kernel module in the list that is registered in the kernel (see below) is used in the generation.
If no valid kernel module is found in the list, a syntax error occurs.
There are still two standard C functions that can appear at the end of the list: FAIL and IGNORE. If one of these functions is specified at the end of the list, then a syntax error is avoided if the previous list does not contain a valid module. IGNORE is used to ignore the call of a kernel method of this type (behavior as in an empty ABAP implementation) and, in the case of FAIL, a handleable exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised.

Examples METHOD meth BY KERNEL MODULE xx_impl_630 xx_impl_620 xx_impl_610.
First, the kernel is searched for xx_impl_630. The kernel is then
searched for nach xx_impl_620, and finally for xx_impl_610. If none of these functions are found, a syntax error is raised. METHOD meth BY KERNEL MODULE xx_impl_630 xx_impl_620 FAIL.
First, the kernel is searched for xx_impl_630. Then the kernel is
searched for xx_impl_620. If neither of these functions are found, a syntax error is not raised; a handleable exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised instead, when the method is called. METHOD meth BY KERNEL MODULE xx_impl_620 xx_impl_610 IGNORE.
First, the kernel is searched for xx_impl_620. Then the kernel is
searched for xx_impl_610. If none of the functions are found, a syntax error is not raised; the empty ABAP implementation is called instead, when the method is called.

Implementation in the Kernel
Currently, only C functions can be used as kernel modules of kernel
methods. The C functions can have any position in the kernel. No special includes from the ABAP runtime environment are required for implementing the C function. The C functions must have a specific interface. The interface itself is wrapped by a macro called ARGUMENTS. All required definitions and prototypes are in the include //src/include/abkmeth.h. This is the only include needed for defining C functions for kernel methods.
Since C functions can be defined in C and C++ , you must use externC
in C++: #include 'abkmeth.h'
...
externC void name_of_cmodule( ARGUMENTS )
{
...
}
A C function that implements a kernel method must be registered for the
kernel method. If, after METHOD meth BY KERNEL MODULE, you specify the name of a C function that was not registered for the kernel method, a syntax error occurs (as mentioned above). You can register several C functions for a kernel method. The sequence of the kernel modules kmod1, kmod2, ... specified in the list after METHOD meth BY KERNEL MODULE defines which of the registered C functions is used. This allows downward-compatible further development of kernel methods.
To make changes to the registration active, you must recompile the
destination lib of the project krn/runt and relink the kernel.

Registration
C functions are registered in the signature file
//src/krn/runt/abkmeth.sig using the following syntax for kernel methods (all ABAP IDs must be specified in uppercase letters): KERNEL_METHOD('CLASS','METH', cfunc,argcnt)
This definition registers the C function cfunc for the kernel
method meth of a global class class. The C function expects a number of argcnt arguments.
Kernel methods of local classes in class pools or other ABAP programs
are registered using the following macros: KERNEL_METHOD_CLASS_LOCAL('GCLASS','CLASS','METH',cmodule,argcnt) KERNEL_METHOD_PROGRAM_LOCAL('PROG','CLASS','METH',cmodule,argcnt)
The technique is the same as with KERNEL_METHOD, except that you
must specify the global class gclass for local classes in class pools and the program prog for program-local classes.

Registering Arguments
All ABAP data objects (such as parameters, attributes, or global data)
that are to be accessed in C functions for kernel methods, are treated as arguments of the C function.
The argument list of a C function for a kernel method is not limited to
the interface parameters of the ABAP method and does not have to contain these completely. Before you access arguments within C functions for kernel methods, these arguments must be registered.
The argcnt arguments must be registered immediately after the C
function is registered using KERNEL_METHOD. A single argument is defined (registered) using one of the following macros: ARGUMENT_basetype(index,'name',type_kind,'type',read_write) ARGUMENT_[C|N|X](index,'name',type_kind,'type',read_write,length) ARGUMENT_P(index,'name',type_kind,'type',read_write,length,decimals) ARGUMENT_STRUCT(index,'name',type_kind,'type',read_write,ctype)
These macros define an argument with the name
name and an index index.
You must use basetype to assign the type of the ABAP data object
according to the following table. If the basetype is C, N, X, P, or STRUCT, you must specify more parameters than for other types. basetypeABAP Data TypeType in C Cc with specified lengthSAP_CHAR (*) [Length] C_GENERICc without specified lengthSAP_CHAR* Xx with specified lengthSAP_RAW (*) [Length] X_GENERICx without specified lengthSAP_RAW* Nn with specified lengthSAP_CHAR (*) [Length] N_GENERICn without specified lengthSAP_CHAR* Pp with specified length and decimalsSAP_BCD (*) [Length] P_GENERICp without specified length and decimals SAP_BCD* DdSAP_DATE* TtSAP_TIME* INT1bSAP_INT1* INT2sSAP_SHORT* IiSAP_INT* FfSAP_DOUBLE* DECFLOAT16decfloat16DecFloat16 DECFLOAT34decfloat34DecFloat34 STRINGstringStrRef* XSTRINGxstringStrRef* TABLEAll table typesTABH_REF* OBJ_REFAll object referencesObjRef* DATA_REFAll data referencesFldRef* STRUCTAll structure typesRegistered type ctype* ANYanyvoid* DATAdatavoid* SIMPLEsimplevoid* CSEQUENCEcsequencevoid* XSEQUENCExsequencevoid* NUMERICnumericvoid* CLIKEclikeSAP_CHAR* C_POINTER%_c_pointervoid**
The macro parameters have the following meanings:

  name is the ID for any ABAP data object in uppercase letters that could also be used in an ABAP implementation of the kernel method. In particular, the ID can contain links with component selectors, for example me->attr or struc-comp.
  index is a sequential number from 1 to argcnt. The arguments are accessed using this index.
  For type_kind you can specify either TYPE or TYPE_REF_TO.
  type is the ID for any ABAP data type in uppercase letters that could also be used in an ABAP implementation of the kernel method. type_kind and type are used to check the interface of the kernel method in ABAP.
  For read_write you can specify either READ or WRITE . This defines whether you have read or write access to the argument and is evaluated in the access macros.
  length is used to specify the length of all ABAP data types with a generic length for ARGUMENT_[C|N|X|P]. In characters for c and n and in bytes for x and p.
  For ARGUMENT_P you use decimals to specify the number of decimal places.
  For ARGUMENT_STRUCT you use ctype to specify a suitable C type. This type should be generated from an ABAP type definition using saphfile.

Accessing Arguments
After registering the arguments, you can use the following macros to
access them within the C function. With the exception of the direct access to the data control block, the access macros do not require any includes from the ABAP runtime environment. ARGUMENT_basetype_READ(index,'name');
This macro returns the read address of an
argument with the type const ctype, where ctype is defined by basetype according to the above table. The index and name of the argument must be passed. You must specify additional parameters for the generic types (see below). You only need the index to access the argument. However, to make the C function more legible and ensure that additional consistency checks can be executed, you must also specify the name. If the kernel is compiled in debugging mode, the system executes a consistency check between index and name; the specified C type and ABAP type of the argument are also checked. In the case of an error, an appropriate ABAP runtime error is triggered ( KMETH_INVALID_ARGUMENT_ID, KMETH_INVALID_ARGUMENT_NAME, or KMETH_INVALID_CTYPE_LENG). No checks are made in the optimized kernel. ARGUMENT_basetype_WRITE(index,'name');
This macro has the same semantics as ARGUMENT_basetype_READ.
However, the system returns the write address. The system also checks whether the argument was defined as a write argument. If you try to write access a read-only argument (for example, a constant), this triggers the ABAP runtime error KMETH_ARGUMENT_READ_ONLY. ARGUMENT_[C|N]_READ(index,'name',lengthU);
ARGUMENT_[C|N]_WRITE(index,'name',lengthU);
ARGUMENT_X_READ(index,'name',lengthR);
ARGUMENT_X_WRITE(index,'name',lengthR);
With these macros you must specify the expected length in bytes
lengthR or in characters lengthU for the generic types c , x, and n ARGUMENT_P_READ(index,'name',lengthR,decimals);
ARGUMENT_P_WRITE(index,'name',lengthR,decimals);
With these macros you must specify the expected length in bytes (
lengthR) and the number of decimal places (decimals) for the generic type p. ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_READ(index,'name',size_tU);
ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_WRITE(index,'name',size_tU);
ARGUMENT_X_GENRIC_READ(index,'name',size_tR);
ARGUMENT_X_GENERIC_WRITE(index,'name',size_tR);
With these macros you must specify a variable of the type size_tU
or size_tR, containing the length in bytes or characters, for the types C_GENERIC, X_GENERIC, N_GENERIC, and CLIKE. ARGUMENT_P_GENERIC_READ(index,'name',size_tR,decimals);
ARGUMENT_P_GENERIC_WRITE(index,'name',size_tR,decimals);
With these macros you must specify a variable decimals (for the
decimal places) as well as the length size_tR for the type P_GENERIC. ARGUMENT_STRUCT_READ(index,'name',ctype);
ARGUMENT_STRUCT_READ(index,'name',ctype);
With these macros you must specify a suitable C typectype
for all structured types STRUCT. ARGUMENT_C_POINTER(index,'name');
This macro is available specifically for the
type %_c_pointer. This type is a special internal ABAP type that has exactly the byte length of a C pointer (4, 8, or 16 bytes, depending on platform). The type is always mapped to the predefined ABAP type x. The macros for the type X or X_GENERIC are not used due to the variable length and platform-dependency. ARGUMENT_IS_SUPPLIED(index,'name');
This macro has the same semantics as the logical expression IS
SUPPLIED in ABAP. The same consistency checks are executed as for ARGUMENT_READ. ARGUMENT_DATA(index,'name',ctype);
This macro returns the data control block with the C type
const DATA *. The same consistency checks are executed as for ARGUMENT_READ. The macro is only active if the include //src/include/abdata.h of the ABAP runtime environment was included.

Raising Exceptions
C functions that implement kernel method can raise
class-based exceptions.

Registering Exceptions
The relevant global exception classes must be registered with an
extension of //src/include/abexcpc.h. Local exception classes cannot be registered.
The exception class is declared in //src/include/abexcpc.h and
any text IDs are defined: //src/include/abexcpc.h
...
CX_ABSTR (CX_..., 'CX_...')
CX_TXTID (CX_..._bar, CX_..., 'BAR') /* special text for class */
...
Classes can also be declared with their standard text only:
//src/include/abexcpc.h
...
CX_CLASS (CX_..., 'CX_...') /* class with standard text */
...
The exact documentation is in the file //src/include/abexcpc.h.

You must extend the file //src/include/abexcpa.h so that any
attributes of an exception class in a C function can be populated; you must specify the name, internal type (according to //src/include/abtypes.h) and the byte length: //src/include/abexcpa.h
...
CX_ATTR (CX_..._attr1, CX_..., 'ATTR1', TYPCSTRING, sizeofR(StrRef))
CX_ATTR (CX_..._attr2, CX_..., 'ATTR2', TYPC, LEN_UC2RAW(30))
...
Finally, you must register exceptions as well as arguments in the file
//src/krn/runt/abkmeth.sig. This is not forced but, during the syntax check, only registered exceptions are checked for their existence: //src/krn/runt/abkmeth.sig
...
EXCEPTION(CX_...)
...

Raising Exceptions
A C function can raise an exception by calling the following macros
consecutively: EXCEPTION_CREATE(CX_..._bar);
EXCEPTION_SET_CSTRING(CX_..._attr1, value, valueLength);
EXCEPTION_SET_C (CX_..._attr2, value, valueLength);
EXCEPTION_RAISE();
Within the macros EXCEPTION_CREATE or EXCEPTION_RAISE, a
long jump to Extri always takes place, which means that the C function that implements the kernel method is exited in a long jump and the ABAP runtime environment takes control. Therefore, the C function should release its temporary memory before raising an exception. If the exception is caught in ABAP using CATCH without the INTO addition, the long jump takes place in EXCEPTION_CREATE. If the exception is caught with the INTO addition (the exception object is used) or not at all, the long jump takes place in EXCEPTION_RAISE.
The exceptions are processed in the runtime environment, as if they were
raised in ABAP and the same dynamic checks are executed.
Currently, the following macros, which can be
extended if necessary, are available for setting exception attributes. Strings, integer and C fields are supported. See the above sequence for use. EXCEPTION_SET_CSTRING_UC
EXCEPTION_SET_C
Value with length specified
EXCEPTION_SET_C_UC
EXCEPTION_SET_INT
Value with null termination


Auxiliary Program for Kernel Methods
The ABAP program RSKMETH serves as a browser
for the registration of kernel modules. You can use it to ascertain which C functions are registered for which kernel methods and which arguments/exceptions are registered for these functions. This is helpful when analyzing syntax errors, since kernel methods process information that only exists in the kernel modules.

Example
The following example is a simplified calculation class for floating
decimal place numbers. The class has an instance attribute in which the last result of each calculation is stored. A method executes a division and is implemented as a kernel method. If the divisor is zero, the method triggers a class-based exception.

Declaration Section of the Class in ABAP CLASS cl_my_calculation DEFINITION ...
...
DATA last_result TYPE decfloat16.
...
METHODS div
IMPORTING p_dividend TYPE decfloat16 p_divisor TYPE decfloat16
RETURNING VALUE(p_result) TYPE decfloat16.
...
ENDCLASS.

Signature File //src/krn/runt/abkmeth.sig in the kernel ...
KERNEL_METHOD(CL_MY_CALCULATION, DIV, xx_myDiv,4)
ARGUMENT_F(1, 'P_DIVIDEND', TYPE, 'F', READ)
ARGUMENT_F(2, 'P_DIVISOR', TYPE, 'F', READ)
ARGUMENT_F(3, 'P_RESULT', TYPE, 'F', WRITE)
ARGUMENT_F(4, 'ME->LAST_RESULT',TYPE, 'F', WRITE)
EXCEPTION('CX_MY_DIV_BY_ZERO')
...

C++ source code //src/krn/.../mycalc.cpp in the kernel #include 'abkmeth.h'
...
externC void xx_myDiv( ARGUMENTS ){

const SAP_DOUBLE *const dividend = ARGUMENT_F_READ(1,'P_DIVIDEND');
const SAP_DOUBLE *const divisor = ARGUMENT_F_READ(2,'P_DIVISOR');
SAP_DOUBLE *result = ARGUMENT_F_WRITE(3,'P_RESULT');
SAP_DOUBLE *last_result = ARGUMENT_F_WRITE(4 ,'ME->LAST_RESULT');

if( 0 == *divisor )
{
EXCEPTION_CREATE(CX_MY_DIV_BY_ZERO);
EXCEPTION_RAISE();
}

*result = *dividend / *divisor;
*last_result = *result;

}

Implementation Section of the Class in ABAP CLASS cl_my_calculation IMPLEMENTATION.
...
METHOD div BY KERNEL MODULE xx_myDiv.
ENDMETHOD.
...
ENDCLASS.

ARTICLE ( Version: 0113)

Kernel Methods
For internal use, kernel methods can be implemented in the
ABAP kernel instead of in the ABAP language.

Introduction
Kernel methods allow ABAP kernel functions implemented in C or C++ to be
called directly. Kernel methods replace the previous concepts of C calls and system calls. No new C calls or system calls need to be introduced.
Kernel methods offer the same checks and security features as normal
ABAP methods. Except for the Constructors and the C Destructor, all ABAP methods can be implemented as kernel methods. An ABAP method can still be redefined as a kernel method and a kernel method can still be redefined as an ABAP method within a path of the inheritance hierarchy.
For C developers who want to implement a kernel method, an API is
available that allows simple, high-performance, and secure access to arguments. Class-based exceptions also continue to be supported.

Defining Kernel Methods

Declaration in ABAP
A kernel method is declared in the same way as a normal ABAP method, in
Class Builder or in the declaration section of a local class. Whether a method is implemented as a kernel method is not important for the declaration. In ABAP, this means that a kernel method can be used just like a normal ABAP method.

Implementation in ABAP
A method is specified as a kernel method in the implementation part of
the class using the optional addition BY KERNEL MODULE kmod1 kmod2 ... of the statement METHOD. kmod1, kmod2, ... are the names of kernel modules that implement the method. The ABAP implementation of a kernel method must be empty, which means that there cannot be any ABAP statements between METHOD and ENDMETHOD: METHOD meth BY KERNEL MODULE kmod1 kmod2 ...
ENDMETHOD.
Constructors and the C
Destructor cannot be implemented as kernel methods. There is a separate mechanism for the C Destructor.
After ABAP_KEY KERNEL MODULE, a list of kernel modules kmod1
, kmod2, ... can be specified. Currently, only C functions of the kernel can be specified for kmod1, kmod2, ... The list after KERNEL MODULE is evaluated by the compiler from left to right. The first kernel module in the list that is registered in the kernel (see below) is used in the generation.
If no valid kernel module is found in the list, a syntax error occurs.
There are still two standard C functions that can appear at the end of the list: FAIL and IGNORE. If one of these functions is specified at the end of the list, then a syntax error is avoided if the previous list does not contain a valid module. IGNORE is used to ignore the call of a kernel method of this type (behavior as in an empty ABAP implementation) and, in the case of FAIL, a handleable exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised.

Examples METHOD meth BY KERNEL MODULE xx_impl_630 xx_impl_620 xx_impl_610.
First, the kernel is searched for xx_impl_630. The kernel is then
searched for xx_impl_620, and finally for xx_impl_610. If none of these functions are found, a syntax error is raised. METHOD meth BY KERNEL MODULE xx_impl_630 xx_impl_620 FAIL.
First, the kernel is searched for xx_impl_630. Then the kernel is
searched for xx_impl_620. If neither of these functions are found, a syntax error is not raised; a handleable exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised instead, when the method is called. METHOD meth BY KERNEL MODULE xx_impl_620 xx_impl_610 IGNORE.
First, the kernel is searched for xx_impl_620. Then the kernel is
searched for xx_impl_610. If none of the functions are found, a syntax error is not raised; the empty ABAP implementation is called instead, when the method is called.

Implementation in the Kernel
Currently, only C functions can be used as kernel modules of kernel
methods. The C functions can have any position in the kernel. No special includes from the ABAP runtime environment are required for implementing the C function. The C functions must have a specific interface. The interface itself is wrapped by a macro called ARGUMENTS. All required definitions and prototypes are in the include //src/include/abkmeth.h. This is the only include needed for defining C functions for kernel methods.
Since C functions can be defined in C and C++ , externC must be
used in C++: #include 'abkmeth.h'
...
externC void name_of_cmodule( ARGUMENTS )
{
...
}
A C function that implements a kernel method must be registered for the
kernel method. If, after METHOD meth BY KERNEL MODULE, the name of a C function is specified that was not registered for the kernel method, a syntax error occurs (as mentioned above). Multiple C functions can be registered for one kernel method. The sequence of the kernel modules kmod1, kmod2, ... specified in the list after METHOD meth BY KERNEL MODULE defines which of the registered C functions is used. This allows downward-compatible further development of kernel methods.
To make changes to the registration active, the destination lib
of the project krn/runt must be recompiled and the kernel relinked.

Registration
C functions are registered in the signature file
//src/krn/runt/abkmeth.sig using the following syntax for kernel methods (all ABAP IDs must be specified in uppercase letters): KERNEL_METHOD('CLASS','METH', cfunc,argcnt)
This definition registers the C function cfunc for the kernel
method meth of a global class class. The C function expects a number of argcnt arguments.
Kernel methods of local classes in class pools or other ABAP programs
are registered using the following macros: KERNEL_METHOD_CLASS_LOCAL('GCLASS','CLASS','METH',cmodule,argcnt) KERNEL_METHOD_PROGRAM_LOCAL('PROG','CLASS','METH',cmodule,argcnt)
The technique is the same as with KERNEL_METHOD, except that the
global class gclass must be specified for local classes in class pools and the program prog for program-local classes.

Registering Arguments
All ABAP data objects (such as parameters, attributes, or global data)
that are to be accessed in C functions for kernel methods, are treated as arguments of the C function.
The argument list of a C function for a kernel method is not limited to
the interface parameters of the ABAP method and does not have to contain these completely. Before arguments within C functions are accessed for kernel methods, these arguments must be registered.
The argcnt arguments must be registered immediately after the C
function is registered using KERNEL_METHOD. A single argument is defined (registered) using one of the following macros: ARGUMENT_basetype(index,'name',type_kind,'type',read_write) ARGUMENT_[C|N|X](index,'name',type_kind,'type',read_write,length) ARGUMENT_P(index,'name',type_kind,'type',read_write,length,decimals) ARGUMENT_STRUCT(index,'name',type_kind,'type',read_write,ctype)
These macros define an argument with the name
name and an index index.
basetype must be used to assign the type of the ABAP data object
in accordance with the following table. If the basetype is C , N, X, P, or STRUCT, more parameters must be specified than for other types. basetypeABAP Data TypeType in C Cc with specified lengthSAP_CHAR (*) [Length] C_GENERICc without specified lengthSAP_CHAR* Xx with specified lengthSAP_RAW (*) [Length] X_GENERICx without specified lengthSAP_RAW* Nn with specified lengthSAP_CHAR (*) [Length] N_GENERICn without specified lengthSAP_CHAR* Pp with specified length and decimalsSAP_BCD (*) [Length] P_GENERICp without specified length and decimals SAP_BCD* DdSAP_DATE* TtSAP_TIME* INT1bSAP_INT1* INT2sSAP_SHORT* IiSAP_INT* FfSAP_DOUBLE* DECFLOAT16decfloat16DecFloat16 DECFLOAT34decfloat34DecFloat34 STRINGstringStrRef* XSTRINGxstringStrRef* TABLEAll table typesTABH_REF* OBJ_REFAll object referencesObjRef* DATA_REFAll data referencesFldRef* STRUCTAll structure typesRegistered type ctype* ANYanyvoid* DATAdatavoid* SIMPLEsimplevoid* CSEQUENCEcsequencevoid* XSEQUENCExsequencevoid* NUMERICnumericvoid* CLIKEclikeSAP_CHAR* C_POINTER%_c_pointervoid**
The macro parameters have the following meanings:

  name is the ID for any ABAP data object in uppercase letters that could also be used in an ABAP implementation of the kernel method. In particular, the ID can contain links with component selectors, for example me->attr or struc-comp.
  index is a sequential number from 1 to argcnt. The arguments are accessed using this index.
  For type_kind, either TYPE or TYPE_REF_TO can be specified.
  type is the ID for any ABAP data type in uppercase letters that could also be used in an ABAP implementation of the kernel method. type_kind and type are used to check the interface of the kernel method in ABAP.
  For read_write, either READ or WRITE can be specified. This defines whether reads or writes can be performed on the argument and this setting is evaluated in the access macros.
  length is used to specify the length of all ABAP data types with a generic length for ARGUMENT_[C|N|X|P]. In characters for c and n and in bytes for x and p.
  When using ARGUMENT_P, decimals must be used to specify the number of decimal places.
  When using ARGUMENT_STRUCT, ctype must be used to specify a suitable C type. This type should be generated from an ABAP type definition using saphfile.

Accessing Arguments
After registering the arguments, the following macros can be used to
access them within the C function. With the exception of the direct access to the data control block, the access macros do not require any includes from the ABAP runtime environment. ARGUMENT_basetype_READ(index,'name');
This macro returns the read address of an
argument with the type const ctype, where ctype is defined by basetype according to the above table. The index and name of the argument must be passed. Additional parameters must be specified for the generic types (see below). Only the index is needed to access the argument. However, to make the C function more legible and to ensure that additional consistency checks can be executed, the name must also be specified. If the kernel is compiled in debugging mode, the system executes a consistency check between index and name; the specified C type and ABAP type of the argument are also checked. In the case of an error, an appropriate ABAP runtime error is triggered ( KMETH_INVALID_ARGUMENT_ID, KMETH_INVALID_ARGUMENT_NAME, or KMETH_INVALID_CTYPE_LENG). No checks are made in the optimized kernel. ARGUMENT_basetype_WRITE(index,'name');
This macro has the same semantics as ARGUMENT_basetype_READ.
However, the system returns the write address. The system also checks whether the argument was defined as a write argument. If a write is performed on a read-only argument (for example, a constant), this triggers the ABAP runtime error KMETH_ARGUMENT_READ_ONLY. ARGUMENT_[C|N]_READ(index,'name',lengthU);
ARGUMENT_[C|N]_WRITE(index,'name',lengthU);
ARGUMENT_X_READ(index,'name',lengthR);
ARGUMENT_X_WRITE(index,'name',lengthR);
If these macros are used, the expected length in bytes lengthR or
in characters lengthU must be specified for the generic types c, x, and n. ARGUMENT_P_READ(index,'name',lengthR,decimals);
ARGUMENT_P_WRITE(index,'name',lengthR,decimals);
If these macros are used, the expected length in bytes (lengthR)
and the number of decimal places (decimals) must be specified for the generic type p. ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_READ(index,'name',size_tU);
ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_WRITE(index,'name',size_tU);
ARGUMENT_X_GENRIC_READ(index,'name',size_tR);
ARGUMENT_X_GENERIC_WRITE(index,'name',size_tR);
When using these macros, a variable of the type size_tU or
size_tR, containing the length in bytes or characters, must be specified for the types C_GENERIC, X_GENERIC, N_GENERIC, and CLIKE. ARGUMENT_P_GENERIC_READ(index,'name',size_tR,decimals);
ARGUMENT_P_GENERIC_WRITE(index,'name',size_tR,decimals);
If these macros are used, a variable decimals (for the
decimal places) as well as the length size_tR must be specified for the type P_GENERIC. ARGUMENT_STRUCT_READ(index,'name',ctype);
ARGUMENT_STRUCT_READ(index,'name',ctype);
If these macros are used, a suitable C typectype must be
specified for all structured types STRUCT. ARGUMENT_C_POINTER(index,'name');
This macro is available specifically for the
type %_c_pointer. This type is a special internal ABAP type that has exactly the byte length of a C pointer (4, 8, or 16 bytes, depending on platform). The type is always mapped to the predefined ABAP type x. The macros for the type X or X_GENERIC are not used due to the variable length and platform-dependency. ARGUMENT_IS_SUPPLIED(index,'name');
This macro has the same semantics as the logical expression IS
SUPPLIED in ABAP. The same consistency checks are executed as for ARGUMENT_READ. ARGUMENT_DATA(index,'name',ctype);
This macro returns the data control block with the C type
const DATA *. The same consistency checks are executed as for ARGUMENT_READ. The macro is only active if the include //src/include/abdata.h of the ABAP runtime environment was included.

Raising Exceptions
C functions that implement kernel method can raise
class-based exceptions.

Registering Exceptions
The relevant global exception classes must be registered with an
extension of //src/include/abexcpc.h. Local exception classes cannot be registered.
The exception class is declared in //src/include/abexcpc.h and
any text IDs are defined: //src/include/abexcpc.h
...
CX_ABSTR (CX_..., 'CX_...')
CX_TXTID (CX_..._bar, CX_..., 'BAR') /* special text for class */
...
Classes can also be declared with their standard text only:
//src/include/abexcpc.h
...
CX_CLASS (CX_..., 'CX_...') /* class with standard text */
...
The exact documentation is in the file //src/include/abexcpc.h.

The file //src/include/abexcpa.h must be extended so that any
attributes of an exception class in a C function can be populated. The name, internal type (according to //src/include/abtypes.h), and the byte length must be specified: //src/include/abexcpa.h
...
CX_ATTR (CX_..._attr1, CX_..., 'ATTR1', TYPCSTRING, sizeofR(StrRef))
CX_ATTR (CX_..._attr2, CX_..., 'ATTR2', TYPC, LEN_UC2RAW(30))
...
Finally, both exceptions and arguments must be registered in the file
//src/krn/runt/abkmeth.sig. This is not forced but, during the syntax check, only registered exceptions are checked for their existence: //src/krn/runt/abkmeth.sig
...
EXCEPTION(CX_...)
...

Raising Exceptions
A C function can raise an exception by calling the following macros
consecutively: EXCEPTION_CREATE(CX_..._bar);
EXCEPTION_SET_CSTRING(CX_..._attr1, value, valueLength);
EXCEPTION_SET_C (CX_..._attr2, value, valueLength);
EXCEPTION_RAISE();
Within the macros EXCEPTION_CREATE or EXCEPTION_RAISE, a
long jump to Extri always takes place, which means that the C function that implements the kernel method is exited in a long jump and the ABAP runtime environment takes control. Therefore, the C function should release its temporary memory before raising an exception. If the exception is caught in ABAP using CATCH without the INTO addition, the long jump takes place in EXCEPTION_CREATE. If the exception is caught with the INTO addition (the exception object is used) or not at all, the long jump takes place in EXCEPTION_RAISE.
The exceptions are processed in the runtime environment, as if they were
raised in ABAP and the same dynamic checks are executed.
Currently, the following macros, which can be
extended if necessary, are available for setting exception attributes. Strings, integer and C fields are supported. See the above sequence for use. EXCEPTION_SET_CSTRING_UC
EXCEPTION_SET_C
Value with length specified
EXCEPTION_SET_C_UC
EXCEPTION_SET_INT
Value with null termination


Auxiliary Program for Kernel Methods
The ABAP program RSKMETH serves as a browser
for the registration of kernel modules. This can be used to ascertain which C functions are registered for which kernel methods and which arguments/exceptions are registered for these functions. This is helpful when analyzing syntax errors, since kernel methods process information that only exists in the kernel modules.

Example
The following example is a simplified calculation class for floating
decimal place numbers. The class has an instance attribute in which the last result of each calculation is stored. A method executes a division and is implemented as a kernel method. If the divisor is zero, the method triggers a class-based exception.

Declaration Section of the Class in ABAP CLASS cl_my_calculation DEFINITION ...
...
DATA last_result TYPE decfloat16.
...
METHODS div
IMPORTING p_dividend TYPE decfloat16 p_divisor TYPE decfloat16
RETURNING VALUE(p_result) TYPE decfloat16.
...
ENDCLASS.

Signature File //src/krn/runt/abkmeth.sig in the kernel ...
KERNEL_METHOD(CL_MY_CALCULATION, DIV, xx_myDiv,4)
ARGUMENT_F(1, 'P_DIVIDEND', TYPE, 'F', READ)
ARGUMENT_F(2, 'P_DIVISOR', TYPE, 'F', READ)
ARGUMENT_F(3, 'P_RESULT', TYPE, 'F', WRITE)
ARGUMENT_F(4, 'ME->LAST_RESULT',TYPE, 'F', WRITE)
EXCEPTION('CX_MY_DIV_BY_ZERO')
...

C++ source code //src/krn/.../mycalc.cpp in the kernel #include 'abkmeth.h'
...
externC void xx_myDiv( ARGUMENTS ){

const SAP_DOUBLE *const dividend = ARGUMENT_F_READ(1,'P_DIVIDEND');
const SAP_DOUBLE *const divisor = ARGUMENT_F_READ(2,'P_DIVISOR');
SAP_DOUBLE *result = ARGUMENT_F_WRITE(3,'P_RESULT');
SAP_DOUBLE *last_result = ARGUMENT_F_WRITE(4 ,'ME->LAST_RESULT');

if( 0 == *divisor )
{
EXCEPTION_CREATE(CX_MY_DIV_BY_ZERO);
EXCEPTION_RAISE();
}

*result = *dividend / *divisor;
*last_result = *result;

}

Implementation Section of the Class in ABAP CLASS cl_my_calculation IMPLEMENTATION.
...
METHOD div BY KERNEL MODULE xx_myDiv.
ENDMETHOD.
...
ENDCLASS.

ARTICLE ( Version: 0117)

Kernel Methods
For internal use, kernel methods can be implemented in the
ABAP kernel instead of in the ABAP language.

Introduction
Kernel methods allow ABAP kernel functions implemented in C or C++ to be
called directly. Kernel methods replace the previous concepts of C calls and system calls. No new C calls or system calls need to be introduced.
Kernel methods offer the same checks and security features as normal
ABAP methods. Except for the Constructors and the C Destructor, all ABAP methods can be implemented as kernel methods. An ABAP method can still be redefined as a kernel method and a kernel method can still be redefined as an ABAP method within a path of the inheritance hierarchy.
For C developers who want to implement a kernel method, an API is
available that allows simple, high-performance, and secure access to arguments. Class-based exceptions also continue to be supported.

Defining Kernel Methods

Declaration in ABAP
A kernel method is declared in the same way as a normal ABAP method, in
Class Builder or in the declaration section of a local class. Whether a method is implemented as a kernel method is not important for the declaration. In ABAP, this means that a kernel method can be used just like a normal ABAP method.

Implementation in ABAP
A method is specified as a kernel method in the implementation part of
the class using the optional addition BY KERNEL MODULE kmod1 kmod2 ... of the statement METHOD. kmod1, kmod2, ... are the names of kernel modules that implement the method. The ABAP implementation of a kernel method must be empty, which means that there cannot be any ABAP statements between METHOD and ENDMETHOD: METHOD meth BY KERNEL MODULE kmod1 kmod2 ...
ENDMETHOD.
Constructors and the C
Destructor cannot be implemented as kernel methods. There is a separate mechanism for the C Destructor.
After ABAP_KEY KERNEL MODULE, a list of kernel modules kmod1
, kmod2, ... can be specified. Currently, only C functions of the kernel can be specified for kmod1, kmod2, ... The list after KERNEL MODULE is evaluated by the compiler from left to right. The first kernel module in the list that is registered in the kernel (see below) is used in the generation.
If no valid kernel module is found in the list, a syntax error occurs.
There are still two standard C functions that can appear at the end of the list: FAIL and IGNORE. If one of these functions is specified at the end of the list, then a syntax error is avoided if the previous list does not contain a valid module. IGNORE is used to ignore the call of a kernel method of this type (behavior as in an empty ABAP implementation) and, in the case of FAIL, a handleable exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised.

Examples METHOD meth BY KERNEL MODULE xx_impl_630 xx_impl_620 xx_impl_610.
First, the kernel is searched for xx_impl_630. The kernel is then
searched for xx_impl_620, and finally for xx_impl_610. If none of these functions are found, a syntax error is raised. METHOD meth BY KERNEL MODULE xx_impl_630 xx_impl_620 FAIL.
First, the kernel is searched for xx_impl_630. Then the kernel is
searched for xx_impl_620. If neither of these functions are found, a syntax error is not raised; a handleable exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD is raised instead, when the method is called. METHOD meth BY KERNEL MODULE xx_impl_620 xx_impl_610 IGNORE.
First, the kernel is searched for xx_impl_620. Then the kernel is
searched for xx_impl_610. If none of the functions are found, a syntax error is not raised; the empty ABAP implementation is called instead, when the method is called.

Implementation in the Kernel
Currently, only C functions can be used as kernel modules of kernel
methods. The C functions can have any position in the kernel. No special includes from the ABAP runtime environment are required for implementing the C function. The C functions must have a specific interface. The interface itself is wrapped by a macro called ARGUMENTS. All required definitions and prototypes are in the include //src/include/abkmeth.h. This is the only include needed for defining C functions for kernel methods.
Since C functions can be defined in C and C++ , externC must be
used in C++: #include 'abkmeth.h'
...
externC void name_of_cmodule( ARGUMENTS )
{
...
}
A C function that implements a kernel method must be registered for the
kernel method. If, after METHOD meth BY KERNEL MODULE, the name of a C function is specified that was not registered for the kernel method, a syntax error occurs (as mentioned above). Multiple C functions can be registered for one kernel method. The sequence of the kernel modules kmod1, kmod2, ... specified in the list after METHOD meth BY KERNEL MODULE defines which of the registered C functions is used. This allows downward-compatible further development of kernel methods.
To make changes to the registration active, the destination lib
of the project krn/runt must be recompiled and the kernel relinked.

Registration
C functions are registered in the signature file
//src/krn/runt/abkmeth.sig using the following syntax for kernel methods (all ABAP IDs must be specified in uppercase letters): KERNEL_METHOD('CLASS','METH', cfunc,argcnt)
This definition registers the C function cfunc for the kernel
method meth of a global class class. The C function expects a number of argcnt arguments.
Kernel methods of local classes in class pools or other ABAP programs
are registered using the following macros: KERNEL_METHOD_CLASS_LOCAL('GCLASS','CLASS','METH',cmodule,argcnt) KERNEL_METHOD_PROGRAM_LOCAL('PROG','CLASS','METH',cmodule,argcnt)
The technique is the same as with KERNEL_METHOD, except that the
global class gclass must be specified for local classes in class pools and the program prog for program-local classes.

Registering Arguments
All ABAP data objects (such as parameters, attributes, or global data)
that are to be accessed in C functions for kernel methods, are treated as arguments of the C function.
The argument list of a C function for a kernel method is not limited to
the interface parameters of the ABAP method and does not have to contain these completely. Before arguments within C functions are accessed for kernel methods, these arguments must be registered.
The argcnt arguments must be registered immediately after the C
function is registered using KERNEL_METHOD. A single argument is defined (registered) using one of the following macros: ARGUMENT_basetype(index,'name',type_kind,'type',read_write) ARGUMENT_[C|N|X](index,'name',type_kind,'type',read_write,length) ARGUMENT_P(index,'name',type_kind,'type',read_write,length,decimals) ARGUMENT_STRUCT(index,'name',type_kind,'type',read_write,ctype)
These macros define an argument with the name
name and an index index.
basetype must be used to assign the type of the ABAP data object
in accordance with the following table. If the basetype is C , N, X, P, or STRUCT, more parameters must be specified than for other types. basetypeABAP Data TypeType in C Cc with specified lengthSAP_CHAR (*) [Length] C_GENERICc without specified lengthSAP_CHAR* Xx with specified lengthSAP_RAW (*) [Length] X_GENERICx without specified lengthSAP_RAW* Nn with specified lengthSAP_CHAR (*) [Length] N_GENERICn without specified lengthSAP_CHAR* Pp with specified length and decimalsSAP_BCD (*) [Length] P_GENERICp without specified length and decimals SAP_BCD* DdSAP_DATE* TtSAP_TIME* INT1bSAP_INT1* INT2sSAP_SHORT*

INT8sSAP_LLONG* ( Version: 0117) IiSAP_INT* FfSAP_DOUBLE* DECFLOAT16decfloat16DecFloat16 DECFLOAT34decfloat34DecFloat34 STRINGstringStrRef* XSTRINGxstringStrRef* TABLEAll table typesTABH_REF* OBJ_REFAll object referencesObjRef* DATA_REFAll data referencesFldRef* STRUCTAll structure typesRegistered type ctype* ANYanyvoid* DATAdatavoid* SIMPLEsimplevoid* CSEQUENCEcsequencevoid* XSEQUENCExsequencevoid* NUMERICnumericvoid* CLIKEclikeSAP_CHAR* C_POINTER%_c_pointervoid**
The macro parameters have the following meanings:

  name is the ID for any ABAP data object in uppercase letters that could also be used in an ABAP implementation of the kernel method. In particular, the ID can contain links with component selectors, for example me->attr or struc-comp.
  index is a sequential number from 1 to argcnt. The arguments are accessed using this index.
  For type_kind, either TYPE or TYPE_REF_TO can be specified.
  type is the ID for any ABAP data type in uppercase letters that could also be used in an ABAP implementation of the kernel method. type_kind and type are used to check the interface of the kernel method in ABAP.
  For read_write, either READ or WRITE can be specified. This defines whether reads or writes can be performed on the argument and this setting is evaluated in the access macros.
  length is used to specify the length of all ABAP data types with a generic length for ARGUMENT_[C|N|X|P]. In characters for c and n and in bytes for x and p.
  When using ARGUMENT_P, decimals must be used to specify the number of decimal places.
  When using ARGUMENT_STRUCT, ctype must be used to specify a suitable C type. This type should be generated from an ABAP type definition using saphfile.

Accessing Arguments
After registering the arguments, the following macros can be used to
access them within the C function. With the exception of the direct access to the data control block, the access macros do not require any includes from the ABAP runtime environment. ARGUMENT_basetype_READ(index,'name');
This macro returns the read address of an
argument with the type const ctype, where ctype is defined by basetype according to the above table. The index and name of the argument must be passed. Additional parameters must be specified for the generic types (see below). Only the index is needed to access the argument. However, to make the C function more legible and to ensure that additional consistency checks can be executed, the name must also be specified. If the kernel is compiled in debugging mode, the system executes a consistency check between index and name; the specified C type and ABAP type of the argument are also checked. In the case of an error, an appropriate ABAP runtime error is triggered ( KMETH_INVALID_ARGUMENT_ID, KMETH_INVALID_ARGUMENT_NAME, or KMETH_INVALID_CTYPE_LENG). No checks are made in the optimized kernel. ARGUMENT_basetype_WRITE(index,'name');
This macro has the same semantics as ARGUMENT_basetype_READ.
However, the system returns the write address. The system also checks whether the argument was defined as a write argument. If a write is performed on a read-only argument (for example, a constant), this triggers the ABAP runtime error KMETH_ARGUMENT_READ_ONLY. ARGUMENT_[C|N]_READ(index,'name',lengthU);
ARGUMENT_[C|N]_WRITE(index,'name',lengthU);
ARGUMENT_X_READ(index,'name',lengthR);
ARGUMENT_X_WRITE(index,'name',lengthR);
If these macros are used, the expected length in bytes lengthR or
in characters lengthU must be specified for the generic types c, x, and n. ARGUMENT_P_READ(index,'name',lengthR,decimals);
ARGUMENT_P_WRITE(index,'name',lengthR,decimals);
If these macros are used, the expected length in bytes (lengthR)
and the number of decimal places (decimals) must be specified for the generic type p. ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_READ(index,'name',size_tU);
ARGUMENT_[C_GENERIC|N_GENERIC|CLIKE]_WRITE(index,'name',size_tU);
ARGUMENT_X_GENRIC_READ(index,'name',size_tR);
ARGUMENT_X_GENERIC_WRITE(index,'name',size_tR);
When using these macros, a variable of the type size_tU or
size_tR, containing the length in bytes or characters, must be specified for the types C_GENERIC, X_GENERIC, N_GENERIC, and CLIKE. ARGUMENT_P_GENERIC_READ(index,'name',size_tR,decimals);
ARGUMENT_P_GENERIC_WRITE(index,'name',size_tR,decimals);
If these macros are used, a variable decimals (for the
decimal places) as well as the length size_tR must be specified for the type P_GENERIC. ARGUMENT_STRUCT_READ(index,'name',ctype);
ARGUMENT_STRUCT_READ(index,'name',ctype);
If these macros are used, a suitable C typectype must be
specified for all structured types STRUCT. ARGUMENT_C_POINTER(index,'name');
This macro is available specifically for the
type %_c_pointer. This type is a special internal ABAP type that has exactly the byte length of a C pointer (4, 8, or 16 bytes, depending on platform). The type is always mapped to the predefined ABAP type x. The macros for the type X or X_GENERIC are not used due to the variable length and platform-dependency. ARGUMENT_IS_SUPPLIED(index,'name');
This macro has the same semantics as the
predicate expression IS SUPPLIED in ABAP. The same consistency checks are executed as for ARGUMENT_READ. ARGUMENT_DATA(index,'name',ctype);
This macro returns the data control block with the C type
const DATA *. The same consistency checks are executed as for ARGUMENT_READ. The macro is only active if the include //src/include/abdata.h of the ABAP runtime environment was included.

Raising Exceptions
C functions that implement kernel method can raise
class-based exceptions.

Registering Exceptions
The relevant global exception classes must be registered with an
extension of //src/include/abexcpc.h. Local exception classes cannot be registered.
The exception class is declared in //src/include/abexcpc.h and
any text IDs are defined: //src/include/abexcpc.h
...
CX_ABSTR (CX_..., 'CX_...')
CX_TXTID (CX_..._bar, CX_..., 'BAR') /* special text for class */
...
Classes can also be declared with their standard text only:
//src/include/abexcpc.h
...
CX_CLASS (CX_..., 'CX_...') /* class with standard text */
...
The exact documentation is in the file //src/include/abexcpc.h.

The file //src/include/abexcpa.h must be extended so that any
attributes of an exception class in a C function can be populated. The name, internal type (according to //src/include/abtypes.h), and the byte length must be specified: //src/include/abexcpa.h
...
CX_ATTR (CX_..._attr1, CX_..., 'ATTR1', TYPCSTRING, sizeofR(StrRef))
CX_ATTR (CX_..._attr2, CX_..., 'ATTR2', TYPC, LEN_UC2RAW(30))
...
Finally, both exceptions and arguments must be registered in the file
//src/krn/runt/abkmeth.sig. This is not forced but, during the syntax check, only registered exceptions are checked for their existence: //src/krn/runt/abkmeth.sig
...
EXCEPTION(CX_...)
...

Raising Exceptions
A C function can raise an exception by calling the following macros
consecutively: EXCEPTION_CREATE(CX_..._bar);
EXCEPTION_SET_CSTRING(CX_..._attr1, value, valueLength);
EXCEPTION_SET_C (CX_..._attr2, value, valueLength);
EXCEPTION_RAISE();
Within the macros EXCEPTION_CREATE or EXCEPTION_RAISE, a
long jump to Extri always takes place, which means that the C function that implements the kernel method is exited in a long jump and the ABAP runtime environment takes control. Therefore, the C function should release its temporary memory before raising an exception. If the exception is caught in ABAP using CATCH without the INTO addition, the long jump takes place in EXCEPTION_CREATE. If the exception is caught with the INTO addition (the exception object is used) or not at all, the long jump takes place in EXCEPTION_RAISE.
The exceptions are processed in the runtime environment, as if they were
raised in ABAP and the same dynamic checks are executed.
Currently, the following macros, which can be
extended if necessary, are available for setting exception attributes. Strings, integer and C fields are supported. See the above sequence for use. EXCEPTION_SET_CSTRING_UC
EXCEPTION_SET_C
Value with length specified
EXCEPTION_SET_C_UC
EXCEPTION_SET_INT
Value with null termination


Auxiliary Program for Kernel Methods
The ABAP program RSKMETH serves as a browser
for the registration of kernel modules. This can be used to ascertain which C functions are registered for which kernel methods and which arguments/exceptions are registered for these functions. This is helpful when analyzing syntax errors, since kernel methods process information that only exists in the kernel modules.

Example
The following example is a simplified calculation class for floating
decimal place numbers. The class has an instance attribute in which the last result of each calculation is stored. A method executes a division and is implemented as a kernel method. If the divisor is zero, the method triggers a class-based exception.

Declaration Section of the Class in ABAP CLASS cl_my_calculation DEFINITION ...
...
DATA last_result TYPE decfloat16.
...
METHODS div
IMPORTING p_dividend TYPE decfloat16 p_divisor TYPE decfloat16
RETURNING VALUE(p_result) TYPE decfloat16.
...
ENDCLASS.

Signature File //src/krn/runt/abkmeth.sig in the kernel ...
KERNEL_METHOD(CL_MY_CALCULATION, DIV, xx_myDiv,4)
ARGUMENT_F(1, 'P_DIVIDEND', TYPE, 'F', READ)
ARGUMENT_F(2, 'P_DIVISOR', TYPE, 'F', READ)
ARGUMENT_F(3, 'P_RESULT', TYPE, 'F', WRITE)
ARGUMENT_F(4, 'ME->LAST_RESULT',TYPE, 'F', WRITE)
EXCEPTION('CX_MY_DIV_BY_ZERO')
...

C++ source code //src/krn/.../mycalc.cpp in the kernel #include 'abkmeth.h'
...
externC void xx_myDiv( ARGUMENTS ){

const SAP_DOUBLE *const dividend = ARGUMENT_F_READ(1,'P_DIVIDEND');
const SAP_DOUBLE *const divisor = ARGUMENT_F_READ(2,'P_DIVISOR');
SAP_DOUBLE *result = ARGUMENT_F_WRITE(3,'P_RESULT');
SAP_DOUBLE *last_result = ARGUMENT_F_WRITE(4 ,'ME->LAST_RESULT');

if( 0 == *divisor )
{
EXCEPTION_CREATE(CX_MY_DIV_BY_ZERO);
EXCEPTION_RAISE();
}

*result = *dividend / *divisor;
*last_result = *result;

}

Implementation Section of the Class in ABAP CLASS cl_my_calculation IMPLEMENTATION.
...
METHOD div BY KERNEL MODULE xx_myDiv.
ENDMETHOD.
...
ENDCLASS.
Message text extract from SAP system. Copyright SAP SE.


Search for further information about these or an SAP related objects



Comments on this SAP object

What made you want to lookup this SAP object? Please tell us what you were looking for and anything you would like to be included on this page!