Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Stefan-Schnell
Active Contributor
2,362
Since a few days I try the possibilities of Microsoft .NET Core development platform. Therefore I downloaded the current release 3.1 of the .NET Core SDK and runtime. In this context I read a very interesting post about exposing .NET Core components to COM. The SAP GUI for Windows offers with its OLE interface the possibility to use COM libraries. I've already described this in a post with Python programming language. And now I was very interested to see if and how it works with .NET Core.

I used the command line interface (CLI) of dotnet only, no IDE.

  1. I created a new folder and in that folder I ran the following command:
    dotnet new classlib --framework netcoreapp3.1


  2. According to the description in the post, I made the additions in the source code, with a few changes: The ComInterfaceType must be Dual for late binding and I added a ProgId.
    //-Begin----------------------------------------------------------------

    using System;
    using System.Runtime.InteropServices;

    namespace Test {

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("fe103d6e-e71b-414c-80bf-982f18f6c1c7")]
    public interface ITest {

    string retHello();

    }

    [ComVisible(true)]
    [Guid("9f35b6f5-2c05-4e7f-93aa-ee087f6e7ab6")]
    [ProgId("Test.Class1")]
    public class Class1 : ITest {

    public string retHello() {
    return "Hello World";
    }

    }
    }

    //-End------------------------------------------------------------------


  3. In the csproj file I added the EnableComHosting tag.

    <Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <EnableComHosting>true</EnableComHosting>
    </PropertyGroup>

    </Project>


  4. Now I builded the library with the CLI
    Important is here the runtime argument, it must be win-x86, this is the architecture of the current SAP GUI for Windows.

    dotnet build --configuration release --runtime win-x86


  5. Then I registered the comhost library and from this point it can be used in ABAP.
    regsvr32 Test.comhost.dll​


  6. Here a tiny ABAP report.
    "-Begin-----------------------------------------------------------------
    Report Z_COM_TEST.

    Data:
    oTest Type OLE2_OBJECT,
    Result Type String.

    Create Object oTest 'Test.Class1'.
    Check sy-subrc = 0 And oTest-Handle <> 0 And oTest-Type = 'OLE2'.
    Call Method Of oTest 'retHello' = Result.
    Write: / Result.

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




This example is not very inventive, but it shows very good how easy .NET Core can be used from ABAP. And should the SAP GUI for Windows also exist in a 64-bit version in the future, we simply change the runtime option to win-x64.

Labels in this area