Technical Articles
How to call an ActiveX object from Abap
Hi!
Last week I had a task to call an ActiveX object.
Our client has a special desktop application is accessible via ActiveX technology. And one of sap-user’s operation requires to call it from abap program.
Ok, it’s not a magic. But I faced with a couple of non-fully clear technical things.
I’ll describe this to save your one or two working hours.
There is a standard demo program SAPRDEMO_ACTIVEX_INTEGRATION. It works fine but it hasn’t any comments and contains some source code that may be is not necessarily to you.
You can easily google a fragment of this program, slightly adapted in stackoverflow.com . This is more compact, but also without comments.
This demo program is a good start. But I spent some time to configure this code to call my ActiveX object.
First, we need to know the name your ActiveX object. It may be something like ‘Excel.Application’. Usually ActiveX object vendor provides this to you.
You may not know the name but know the CLSID of the ActiveX object. CLSID is a GUID (32-digit number) that identifies the ActiveX object in operating system.
You can search CLSID in Windows registry utility. Pay attention your need to user the name from ‘ProgId’ registry node.
The second thing your need to know are the ActiveX object method name and the input parameters. Note that the sequence of parameters is important.
Well, thus we have all we need, let look at the program template and adapt it to your specific ActiveX object call.
REPORT zca_114_activex_demo.
* The ActiveX processing demo program by Anton Sorokin
* email: a40.sorokin@gmail.com
* Fill free to contact me to discuss this demo and so on
* WARNING !!!
* This program will not work on your system.
* Becouse you don't have the 'Styx.Crypto.AX.ASClientControl' ActiveX object installed
* which is used in the code
*
* This is just a template!
* You have to adapt it to your requirements, see the comments bellow.
DATA: control TYPE REF TO i_oi_container_control,
oxc_document TYPE REF TO i_oi_document_proxy,
activex_is_enabled TYPE flag,
retcode TYPE soi_ret_string,
doc_handle TYPE cntl_handle.
CONSTANTS gc_retcode_ok TYPE soi_ret_string VALUE 'OK'.
CONSTANTS
" ActiveX object name. You have to know it.
" Or you can find out the ActiveX object name via CLSID (GUID) and windows registry
gc_activex_name TYPE soi_document_type VALUE 'Styx.Crypto.AX.ASClientControl'.
CONSTANTS
" ActiveX object method name. You should know it.
" Regestry doesn't matter. 'HashKey' is equal 'HASHKEY' or 'hasskey'
gc_method_name TYPE string VALUE 'HashKey'. " It's my method name. You have to change it by yours.
START-OF-SELECTION.
" Step 1
" Test whether activeX is supported"
CALL FUNCTION 'GUI_HAS_ACTIVEX'
IMPORTING
return = activex_is_enabled.
" Windows Security Policy may disable the use of ActiveX.
" In this case, you have no way to run activex object.
CHECK NOT activex_is_enabled IS INITIAL.
" Step 2
" Some preparatory actions. Same for any ActiveX
CALL METHOD c_oi_container_control_creator=>get_container_control
IMPORTING
control = control
retcode = retcode.
" The minimal error handling. You may do better :)
IF retcode NE gc_retcode_ok. " comparing with 'OK' literal
MESSAGE retcode TYPE 'E'.
RETURN.
ENDIF.
" In the following cases, we will skip this to make the demo as short as possible
CALL METHOD control->init_control
EXPORTING
r3_application_name = 'R/3 Basis' " doesn't matter what 'name' is present
inplace_enabled = 'X'
register_on_close_event = 'X'
register_on_custom_event = 'X'
parent = cl_gui_container=>default_screen
IMPORTING
retcode = retcode.
" Step 3
" Getting Activex object handle.
" In this code the ActiveX name (const gc_activex_name) is specific
" Others lines still remain for any ActiveX object.
CALL METHOD control->get_document_proxy
EXPORTING
document_type = gc_activex_name " put your ActiveX name here
IMPORTING
document_proxy = oxc_document
retcode = retcode.
CALL METHOD oxc_document->open_activex_document
IMPORTING
retcode = retcode.
CALL METHOD oxc_document->get_document_handle
IMPORTING
handle = doc_handle
retcode = retcode.
" Step 4
" Now we can call ActiveX object methods.
" Yout should be know the method name and input parameter count.
" Method always has one return paramenter of the type 'any'
DATA gv_return TYPE string. " value returned from activex method
" Use the appropriate type of the 'return' parameter.
" In my case the method returns string data.
" The method of your ActiveX object's can return data of some other type.
CALL FUNCTION 'CONTROL_CALL_METHOD'
EXPORTING
h_control = doc_handle
method = gc_method_name " change constant 'gc_method_name' to your method name
p_count = 2 " count of method inpit parameters.
" In my case the method 'HashKey' has 2 input parameters
" so I use p1 and p2 FM parameter
" Your method can requers from 0 up to 16 input parameters
" (16 - is a 'CONTROL_CALL_METHOD' FM limitation)
p1 = '1234567890'
p2 = 'Anton Sorokin welcomes you!'
IMPORTING
return = gv_return
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2
OTHERS = 3.
IF sy-subrc IS NOT INITIAL.
" Some error handling
ENDIF.
" Step 5
" The end of the ActiveX processing.
" Abap and operating system resources will no longer be used.
" I mean resources for activex calls.
" This call is the same for any activex handle
CALL METHOD oxc_document->close_activex_document
IMPORTING
retcode = retcode.
Hope it was helpful for you!
Feel free to contact me to discuss this subject!
Bye!