Skip to Content

How to call DLL functions from the client in ABAP programs

Hello community,

it is not ordinary but very easy to use DLL calls in ABAP language. Yuri Popov programmed a module, called DynamicWrapperX. It is an ActiveX component, which allow to call each function in a DLL you want via COM. A great module, you find the module [here|http://www.script-coding.info/dynwrapx_eng.html].

It is necessary before, with the transaction code SOLE, register the OLE application DynamicWrapperX in the SAP system. Type in:

u2022OLE Application: DynamicWrapperX

u2022CLSID: {89565275-A714-4a43-912E-978B935EDCCC}

u2022CLSID Type Lib: {89565275-A714-4a43-912E-978B935EDCCC}

u2022OLE-Objectname: DynamicWrapperX

u2022Language: EN

u2022Text: Component allows to call functions exported by DLLs

And it is necessary to register DynamicWrapperX on the client system too, with regsvr32.exe dynwrapx.dll.

Look at the following example how easy it is to use a Windows function from USER32.DLL:

  INCLUDE OLE2INCL.

  "-Constants------------------------------
    DATA IDYes TYPE i VALUE 6.
    DATA IDNo TYPE i VALUE 7.

  "-Variables------------------------------
    DATA Win32 TYPE OLE2_OBJECT.
    DATA ret TYPE i.

  CREATE OBJECT Win32 'DynamicWrapperX'.

  CALL METHOD OF Win32 'Register' EXPORTING
    #1 = 'user32.dll' #2 = 'MessageBoxW'
    #3 = 'i=hwwu' #4 = 'r=l'.

  CALL METHOD OF Win32 'MessageBoxW' = ret
    EXPORTING #1 = 0 #2 = 'Hello World'
    #3 = 'Test' #4 = 4.

  IF ret = IDYes.
    WRITE 'Ja'.
  ELSEIF ret = IDNo.
    WRITE 'Nein'.
  ELSE.
    WRITE '?'.
  ENDIF.

  FREE OBJECT Win32.

Important hint: This method works only with foreground jobs and online processes and not with background jobs and batch processes.

Enjoy the possibilities.

Cheers

Stefan

Edited by: Stefan Schnell on Oct 25, 2010 5:02 PM

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Oct 25, 2010 at 03:15 PM

    Thanks for sharing, please create an entry in the ABAP section of the SDN wiki with this information.

    Thomas

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 28, 2012 at 12:12 PM

    Thanks a lot!

    Can you show example, when METHOD has to return the value.

    For example: I need to use some  AddInISC.dll.

    it works whith VB in our project like this:

    Set DX = CreateObject("DynamicWrapperX")

    DX.Register  "AddInISC.dll",  "CalcRic"

    _value = DX.CalcRic( "MANYSYMBOLS", 0)

    now _value has data.

    How do use this example in ABAP?

    I try to use as in your example, but ... I don't understand where I do mistake.

    CREATE OBJECT Win32 'DynamicWrapperX'.

    CALL METHOD OF Win32 'Register'
    EXPORTING
       #1 = 'C:\WINDOWS\system\AddInISC.dll' #2 = 'CalcRic'.

    CALL METHOD OF Win32 'CalcRic'
    EXPORTING
       #1 = 'MANYSYMBOLS' #2 = 0.

    GET PROPERTY OF Win32 'CalcRic' = cik.


    WRITE: cik. " nothing here.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Stefan Schnell

      well......

      Thank you very much for your attention and help.

      However it doesn't work, despite that was made as ActivX kind. Perhaps, author of that "dll" made it

      not correctly.

      I leave it for later temporary.

      Cheers

      Sergei

  • Dec 26, 2013 at 03:05 PM

    Hi Stefan,

    Greetings!!

    need your help for one of my requirement, mentioned below.Any help from you would be  higly appriciated.

    We have got requirement to call DLL functions from SAP for POS device(Point of Sale i.e Card swipe machine during Payment).

    Here POS device has certain functions like api_CheckExistance, api_TriggerTrxTrack2 and api_GetTransactionResult to use POS. we tried to implement the same methods by using above approach that you mentioned, but could not success. Could you please let us know, how to handle POS device functions, especially importing parameter?

    Example : Below function need to call in SAP

    Function Name:

    api_GetTerminalParams

    Function Usage:

    Can be used to read the Terminal Identification Number and Retailer Identification Number from the terminal’s Non- volatile configuration memory for the host application’s internal purposes.

    One common use of it is to stamp the transaction with the terminal ID that processed the transaction.

    Input Parameter:

    N/A

    Return Parameter:

    The function requires two parameters, which will be filled with appropriate data.

    1. 1. szTerminalID: This parameter should be at least 9 bytes long as the terminal ID size is always eight digits and the library appends a null character at the end for “C” specific null termination.
    2. 2. szRetailerID: should be at least 17 bytes long as the retailer ID could be a maximum of 16 characters, if the retailer ID is smaller than 16 characters, spaces are appended at the end to retain the 16 byte boundary.

    Function Declaration in C++:

    extern "C" int FAR PASCAL EXPORT api_GetTerminalParams(char FAR *szTerminalID, char FAR *szRetailerID);

    An example C++ code is present in the C++ project source supplied along with this document

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Harjyot,

      I expand the Pointer for COM (P4COM) library. Now it is also possible to call functions in a dynamic link library - not so comfortable but it works.

      Here an example:

      "-Begin-----------------------------------------------------

        Program zPointOfSale.

          "-Type Pools--------------------------------------------

            Type-Pools OLE2.

          "-Variables---------------------------------------------

            Data Ptr Type OLE2_OBJECT.

            Data TerminalID Type String Value ''.

            Data ptrTerminalID Type i Value 0.

            Data RetailerID Type String Value ''.

            Data ptrRetailerID Type i Value 0.

            Data hDLL Type i Value 0.

            Data Vars Type i Value 0.

            Data ret Type i Value 0.

          "-Main--------------------------------------------------

            Create Object Ptr 'P4Com'.

            If sy-subrc = 0 And Ptr-Handle <> 0 And

              Ptr-Type = 'OLE2'.

              "-Create variables in memory------------------------

                Call Method Of Ptr 'CreateVar' = ptrTerminalID

                  Exporting #1 = 'TerminalID' #2 = 'String'

                    #3 = 9.

                Call Method Of Ptr 'CreateVar' = ptrRetailerID

                  Exporting #1 = 'RetailerID' #2 = 'String'

                    #3 = 17.

              "-Open the library----------------------------------

                Call Method Of Ptr 'OpenLibrary' = hDLL

                  Exporting #1 = 'C:\Dummy\POS.dll'.

              If hDLL <> 0 And ptrTerminalID <> 0 And

                ptrRetailerID <> 0.

                "-Create stack for function call------------------

                  Call Method Of Ptr 'CreateVar' = Vars

                    Exporting #1 = 'Vars' #2 = 'Array' #3 = 8.

                If Vars <> 0.

                  "-Push variables on the stack-------------------

                  "

                  " Hint: It is in reverse order, because it is

                  " Pascal calling convention

                  "

                  "-----------------------------------------------

                    Call Method Of Ptr 'SetArrayLong'

                      Exporting #1 = 'Vars' #2 = 0

                        #3 = ptrRetailerID.

                    Call Method Of Ptr 'SetArrayLong'

                      Exporting #1 = 'Vars' #2 = 4

                        #3 = ptrTerminalID.

                  "-Call the function in the library--------------

                    Call Method Of Ptr 'CallFunction' = ret

                      Exporting #1 = hDLL

                        #2 = 'API_GETTERMINALPARAMS'

                        #3 = 2 #4 = Vars.

                  "-Get result------------------------------------

                    Call Method Of Ptr 'GetStringVar' = TerminalID

                      Exporting #1 = 'TerminalID'.

                    Call Method Of Ptr 'GetStringVar' = RetailerID

                      Exporting #1 = 'RetailerID'.

                  "-Destroy stack---------------------------------

                    Call Method Of Ptr 'DestroyVar' = ret

                      Exporting #1 = 'Vars'.

                EndIf.

                "-Destroy variables-------------------------------

                  Call Method Of Ptr 'DestroyVar' = ret

                    Exporting #1 = 'RetailerID'.

                  Call Method Of Ptr 'DestroyVar' = ret

                    Exporting #1 = 'TerminalID'.

                "-Close the library-------------------------------

                  Call Method Of Ptr 'CloseLibrary' = ret

                    Exporting #1 = hDLL.

              EndIf.

              Write: / TerminalID.

              Write: / RetailerID.

              Free Object Ptr.

            EndIf.

      "-End-------------------------------------------------------

      Cheers

      Stefan

  • Jan 23, 2014 at 06:59 PM

    Hi Stefan,

    Please check the link How to call a dll file of windows using SAP ABAP

    Regards

    Marcelo Macedo

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Stefan Schnell

      Dear Stefan

      I finially got the right result in VBS and ABAP, it's because of the dll file which should be complied in Release but not in Debug. Really appraciate your help. Thank you, thank you very much🤣

      Best regards

      James