Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. namespace CSharpPlayground
  5. {
  6. public static class GetProcArgv
  7. {
  8. private const int CTL_KERN = 1;
  9. private const int KERN_ARGMAX = 8;
  10. private const int KERN_PROCARGS2 = 49;
  11.  
  12. public static void PrintCurrProcArgv()
  13. {
  14. unsafe
  15. {
  16. // Get current process pid
  17. int pid = getpid();
  18.  
  19. // Get proc table size
  20. int[] mib = new [] { CTL_KERN, KERN_ARGMAX };
  21. int size = sizeof(int);
  22. int maxargs = 0;
  23.  
  24. if (sysctl(mib, mib.Length, &maxargs, &size, IntPtr.Zero, 0) == -1)
  25. {
  26. Console.Error.WriteLine("argmax");
  27. return;
  28. }
  29.  
  30. // Now read the proc table
  31.  
  32. IntPtr procargs = Marshal.AllocHGlobal(maxargs);
  33. size = maxargs;
  34.  
  35. mib = new [] { CTL_KERN, KERN_PROCARGS2, pid };
  36.  
  37. if (sysctl(mib, mib.Length, procargs.ToPointer(), &size, IntPtr.Zero, 0) == -1)
  38. {
  39. Console.Error.WriteLine("procargs");
  40. return;
  41. }
  42.  
  43. // Skip over argc
  44. IntPtr argv0Ptr = IntPtr.Add(procargs, sizeof(int));
  45. string argv0 = Marshal.PtrToStringAnsi(argv0Ptr);
  46. Console.WriteLine(argv0);
  47. }
  48. }
  49.  
  50. [DllImport("libc")]
  51. private static extern int getpid();
  52.  
  53. [DllImport("libc")]
  54. private static unsafe extern int sysctl(int[] mib, int length, void* oldp, int* oldlenp, IntPtr newp, int newlenp);
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement