Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Using .NET PInvoke for Linux system functions

March 25, 2019
Tom Deseyn
Related topics:
.NETC, C#, C++Linux
Related products:
Red Hat Enterprise Linux

    If you've developed Windows applications with .NET, you may have found yourself in a situation where the framework did not provide the APIs you needed. When that happens, you first need to identify the system APIs and then make them available using PInvoke. A website like pinvoke.net provides copy-and-pasteable code snippets for many Win32 API functions.

    .NET Platform Invoke (PInvoke) makes it easy to consume native libraries. In this article, we'll take a look at using PInvoke for Linux system functions.

    PInvoking Linux

    In case you are not familiar with PInvoke, let's look at a simple example:

    [DllImport("mylibrary")]
    public static extern int foo();
    

    This makes available the function foo from the native library mylibrary. This function accepts no arguments and returns an int. .NET takes care of marshaling the argument types. It is possible to use managed types (like strings) in these signatures, which will be automagically marshaled.

    PInvoke works at the application binary interface (ABI) level: It respects the binary calling conventions of the platform on which it is running.

    Linux is a UNIX flavor operating system. It provides many of the APIs prescribed by POSIX and SUS; however, these APIs are defined using the C programming language. They are not APIs at the binary level (ABIs).

    For example, the standards describe that there should be positive values for indicating certain types of errors. These values are named (for example, EAGAIN), but their values are not defined. This means different platforms can (and will) have different values for these names. This makes it impossible to add a const int to a C# file that has the correct value for all platforms.

    Another example is struct, which is used in the API to define a number of fields; however, the order is not fixed, and platforms are allowed to add extra fields. So again, we can't describe a struct in C# that matches the native struct on all platforms.

    The generic way to solve this is to introduce a native shim library. This library provides its own set of functions, constants, and structs that are platform-independent with a fixed ABI. This API gets PInvoked by .NET. Within the library, this API calls the native functions.

    If you want to PInvoke the system C library without using a shim, you can use libc as the library name. The .NET Core runtime maps this to the system C library. For example, we can expose the kill function as follows:

    [DllImport("libc", SetLastError = true))]
    public static extern int kill(int pid, int sig);
    

    SetLastError means the function uses errno to indicate what went wrong. Marshal.GetLastWin32Error can be used to retrieve errno.

    Mono.Posix.NETStandard

    The Mono.Posix.NETStandard package provides system functions. It uses native shims to work across a number of platforms. If we download the package from nuget.org, we can take a look inside:

    $ unzip -l mono.posix.netstandard.1.0.0.nupkg
    Archive:  mono.posix.netstandard.1.0.0.nupkg
      Length  	Date	Time	Name
    ---------  ---------- -----   ----
      	516  02-28-2018 15:18   _rels/.rels
      	814  02-28-2018 15:18   Mono.Posix.NETStandard.nuspec
    	13376  02-28-2018 15:18   lib/net40/Mono.Posix.NETStandard.dll
    	13376  02-28-2018 15:18   ref/net40/Mono.Posix.NETStandard.dll
       189504  02-28-2018 15:18   ref/netstandard2.0/Mono.Posix.NETStandard.dll
       189504  02-28-2018 15:18   runtimes/linux-arm/lib/netstandard2.0/Mono.Posix.NETStandard.dll
       183240  02-28-2018 15:18   runtimes/linux-arm/native/libMonoPosixHelper.so
       189504  02-28-2018 15:18   runtimes/linux-arm64/lib/netstandard2.0/Mono.Posix.NETStandard.dll
       246736  02-28-2018 15:18   runtimes/linux-arm64/native/libMonoPosixHelper.so
       189504  02-28-2018 15:18   runtimes/linux-armel/lib/netstandard2.0/Mono.Posix.NETStandard.dll
       236480  02-28-2018 15:18   runtimes/linux-armel/native/libMonoPosixHelper.so
       189504  02-28-2018 15:18   runtimes/linux-x64/lib/netstandard2.0/Mono.Posix.NETStandard.dll
       920882  02-28-2018 15:18   runtimes/linux-x64/native/libMonoPosixHelper.so
       189504  02-28-2018 15:18   runtimes/linux-x86/lib/netstandard2.0/Mono.Posix.NETStandard.dll
       766620  02-28-2018 15:18   runtimes/linux-x86/native/libMonoPosixHelper.so
       189504  02-28-2018 15:18   runtimes/osx/lib/netstandard2.0/Mono.Posix.NETStandard.dll
       535888  02-28-2018 15:18   runtimes/osx/native/libMonoPosixHelper.dylib
       189504  02-28-2018 15:18   runtimes/win-x64/lib/netstandard2.0/Mono.Posix.NETStandard.dll
      1495800  02-28-2018 15:18   runtimes/win-x64/native/libMonoPosixHelper.dll
    	87600  02-28-2018 15:18   runtimes/win-x64/native/MonoPosixHelper.dll
       189504  02-28-2018 15:18   runtimes/win-x86/lib/netstandard2.0/Mono.Posix.NETStandard.dll
      1256296  02-28-2018 15:18   runtimes/win-x86/native/libMonoPosixHelper.dll
       400120  02-28-2018 15:18   runtimes/win-x86/native/MonoPosixHelper.dll
      	592  02-28-2018 15:18   [Content_Types].xml
      	692  02-28-2018 15:18   package/services/metadata/core-properties/4b5c471cadba4490965015071f90289f.psmdcp
     	9475  10-11-2018 20:23   .signature.p7s
    ---------                 	-------
      7874039                 	26 files
    

    The libMonoPosixHelper.* files are the native shim libraries. The directories under runtimes are the supported platforms. There is Linux support for arm, arm64, armel, x64 and x86. The package also provides support for Windows and OS X. The Linux libraries are for flavors of Linux that are based on glibc (GNU C library), such as Fedora and Red Hat Enterprise Linux (RHEL). Some things won't work on flavors of Linux that are based on musl, such as Alpine. .NET Core applications using this package will pick the proper variant under the runtimes folder based on the platform on which they are running. This is based on the runtime identifier (RID) graph.

    Tmds.LibC

    A while back, I blogged about a Linux-specific transport for Kestrel. This library uses Linux-specific APIs that are not provided by .NET Core and Mono.Posix.NETStandard. So far, the library has used a native shim to make those functions accessible. A compiled version for glibc x64 is in the NuGet package. This means the Transport doesn’t work on other architectures or with other C libraries.

    As explained before, one way to solve this is to recompile the shim for different platforms. While that is easy to do in theory, in practice, there is quite a bit of work involved. On each platform, the native libraries need to be built. Then those libraries need to be collected in a single NuGet package. I wanted to try a different approach: a fully managed implementation.

    The managed implementation captures the platform ABIs in managed code. For each platform, we build an assembly that matches that platform’s ABI. The differences between platforms are small, so we can share a lot of code between them.

    A nice effect of capturing the platform ABIs is that our .NET API can be very close to the C API. We don’t need to introduce shim structs or shim constants.

    For example, the setsockopt option for changing a socket option has this C API:

    int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
    

    The C# API looks like this:

    public static int setsockopt(int socket, int level, int optname, void* optval, socklen_t optlen);
    

    As you can see, the signatures are virtually the same.

    Because these system functions may be useful for other developers, I’ve collected them in a separate library, named Tmds.LibC. You can find the source code on GitHub and packages are published to NuGet.org. The Kestrel Linux Transport has been updated to use this library. The Transport does not need to deal with platform specifics, and it now runs on arm32, arm64, and x64.

    Conclusion

    In this article, we looked at .NET PInvoke and how PInvoke differs on Linux compared to Windows. We then explored how Mono.Posix.NETStandard provides a set of POSIX functions on various platforms using native shims. Finally, we took a look at Tmds.LibC, which provides Linux system functions for a number architectures using a fully managed implementation.

    Last updated: May 8, 2024

    Recent Posts

    • Tekton joins the CNCF as an incubating project

    • Federated identity across the hybrid cloud using zero trust workload identity manager

    • Confidential virtual machine storage attack scenarios

    • Introducing virtualization platform autopilot

    • Integrate zero trust workload identity manager with Red Hat OpenShift GitOps

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.