From bbe5f6002cbf3f17d51bb7a89cc04cf9447ae769 Mon Sep 17 00:00:00 2001 From: David Hall Date: Wed, 31 Jul 2019 20:36:02 -0600 Subject: [PATCH] Simplified GetEnvironmentStrings --- PInvoke/Kernel32/ProcessEnv.cs | 35 +++++++-------------------- UnitTests/PInvoke/Kernel32/ProcessEnvTests.cs | 6 ++--- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/PInvoke/Kernel32/ProcessEnv.cs b/PInvoke/Kernel32/ProcessEnv.cs index da7e2789..a7cd700b 100644 --- a/PInvoke/Kernel32/ProcessEnv.cs +++ b/PInvoke/Kernel32/ProcessEnv.cs @@ -119,9 +119,13 @@ namespace Vanara.PInvoke /// If the function fails, the return value is NULL. /// // LPTCH WINAPI GetEnvironmentStrings(void); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx - [DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("WinBase.h", MSDNShortId = "ms683187")] - public static extern EnvironmentStrings GetEnvironmentStrings(); + public static string[] GetEnvironmentStrings() + { + var ptr = InternalGetEnvironmentStrings(); + try { return ptr.ToStringEnum().ToArray(); } + finally { FreeEnvironmentStrings(ptr); } + } /// Retrieves the contents of the specified variable from the environment block of the calling process. /// The name of the environment variable. @@ -358,29 +362,8 @@ namespace Vanara.PInvoke [DllImport(Lib.Kernel32, SetLastError = false, EntryPoint = "GetCommandLine", CharSet = CharSet.Auto)] private static extern IntPtr GetCommandLineInternal(); - /// Represents a block of environment strings obtained by and freed by . - /// - /// - public sealed class EnvironmentStrings : GenericSafeHandle, IEnumerable - { - /// Initializes a new instance of the class. - public EnvironmentStrings() : this(IntPtr.Zero) { } - - /// Initializes a new instance of the class. - /// The handle. - public EnvironmentStrings(IntPtr handle) : base(handle, FreeEnvironmentStrings) { } - - /// Gets the value. - /// The value. - public IEnumerable Value => handle.ToStringEnum(); - - /// Returns an enumerator that iterates through the collection. - /// A that can be used to iterate through the collection. - public IEnumerator GetEnumerator() => Value.GetEnumerator(); - - /// Returns an enumerator that iterates through a collection. - /// An object that can be used to iterate through the collection. - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - } + [DllImport(Lib.Kernel32, SetLastError = false, EntryPoint = "GetEnvironmentStrings", CharSet = CharSet.Auto)] + [PInvokeData("WinBase.h", MSDNShortId = "ms683187")] + private static extern IntPtr InternalGetEnvironmentStrings(); } } \ No newline at end of file diff --git a/UnitTests/PInvoke/Kernel32/ProcessEnvTests.cs b/UnitTests/PInvoke/Kernel32/ProcessEnvTests.cs index c3634a6e..4ae1d0ae 100644 --- a/UnitTests/PInvoke/Kernel32/ProcessEnvTests.cs +++ b/UnitTests/PInvoke/Kernel32/ProcessEnvTests.cs @@ -24,12 +24,12 @@ namespace Vanara.PInvoke.Tests } [Test] - public void GetEnvironmentStringsTest() + public void GetSetEnvironmentStringsTest() { var es = GetEnvironmentStrings(); - Assert.That(es.IsInvalid, Is.False); - Assert.That(es.Value, Is.Not.Empty); + Assert.That(es, Is.Not.Empty); TestContext.WriteLine(string.Join("\r\n", es)); + Assert.That(SetEnvironmentStrings(es), ResultIs.Successful); } [Test]