"-Begin-----------------------------------------------------------------
"-
"-  Include           zDebug
"-
"-  Procedures to use external debug monitor
"-
"-  Author:  Stefan Schnell
"-  Version: 0.4
"-
"-----------------------------------------------------------------------

Include OLE2INCL.

"-Procedure Debug-------------------------------------------------------
"-
"-  Call it: Perform Debug Using 'This is a test'.
"-  Parameter: Output for external debugger as value or string
"-
"-----------------------------------------------------------------------
Form Debug Using p_Output Type Any.
  Data l_UserName Type String.
  l_UserName = sy-uname.
  Perform DebugUser Using p_Output l_UserName.
EndForm.

"-Procedure DebugUser---------------------------------------------------
"-
"-  Call it: Perform DebugUser Using 'This is a test' 'YI00159'.
"-  Parameter: Output for external debugger as value or string
"-  Parameter: Name of the user as string
"-
"-----------------------------------------------------------------------
Form DebugUser USING p_Output Type Any
                     p_UserName Type String.

  "-Get system information from client----------------------------------
  Data l_SysInf Type RFCSI.
  Call Function 'RFC_SYSTEM_INFO' Destination 'SAPGUI'
    Importing RFCSI_EXPORT = l_SysInf.
  Data l_OS Type String.
  l_OS = l_SysInf-RFCOPSYS(3).
  TRANSLATE l_OS TO UPPER CASE.

  IF l_OS = 'WIN'.
    "-Typecasting-------------------------------------------------------
    Data l_OutputDebugString Type String.
    l_OutputDebugString = p_Output.
    "-Delete leading and following spaces-------------------------------
    Shift l_OutputDebugString Left Deleting Leading Space.
    Shift l_OutputDebugString Right Deleting Trailing Space.
    "-If user name is initial, then set actual user name----------------
    Data l_UserName Type String.
    l_UserName = p_UserName.
    If l_UserName Is Initial.
      l_UserName = sy-uname.
    EndIf.
    "-If user name is equal to actual user name-------------------------
    If sy-uname = l_UserName.
      "-If OutputDebugString is not empty-------------------------------
      If l_OutputDebugString <> ''.
        "-If OutputDebugString is greater than 256 chars then cut it----
        If StrLen( l_OutputDebugString ) > 256.
          l_OutputDebugString = l_OutputDebugString(255).
        EndIf.
        "-Create DynamicWrapperX object---------------------------------
        Data l_Win32 Type Ole2_Object.
        Create Object l_Win32 'DynamicWrapperX'.
        "-Register OutputDebugString------------------------------------
        If sy-subrc = 0.
          Call Method Of l_Win32 'Register'
            Exporting
              #1 = 'kernel32.dll'
              #2 = 'OutputDebugStringA'
              #3 = 'i=s'.
          "-Output to external debugger---------------------------------
          If sy-subrc = 0.
            Call Method Of l_Win32 'OutputDebugStringA'
              Exporting
                #1 = l_OutputDebugString.
          EndIf.
          "-Free DynamicWrapperX object---------------------------------
          Free Object l_Win32.
        EndIf.
      EndIf.
    EndIf.
  EndIf.

EndForm.

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


"-Begin-----------------------------------------------------------------
"-
"-  Class           zDebug
"-
"-  Methods to use external debug monitor
"-
"-  Author:  Stefan Schnell
"-  Version: 0.4
"-
"-----------------------------------------------------------------------

"-Method Print----------------------------------------------------------
"-
"-  Call it: Call Method Debug->Print 
"-             Exporting i_Output = 'This is a test'.
"-  Parameter: Output for external debugger as value or string
"-
"-----------------------------------------------------------------------

  Method Print.
    Data l_UserName Type String.
    l_UserName = sy-uname.
    PrintUser( Exporting i_Output = i_Output i_UserName = l_UserName ).
  EndMethod.

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


"-Method PrintUser------------------------------------------------------
"-
"-  Call it: Call Method Debug->PrintUser 
"-             Exporting i_Output = 'This is a test'
"-                       i_UserName = 'YI00159'.
"-  Parameter: Output for external debugger as value or string
"-  Parameter: Name of the user as string
"-
"-----------------------------------------------------------------------

Method PrintUser.

  "-Get system information from client----------------------------------
  Data l_SysInf Type RFCSI.
  Call Function 'RFC_SYSTEM_INFO' Destination 'SAPGUI'
    Importing RFCSI_EXPORT = l_SysInf.
  Data l_OS Type String.
  l_OS = l_SysInf-RFCOPSYS(3).
  Translate l_OS To Upper Case.

  If l_OS = 'WIN'.
    Type-Pools OLE2.
    "-Typecasting-------------------------------------------------------
    Data l_OutputDebugString Type String.
    l_OutputDebugString = i_Output.
    "-Delete leading and following spaces-------------------------------
    Shift l_OutputDebugString Left Deleting Leading Space.
    Shift l_OutputDebugString Right Deleting Trailing Space.
    "-If user name is initial, then set actual user name----------------
    Data l_UserName Type String.
    l_UserName = i_UserName.
    If l_UserName Is Initial.
      l_UserName = sy-uname.
    EndIf.
    "-If user name is equal to actual user name-------------------------
    If sy-uname = l_UserName.
      "-If OutputDebugString is not empty-------------------------------
      If l_OutputDebugString <> ''.
        "-If OutputDebugString is greater than 256 chars then cut it----
        If StrLen( l_OutputDebugString ) > 256.
          l_OutputDebugString = l_OutputDebugString(255).
        EndIf.
        "-Create DynamicWrapperX object---------------------------------
        Data l_Win32 Type Ole2_Object.
        Create Object l_Win32 'DynamicWrapperX'.
        "-Register OutputDebugString------------------------------------
        If sy-subrc = 0.
          Call Method Of l_Win32 'Register'
            Exporting
              #1 = 'kernel32.dll'
              #2 = 'OutputDebugStringA'
              #3 = 'i=s'.
          "-Output to external debugger---------------------------------
          If sy-subrc = 0.
            Call Method Of l_Win32 'OutputDebugStringA'
              Exporting
                #1 = l_OutputDebugString.
          EndIf.
          "-Free DynamicWrapperX object---------------------------------
          Free Object l_Win32.
        EndIf.
      EndIf.
    EndIf.
  EndIf.

EndMethod.

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


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

Report  zDebugTest.

Break-Point.

"-Prozeduraler Test-----------------------------------------------------
Perform Debug Using 'Hallo zusammen!'.

"-Objektorientierter Test-----------------------------------------------
Types tDebug Type Ref To zDebug.
Data Debug Type tDebug.
Create Object Debug.

Call Method Debug->Print Exporting i_Output = 'Hallo zusammen 2!'.

Include zDebug.

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