Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only
Stefan-Schnell
Active Contributor
1,508 Views
0 Comments
A longer time ago I wrote here how to simulate GetObject with ABAP. Here is now a variation to simulate CreateObject on the same way.

Another very interesting aspect of this possibility is the using of classes, which are not allowed under normal circumstances. The security options of the SAP logon disables the instantiation of some classes e.g. like SapROTWr.SapROTWrapper.1.

 



 

With this way you can use any class you want. This example shows how to use SAP GUI Scripting inside ABAP.

Hint: This is not a guide to open back doors or to bypass security rules. It will be only use available standards and their unusual combination.
"-Begin-----------------------------------------------------------------
"-
"- Example how to simulate CreateObject in ABAP via VBScript
"-
"- Author: Stefan Schnell
"- Site: www.stschnell.de
"-
"-----------------------------------------------------------------------
Report zCreateObject.

"-Type pools--------------------------------------------------------
Type-Pools OLE2.

"-Variables---------------------------------------------------------
Data SAPROTWrapper Type OLE2_OBJECT.
Data SapGuiAuto Type OLE2_OBJECT.
Data Application Type OLE2_OBJECT.
Data Connection Type OLE2_OBJECT.
Data Session Type OLE2_OBJECT.
Data MajorVersion Type String.
Data MinorVersion Type String.
Data Id Type String.

"-Main--------------------------------------------------------------
Create Object SAPROTWrapper 'SapROTWr.SapROTWrapper'.
If sy-subrc <> 0 Or SAPROTWrapper-HANDLE = 0 Or
SAPROTWrapper-TYPE <> 'OLE2'.

PerForm CreateObject Using 'SapROTWr.SapROTWrapper'
Changing SAPROTWrapper.
If SAPROTWrapper-HANDLE = 0 Or SAPROTWrapper-TYPE <> 'OLE2'.
Exit.
EndIf.

EndIf.

Call Method Of SAPROTWrapper 'GetROTEntry' = SapGuiAuto
Exporting #1 = 'SAPGUI'.
If sy-subrc <> 0 Or SapGuiAuto-HANDLE = 0 Or
SapGuiAuto-TYPE <> 'OLE2'.
Free Object SAPROTWrapper.
Exit.
EndIf.

Call Method Of SapGuiAuto 'GetScriptingEngine' = Application.
If sy-subrc <> 0 Or Application-HANDLE = 0 Or
Application-TYPE <> 'OLE2'.
Free Object SapGuiAuto.
Free Object SAPROTWrapper.
Exit.
EndIf.

Get Property Of Application 'MajorVersion' = MajorVersion.
Get Property Of Application 'MinorVersion' = MinorVersion.

Write: / 'SAP GUI Scripting Version ', MajorVersion, '.',
MinorVersion.

Get Property Of Application 'Children' = Connection
Exporting #1 = 1.
If sy-subrc <> 0 Or Connection-HANDLE = 0 Or
Connection-TYPE <> 'OLE2'.
Free Object Application.
Free Object SapGuiAuto.
Free Object SAPROTWrapper.
Exit.
EndIf.

Get Property Of Connection 'Children' = Session
Exporting #1 = 2.
If sy-subrc <> 0 Or Session-HANDLE = 0 Or
Session-TYPE <> 'OLE2'.
Free Object Connection.
Free Object Application.
Free Object SapGuiAuto.
Free Object SAPROTWrapper.
Exit.
EndIf.

Get Property Of Session 'Id' = Id.

Write: / 'ID ', Id.

Free Object Session.
Free Object Connection.
Free Object Application.
Free Object SapGuiAuto.
Free Object SAPROTWrapper.

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

"-SubRoutines begin-----------------------------------------------------

"-Form CreateObject---------------------------------------------------
Form CreateObject Using ClassName Type String
Changing Result Type OBJ_RECORD.

"-Constants-------------------------------------------------------
Constants CrLf(2) Type c Value %_CR_LF.

"-Variables-------------------------------------------------------
Data ScriptCtrl Type OBJ_RECORD.
Data Expression Type String Value ''.
Data Module Type String Value ''.

"-Macros----------------------------------------------------------
Define AddLine.
Concatenate Module &1 CrLf Into Module.
End-Of-Definition.

AddLine 'Function CreateObj(ClassName)'.
AddLIne ' Dim oObj'.
AddLine ' Set oObj = CreateObject(ClassName)'.
AddLine ' If IsObject(oObj) Then'.
AddLine ' Set CreateObj = oObj'.
AddLine ' End If'.
AddLine 'End Function'.

Concatenate 'CreateObj("' ClassName '")' Into Expression.
Create Object ScriptCtrl 'MSScriptControl.ScriptControl'.
If sy-subrc = 0 And ScriptCtrl-HANDLE <> 0 And
ScriptCtrl-TYPE = 'OLE2'.
Set Property Of ScriptCtrl 'AllowUI' = 1.
Set Property Of ScriptCtrl 'Language' = 'VBScript'.
Call Method Of ScriptCtrl 'AddCode' Exporting #1 = Module.
If sy-subrc = 0.
Call Method Of ScriptCtrl 'Eval' = Result
Exporting #1 = Expression.
EndIf.
EndIf.
Free Object ScriptCtrl.

EndForm.

"-SubRoutines end-------------------------------------------------------