Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.InteropServices;
- namespace CSharpPlayground
- {
- public static class GetProcArgv
- {
- private const int CTL_KERN = 1;
- private const int KERN_ARGMAX = 8;
- private const int KERN_PROCARGS2 = 49;
- public static void PrintCurrProcArgv()
- {
- unsafe
- {
- // Get current process pid
- int pid = getpid();
- // Get proc table size
- int[] mib = new [] { CTL_KERN, KERN_ARGMAX };
- int size = sizeof(int);
- int maxargs = 0;
- if (sysctl(mib, mib.Length, &maxargs, &size, IntPtr.Zero, 0) == -1)
- {
- Console.Error.WriteLine("argmax");
- return;
- }
- // Now read the proc table
- IntPtr procargs = Marshal.AllocHGlobal(maxargs);
- size = maxargs;
- mib = new [] { CTL_KERN, KERN_PROCARGS2, pid };
- if (sysctl(mib, mib.Length, procargs.ToPointer(), &size, IntPtr.Zero, 0) == -1)
- {
- Console.Error.WriteLine("procargs");
- return;
- }
- // Skip over argc
- IntPtr argv0Ptr = IntPtr.Add(procargs, sizeof(int));
- string argv0 = Marshal.PtrToStringAnsi(argv0Ptr);
- Console.WriteLine(argv0);
- }
- }
- [DllImport("libc")]
- private static extern int getpid();
- [DllImport("libc")]
- private static unsafe extern int sysctl(int[] mib, int length, void* oldp, int* oldlenp, IntPtr newp, int newlenp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement