Skip to content

Commit e1d6269

Browse files
committed
More elegant way to get win32 last error as string
1 parent d64923c commit e1d6269

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Project/Hid/HidUtils.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
using System;
2121
using System.Collections.Generic;
22+
using System.Runtime.InteropServices;
2223
using System.Text;
2324

2425

@@ -119,5 +120,25 @@ public static T ToEnum<T>(this int value)
119120
}
120121

121122
}
123+
}
124+
125+
//TODO: Move this to SharpLib.Win32 v2
126+
namespace SharpLib.Win32
127+
{
128+
using SharpLib.ToEnum;
129+
using Windows.Win32.Foundation;
130+
public static class GetLastError
131+
{
132+
public static string String()
133+
{
134+
return Enum().ToString();
135+
}
122136

137+
public static WIN32_ERROR Enum()
138+
{
139+
int err = Marshal.GetLastWin32Error();
140+
return err.ToEnum<WIN32_ERROR>();
141+
}
142+
143+
}
123144
}

Project/HidDemo/MainForm.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ public unsafe static bool OpenPropertiesDialog(string aDevicePath)
315315
SetupDiDestroyDeviceInfoListSafeHandle hDevInfo = PInvoke.SetupDiGetClassDevs(null, (string)null, new HWND(0), Extra.Const.DIGCF_ALLCLASSES | Extra.Const.DIGCF_PRESENT);
316316
if (hDevInfo.IsInvalid)
317317
{
318-
int err = Marshal.GetLastWin32Error();
319-
Trace.WriteLine("SetupDiGetClassDevs error: " + err.ToEnum<WIN32_ERROR>());
318+
Trace.WriteLine("SetupDiGetClassDevs error: " + GetLastError.String());
320319
return false;
321320
}
322321

@@ -349,11 +348,7 @@ public unsafe static bool OpenPropertiesDialog(string aDevicePath)
349348
keepGoing = PInvoke.SetupDiEnumDeviceInterfaces(hDevInfo, null, null, index, &deviceInterfaceData);
350349
if (!keepGoing)
351350
{
352-
int err = Marshal.GetLastWin32Error();
353-
354-
Trace.WriteLine("SetupDiEnumDeviceInterfaces error: " + err.ToEnum<WIN32_ERROR>());
355-
356-
//Debug.Print(Marshal.GetLastWin32Error().ToString());
351+
Trace.WriteLine("SetupDiEnumDeviceInterfaces error: " + GetLastError.String());
357352
break;
358353
}
359354

0 commit comments

Comments
 (0)