Fixed CallMethodWithTypedBuf to only set value on success.

pull/83/head
David Hall 2019-11-03 16:01:49 -07:00
parent 4143c8767e
commit 31775c94f0
1 changed files with 6 additions and 4 deletions

View File

@ -112,13 +112,15 @@ namespace Vanara.PInvoke
if (err.Failed && (bufErr == null || bufErr.Value != err) && !buffErrs.Contains(err)) return err; if (err.Failed && (bufErr == null || bufErr.Value != err) && !buffErrs.Contains(err)) return err;
using (var buf = new SafeHGlobalHandle(sz.ToInt32(null))) using (var buf = new SafeHGlobalHandle(sz.ToInt32(null)))
{ {
var ret = method(buf.DangerousGetHandle(), ref sz); err = method(buf.DangerousGetHandle(), ref sz);
result = (outConverter ?? Conv)(buf.DangerousGetHandle(), sz); if (err.Succeeded)
return ret; result = (outConverter ?? Conv)(buf.DangerousGetHandle(), sz);
return err;
} }
Win32Error GetSize(ref TSize sz1) => method(IntPtr.Zero, ref sz1); Win32Error GetSize(ref TSize sz1) => method(IntPtr.Zero, ref sz1);
TOut Conv(IntPtr p, TSize s) => p.ToStructure<TOut>();
static TOut Conv(IntPtr p, TSize s) => p == IntPtr.Zero ? default : p.Convert<TOut>(Convert.ToUInt32(s));
} }
/// <summary>Calls a method with buffer for a type and gets the result or error.</summary> /// <summary>Calls a method with buffer for a type and gets the result or error.</summary>