Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using System.ComponentModel;
  8.  
  9. namespace EnvironmentVarTest
  10. {
  11. class Program
  12. {
  13. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  14. [return: MarshalAs(UnmanagedType.Bool)]
  15. public static extern bool
  16. SendMessageTimeout(
  17. IntPtr hWnd,
  18. int Msg,
  19. int wParam,
  20. string lParam,
  21. int fuFlags,
  22. int uTimeout,
  23. out int lpdwResult
  24. );
  25.  
  26. public const int HWND_BROADCAST = 0xffff;
  27. public const int WM_SETTINGCHANGE = 0x001A;
  28. public const int SMTO_NORMAL = 0x0000;
  29. public const int SMTO_BLOCK = 0x0001;
  30. public const int SMTO_ABORTIFHUNG = 0x0002;
  31. public const int SMTO_NOTIMEOUTIFNOTHUNG = 0x0008;
  32.  
  33.  
  34. static void Main(string[] args)
  35. {
  36. Program p = new Program();
  37. string environmentVariableValue = DateTime.Now.ToLongTimeString().Replace(":", String.Empty);
  38. Console.WriteLine("On the CMD window that opens up after about 10 seconds, if you type %samplevar% and hit Enter, you should see: " + environmentVariableValue);
  39. p.SetEnvironmentVariable(environmentVariableValue);
  40. RefreshProcessVars();
  41. p.ReadEnvironmentVariable();
  42.  
  43. p.StartCMD();
  44. Console.ReadLine();
  45. }
  46.  
  47. void SetEnvironmentVariable(string value)
  48. {
  49. System.Environment.SetEnvironmentVariable("samplevar", value, EnvironmentVariableTarget.Machine);
  50.  
  51. }
  52.  
  53. static void RefreshProcessVars()
  54. {
  55. int result;
  56. bool callresult = SendMessageTimeout(
  57. (System.IntPtr)HWND_BROADCAST,
  58. WM_SETTINGCHANGE,
  59. 0,
  60. "Environment",
  61. SMTO_BLOCK | SMTO_ABORTIFHUNG | SMTO_NOTIMEOUTIFNOTHUNG,
  62. 10000,
  63. out result);
  64.  
  65. if (!callresult || result == 0)
  66. {
  67. int lasterror = Marshal.GetLastWin32Error();
  68. Win32Exception winex = new Win32Exception(lasterror);
  69. Console.WriteLine("Exception happened while calling SendMessageTimeOut. The exception message is " + winex.Message);
  70. }
  71. }
  72. void ReadEnvironmentVariable()
  73. {
  74. var x = System.Environment.GetEnvironmentVariable("smaplevar", EnvironmentVariableTarget.Machine);
  75.  
  76. }
  77.  
  78. void StartCMD()
  79. {
  80. Process.Start("cmd.exe");
  81. }
  82. }
  83. }
  84.  
  85. [DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  86. [return:MarshalAs(UnmanagedType.Bool)]
  87. public static extern bool
  88. SendMessageTimeout(
  89. IntPtr hWnd,
  90. int Msg,
  91. int wParam,
  92. string lParam,
  93. int fuFlags,
  94. int uTimeout,
  95. out int lpdwResult
  96. );
  97.  
  98. public const int HWND_BROADCAST = 0xffff;
  99. public const int WM_SETTINGCHANGE = 0x001A;
  100. public const int SMTO_NORMAL = 0x0000;
  101. public const int SMTO_BLOCK = 0x0001;
  102. public const int SMTO_ABORTIFHUNG = 0x0002;
  103. public const int SMTO_NOTIMEOUTIFNOTHUNG = 0x0008;
  104.  
  105. int result;
  106. SendMessageTimeout(
  107. (System.IntPtr)HWND_BROADCAST,
  108. WM_SETTINGCHANGE,
  109. 0,
  110. "Environment",
  111. SMTO_BLOCK | SMTO_ABORTIFHUNG | SMTO_NOTIMEOUTIFNOTHUNG,
  112. SomeTimeoutValue,
  113. out result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement