From 31775c94f0623532c47ad2f05da5a1685a172ac5 Mon Sep 17 00:00:00 2001 From: David Hall Date: Sun, 3 Nov 2019 16:01:49 -0700 Subject: [PATCH] Fixed CallMethodWithTypedBuf to only set value on success. --- PInvoke/Shared/FunctionHelper.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/PInvoke/Shared/FunctionHelper.cs b/PInvoke/Shared/FunctionHelper.cs index 2a7fdc2b..64f3c34e 100644 --- a/PInvoke/Shared/FunctionHelper.cs +++ b/PInvoke/Shared/FunctionHelper.cs @@ -112,13 +112,15 @@ namespace Vanara.PInvoke if (err.Failed && (bufErr == null || bufErr.Value != err) && !buffErrs.Contains(err)) return err; using (var buf = new SafeHGlobalHandle(sz.ToInt32(null))) { - var ret = method(buf.DangerousGetHandle(), ref sz); - result = (outConverter ?? Conv)(buf.DangerousGetHandle(), sz); - return ret; + err = method(buf.DangerousGetHandle(), ref sz); + if (err.Succeeded) + result = (outConverter ?? Conv)(buf.DangerousGetHandle(), sz); + return err; } Win32Error GetSize(ref TSize sz1) => method(IntPtr.Zero, ref sz1); - TOut Conv(IntPtr p, TSize s) => p.ToStructure(); + + static TOut Conv(IntPtr p, TSize s) => p == IntPtr.Zero ? default : p.Convert(Convert.ToUInt32(s)); } /// Calls a method with buffer for a type and gets the result or error.