cancel
Showing results for 
Search instead for 
Did you mean: 

Write New Entry in Windows Registry

stefan_schnell
Active Contributor
0 Kudos

Hello community,

the following routine delivers always 1. It is no matter if the entry is written to the Windows registry or not, rc is always 1.

"-Begin-----------------------------------------------------------------
  Program ZTEST.

    "-Variables---------------------------------------------------------
      Data rc Type i.

    PerForm CreateKey Using 'Software\Classes\Test' '' '' Changing rc.

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

"-Subroutines begin-----------------------------------------------------

  "-Function CreateKey--------------------------------------------------
    Form CreateKey Using SubKey Type String ValueName Type String
      Value Type String Changing rc Type i.

      Call Method cl_gui_frontend_services=>registry_set_value
        Exporting
          root = cl_gui_frontend_services=>hkey_current_user
          key = SubKey
          value_name = ValueName
          value = Value
        Importing
          rc = rc
        Exceptions
          registry_error = 1

          cntl_error
= 2
          error_no_gui = 3
          not_supported_by_gui = 4
          Others = 5.

      Call Method cl_gui_cfw=>flush.

    EndForm.

"-Subroutines end-------------------------------------------------------

Is it correct?

Thanks for hints and tips.

Cheers

Stefan

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

It is working for me. Your windows user account could have insufficient permissions at OS level.

stefan_schnell
Active Contributor
0 Kudos

Hello Manish,

thanks for your reply. I tried it with different user accounts with different rights but the result is the same.

Here is my solution to solve the problem:

"-Begin-----------------------------------------------------------------
  Report ZTEST.

    "-Includes----------------------------------------------------------
      Include OLE2INCL.

    "-Variables---------------------------------------------------------
      Data rc Type i Value -1.

">Stop<
Break-Point.

    PerForm CreateKey1 Using 'Software' '' '' Changing rc.
    PerForm CreateKey1 Using 'Software' 'Test' 'Test' Changing rc.

    PerForm CreateKey2 Using 'Software' '' '' Changing rc.
    PerForm CreateKey2 Using 'Software' 'Test' 'Test' Changing rc.

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

"-Subroutines begin-----------------------------------------------------

  "-Function CreateKey1-------------------------------------------------
    Form CreateKey1 Using SubKey Type String ValueName Type String
      Value Type String Changing rc Type i.

      "-Variables-------------------------------------------------------
        Data ret Type i Value 0.
        Data rValue Type String Value ''.

      Call Method cl_gui_frontend_services=>registry_set_value
        Exporting
          root = cl_gui_frontend_services=>hkey_current_user
          key = SubKey
          value_name = ValueName
          value = Value
        Importing
          rc = ret
        Exceptions
          registry_error = 1
          cntl_error = 2
          error_no_gui = 3
          not_supported_by_gui = 4
          Others = 5.

      Call Method cl_gui_cfw=>flush.

      Call Method cl_gui_frontend_services=>registry_get_value
        Exporting
          root = cl_gui_frontend_services=>hkey_current_user
          key = SubKey
          value = Value
        Importing
          reg_value = rValue
        Exceptions
          get_regvalue_failed = 1
          cntl_error = 2
          error_no_gui = 3
          not_supported_by_gui = 4
          Others = 5.

      Call Method cl_gui_cfw=>flush.

      If rValue = Value.
        rc = 0.
      Else.
        rc = 1.
      EndIf.

    EndForm.

  "-Class BASIC---------------------------------------------------------
    Class cBASIC Definition.
      Public Section.
        Methods Right Importing Str Type String Length Type i
          Exporting Result Type String.
    EndClass.

    Class cBASIC Implementation.

      "-Method Right----------------------------------------------------
        Method Right.

          "-Variables---------------------------------------------------
            Data pos Type i Value 0.

          pos = strlen( Str ) - Length.
          Move Str+pos(Length) To Result.

        EndMethod.

    EndClass.

  "-Function CreateKey2-------------------------------------------------
    Form CreateKey2 Using SubKey Type String ValueName Type String
      Value Type Any Changing rc Type i.

      "-Variables-------------------------------------------------------
        Data BASIC Type Ref To cBASIC.
        Data WShell Type OLE2_OBJECT.
        Data Key Type String Value ''.
        Data rValue Type String Value ''.
        Data strTmp Type String Value ''.

      Create Object WShell 'WScript.Shell'.
      If sy-subrc = 0 And WShell-Handle <> 0 And WShell-Type = 'OLE2'.

        Create Object BASIC.
        Call Method BASIC->Right Exporting Str = SubKey Length = 1
          Importing Result = strTmp.
        If strTmp <> '.
          Concatenate 'HKCU SubKey ' ValueName Into Key.
        Else.
          Concatenate 'HKCU SubKey ValueName Into Key.
        EndIf.

        Call Method Of WShell 'RegWrite' Exporting #1 = Key #2 = Value.
        Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.

        Call Method Of WShell 'RegRead' = rValue Exporting #1 = Key.
        Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.

        If rValue = Value.
          rc = 0.
        Else.
          rc = 1.
        EndIf.

        Free Object WShell.

      EndIf.

    EndForm.

"-Subroutines end-------------------------------------------------------

I use two different ways. The first is with the standard methods - from the class with the ProgID Sapgui.InfoCtrl.1 on SAP GUI for Windows. And the second is via Windows Scripting Host and the methods RegWrite resp. RegRead. Both work as expected.

Cheers

Stefan