//Release the device handle counter. Must be called for every GetReaderHandle to prevent resource leak. public static void ReleaseReaderHandle(DPUruNet.Reader reader) { BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; FieldInfo field = reader.GetType().GetField("m_handle", flags); System.Runtime.InteropServices.SafeHandle privateHandle = (System.Runtime.InteropServices.SafeHandle)field.GetValue(reader); if (privateHandle != null && !privateHandle.IsInvalid && !privateHandle.IsClosed) { privateHandle.DangerousRelease(); //decrement the device handle counter so the reader can be freed } }
//Must be called to get device handle to control the LEDs (this must be coupled with a ReleaseReaderHandle) public static IntPtr GetReaderHandle(DPUruNet.Reader reader) { //We must use reflection here to get the private device handle BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; FieldInfo field = reader.GetType().GetField("m_handle", flags); System.Runtime.InteropServices.SafeHandle privateHandle = (System.Runtime.InteropServices.SafeHandle)field.GetValue(reader); Boolean bSuccess = false; if (privateHandle != null) { privateHandle.DangerousAddRef(ref bSuccess); //increment the reference counter for the reader\device handle } if (bSuccess) { return(privateHandle.DangerousGetHandle()); //return the handle to the caller } else { return(IntPtr.Zero); } }