Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created May 24, 2023 00:44
Show Gist options
  • Save brianmed/8a36b67a6070607227e9727f92e1480f to your computer and use it in GitHub Desktop.
Save brianmed/8a36b67a6070607227e9727f92e1480f to your computer and use it in GitHub Desktop.
C# macOS getrlimit example via DllImport
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
using System.Runtime.InteropServices;
namespace getrlimit;
class Program
{
[DllImport("libc", CallingConvention = CallingConvention.Cdecl)]
public static extern int getrlimit(int resource, IntPtr rlp);
[StructLayout(LayoutKind.Sequential)]
public struct rlimit
{
public Int64 rlim_cur;
public Int64 rlim_max;
}
static void Main(string[] args)
{
int RLIMIT_STACK = 3;
rlimit joy = new();
unsafe
{
void *rlimitPtr = &joy;
getrlimit(RLIMIT_STACK, new(rlimitPtr));
}
Console.WriteLine(joy.rlim_cur);
Console.WriteLine(joy.rlim_max);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment