Jump to content
Sign in to follow this  
StSchnell

Multi Threading with AutoIt and SAP RFC Library

Recommended Posts

StSchnell

Hello community,

here is an example code of a server application for an SAP system in AutoIt language.

My problem is, that this AutoIt example crashs after the first function call of ABAPCall from an ABAP report.

I try two methods:

  • Use DllCallbackGetPtr
  • Use CreateThread and get the address of the thread.
But both methods crashs at the same code position, look at the example below.

Is there any other way to use this kind of multithreading?

Thanks for hints.

Cheers

Stefan

;-Begin-----------------------------------------------------------------

  ;-Directives----------------------------------------------------------
    AutoItSetOption("MustDeclareVars", 1)

  ;-Constants-----------------------------------------------------------
    Const $RFC_OK = 0
    Const $RFC_RETRY = 14

  ;-Variables-----------------------------------------------------------
    Dim $SAP, $ABAPCall, $ptrABAPCall, $hDesc, $conPar[3]
    Dim $ptrConPar[3], $ConParPtr, $i, $rc, $hCon
;   Dim $hThread

  ;-Function CreateThread-----------------------------------------------
    Func CreateThread($Handle)
      Local $Ret
      $Ret = DllCall("kernel32.dll", "handle", "CreateThread", "ptr", 0, _
        "dword", 0, "long", DllCallbackGetPtr($Handle), "ptr", 0, _
        "long", 0, "int*", 0)
      Return $Ret[0]
    EndFunc

  ;-Function GetThreadAddr----------------------------------------------
    Func GetThreadAddr($hThread)
      Local $ThreadAddr = DllStructCreate("ptr")
      DllCall("NTDLL.dll", "long", "NtQueryInformationThread", _
        "handle", $hThread, "long", 9, "ptr", _
      DllStructGetPtr($ThreadAddr), "ulong", 4, "ulong*", 0)
      Return Number(DllStructGetData($ThreadAddr, 1))
    EndFunc

  ;-Function OutputDebugString------------------------------------------
    Func OutputDebugString($OutputString)
      Local $strOut = String($OutputString)
      DllCall("kernel32.dll", "none", "OutputDebugStringW", "wstr", _
        $strOut)
    EndFunc

  ;-Function ABAPCall---------------------------------------------------
    Func ABAPCall()
      MsgBox(0, "", "Ja")
      Return $RFC_OK

      ;-Important hint--------------------------------------------------
      ;-
      ;- Program crashs here, because AutoIt is not able to use multi
      ;- threading with SAP NetWeaver RFC library
      ;-
      ;-----------------------------------------------------------------

    EndFunc

  ;-Main----------------------------------------------------------------
    $SAP = ObjCreate("COMNWRFC")
    If IsObj($SAP) Then

      $ABAPCall = DllCallbackRegister("ABAPCall", "long", "")
      $hDesc = $SAP.RfcCreateFunctionDesc("ABAPCall")
      If $hDesc And $ABAPCall Then

        ;-Define RFC_CONNECTION_PARAMETER structures--------------------
          For $i = 0 To 2
            $conPar[$i] = DllStructCreate("wchar name[16];wchar value[16]")
          Next

        ;-Set RFC_CONNECTION_PARAMETER----------------------------------
          DllStructSetData($conPar[0], "name", "program_id")
         DllStructSetData($conPar[0], "value", "AUTOITSERVER")
         DllStructSetData($conPar[1], "name", "gwhost")
         DllStructSetData($conPar[1], "value", "ABAP")
         DllStructSetData($conPar[2], "name", "gwserv")
         DllStructSetData($conPar[2], "value", "sapgw00")

    ;-Switch RFC_CONNECTION_PARAMETER strings to pointers-----------
      $ptrConPar = DllStructCreate("ptr;ptr;ptr;ptr;ptr;ptr")
      DllStructSetData($ptrConPar, 1, DllStructGetPtr($conPar[0], "name"))
      DllStructSetData($ptrConPar, 2, DllStructGetPtr($conPar[0], "value"))
      DllStructSetData($ptrConPar, 3, DllStructGetPtr($conPar[1], "name"))
      DllStructSetData($ptrConPar, 4, DllStructGetPtr($conPar[1], "value"))
      DllStructSetData($ptrConPar, 5, DllStructGetPtr($conPar[2], "name"))
      DllStructSetData($ptrConPar, 6, DllStructGetPtr($conPar[2], "value"))

      $ptrABAPCall = Number(DllCallbackGetPtr($ABAPCall))

;-Alternative-----------------------------------------------------------
;
;      $hThread = CreateThread($ABAPCall)
;      If $hThread Then
;        $ptrABAPCall = GetThreadAddr($hThread)
;        If $ptrABAPCall Then
;
;-----------------------------------------------------------------------

      $rc = $SAP.RfcInstallServerFunction("", $hDesc, $ptrABAPCall)
      If $rc = $RFC_OK Then

        $ConParPtr = Number(DllStructGetPtr($ptrConPar))
        $hCon = $SAP.RfcRegisterServer($ConParPtr, 3)
        If $hCon Then

          While $rc = $RFC_OK Or $rc = $RFC_RETRY

            $rc = $SAP.RfcListenAndDispatch($hCon, 1)
           Select
             Case $rc = $RFC_OK
               OutputDebugString("RFC_OK")
             Case $rc = $RFC_RETRY
               OutputDebugString("RFC_RETRY")
           EndSelect
           Sleep(256)

          Wend

          EndIf
        EndIf

        $SAP.RfcDestroyFunctionDesc($hDesc)
        DllCallbackFree($ABAPCall)

;          DllCall("kernel32.dll", "boolean", "CloseHandle", _
;            "handle", $hThread)
;        EndIf
;      EndIf

      EndIf
      $SAP = 0
    EndIf

;-End-------------------------------------------------------------------
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • PauloRodrigues
      By PauloRodrigues
      I need to select an item from a SAP combobox, could anyone help me or did something like that? I tried to use some AutoIt functions but without success.
      When i try to identify the combobox with au3Info, this is the return:


      Could anyone help me with this challange?
    • wakillon
      By wakillon
      Mp3SearchEngine v2.0.0.6

      May be some of you know Songr .
      This script do the same job, it can find more mp3 files but is not as fast as Songr.
       
       



      Sites used are music search engine Websites designed for LEGAL entertainment purposes only.
      Thanks to Brett Francis, Prog@ndy and Eukalyptus for >Bass Udf, trancex for >WinHttp Udf and the AutoIt Community for his help.

       
      Changes of v1.0.8.5
       
      Three websites replaced cause they are dead or use now js.
      All search engines updated ( not without difficulties for audiodump)
      I use RAGrid.dll for the first listview (more fast and stable, but with some inconvenients to manage the no-edit of cells)
      Input queries are saved ( the twenty latest)
      I use now an mp3 pre-Load management before playing and a double progressbar for visualize pre-load and play, where you can click for directly go play in the loaded part.
      Most includes needed are embedded and all external files are embedded in script with >BinaryToAu3Kompressor .
      Multi downloads available with embedded downloader.exe
       
      Changes of v1.0.8.8
      Search on audiodump and myfreemp3 fixed.
      New buttons.
      Added Gui Menu.
      Titles are no more editable.
      New "About" with >TaskDialog (Thanks Prog@andy)
      Query button permit now to check / uncheck all checkboxes
      And some few fixes and cleaning.
      Really more stable now.
      Changes of v1.0.9.2
      Dilandau is replaced by mp3chief and mp3ili by mp3clan 
      Search on mp3juices, baseofmp3 and soundcloud fixed.
      Soso now provide m4a (aac) instead of mp3 ( m4a can be played by MSE)
      Added possibility to encode automaticaly to mp3, aac or ogg ( at the end of download) using bassenc.dll and command line tools : lame, faac and oggenc.
       
      Changes of v1.0.9.3   mp3skull fixed mp3chief fixed myfreemp3 fixed mp3clan changed to tusmp3  mp3juices changed to emp3world baseofmp3 changed to imp3 and some minor improvements.  
      Version 2.0.0.6
      Most previous websites used are dead or have changed the way to get links, 
      so instead of try to repair the previous version, i have created a complete new version.
      The main tendency is the simplification :
      Only one website : audiodump (Up to 500 results by request)
      Script use now the little pearl created by Ward : curl.au3
      It permit to create tasks (get source and get multi mp3) in asynchronous mode.
      So now, no need to use several executables and no more gui who do not respond in case of connection problems. 
      Script use Bass.dll X86 loaded in memory for play songs.
      Result is light and fast, but don't abuse of audiodump servers who are not beasts of race.
      Warning : For avoid errors with curl.au3, you'll need to comment the line 63 : ;~ #Include <BinaryCall.au3>
      @AutoItX64 not supported and only tested on Win7X64 and Win8.1X64.
      As your browser, use Ctrl+w for remove the current Tab.(if there is no search or download running from it)
      And also Ctrl+q for set/remove Gridlines.
      Events are displayed to the bottom of the Gui.
       
      Version 2.0.1.1
      Added a Paste Button.
      Querry list is now correctly saved.
      Querry Combo is now sorted in alphabetical order
      After a 'No match', the next search will use the previous empty listview.
      Bug when removing tabs is corrected.
      Added string correction for the request that, in the previous version, was not always able to return a correct result.
       
      A big thanks to Ward for his great UDF, and Nina my favorite tester, (who between us is also my third daughter), for his precious advices .
      previous downloads : 1703
       
      As there is no more script downloads count, source and executable are available in the downloads section

      Enjoy ! 
      July 2017 Project Discontinued due to website changes
    • Clemens
      By Clemens
      Hi there,
      I have a small hopefully quick fixable issue with reading information from my ListView:
      So in fact I just want to have information about which items are selected, so I'm using
      msgbox(0 , "return", GUICtrlRead($myListView), 1)
      but unfortunately it only returns me either the first item id or if this is not selected the second item id, or if this is not selected the third, etc. or 0 if none is selected.
      Anybody has an idea how to get the full picture of my >>multiple Items selected<< ListView?
      Thanks in advance!
      Clemens
    • ruslanas402
      By ruslanas402
      Hello there, could anyone advanced in WinHTTP tell me what am I missing please?
      #include "WinHttp.au3" #include <Array.au3> $sPic = "C:\Users\Source\Pictures\Capturex1.PNG" $sPic2 = "C:\Users\Source\Pictures\Capturex2.PNG" $hOpen = _WinHttpOpen() $hConnect = _WinHttpConnect($hOpen, "https://m.facebook.com/") $sRead = _WinHttpSimpleFormFill($hConnect, "login.php", "login_form", "name:email", "login@mail.com", "name:pass", "pasword") $aRead = _WinHttpSimpleFormFill($hConnect, "/groups/1111111111111", "index:1", "name:view_photo", True, "[RETURN_ARRAY]") ;<-- 11111... <-- Group ID $aURL = _WinHttpCrackUrl($aRead[2]) $aRead = _WinHttpSimpleFormFill($hConnect, $aURL[6] & $aURL[7], Default, "name:file1", $sPic, "[RETURN_ARRAY]") $aURL = _WinHttpCrackUrl($aRead[2]) $aRead = _WinHttpSimpleFormFill($hConnect, $aURL[6] & $aURL[7], "index:0", "name:view_photo", True, "[RETURN_ARRAY]") ;<-- Suppose to press on Add More photos isn't? $aURL = _WinHttpCrackUrl($aRead[2]) $aRead = _WinHttpSimpleFormFill($hConnect, $aURL[6] & $aURL[7], Default, "name:file1", $sPic2, "[RETURN_ARRAY]") ; <-- also submit second photo? $aURL = _WinHttpCrackUrl($aRead[2]) _WinHttpSimpleFormFill($hConnect, $aURL[6] & $aURL[7], Default, "name:view_post", True) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) I'm trying to post multi photos in group, but no luck. Here is idea I came from:
       
    • wakillon
      By wakillon
      I want associate a multi line tip text to each tab items using GUICtrlSetTip.
      I have recreated a simple example :
      #include <GUIConstantsEx.au3> GUICreate("My GUI Tab", 250, 150); will create a dialog box that when displayed is centered GUISetBkColor(0x00E0FFFF) GUISetFont(9, 300) $sTipTxt1 = 'Line 1 : Paul' & @CR & _ 'Line 2 : Jo' & @CR & _ 'Line 3 : Al' & @CR & _ 'Line 4 : Steve' $sTipTxt2 = 'Line 1 : Marc' & @CR & _ 'Line 6 : Mickael' & @CR & _ 'Line 7 : Franck' Local $idTab = GUICtrlCreateTab(10, 10, 200, 100) $idTabItem0 = GUICtrlCreateTabItem("tab0") $idTabItem1 = GUICtrlCreateTabItem("tab----1") $idTabItem2 = GUICtrlCreateTabItem("tab2") GUICtrlSetState(-1, $GUI_SHOW); will be display first GUICtrlCreateTabItem(""); end tabitem definition GUICtrlCreateLabel("Label With Tip", 20, 130, 250, 20) GUISetState(@SW_SHOW) GUICtrlSetTip ( -1, $sTipTxt1, ' ', 1, 1 ) GUICtrlSetTip ( $idTabItem0, $sTipTxt2, ' ', 1, 1 ) GUICtrlSetTip ( $idTabItem1, $sTipTxt1, ' ', 1, 1 ) GUICtrlSetTip ( $idTabItem2, $sTipTxt2, ' ', 1, 1 ) Local $idMsg ; Loop until the user exits. While 1 $idMsg = GUIGetMsg() If $idMsg = $GUI_EVENT_CLOSE Then ExitLoop If $idMsg = $idTab Then ; display the clicked tab WinSetTitle("My GUI Tab", "", "My GUI Tab" & GUICtrlRead($idTab)) EndIf WEnd Nothing found about that in help file or wiki/Tabs.
      I have only seen that GUICtrlSetTip for tabitems sets incorrectly in certain situations
      but i also seen it was fixed for the 3.3.10.0 AutoIt version.
      So... may be not in case of multi lines ? 
       
×