/* helper functions */ // some more details: https://blog.quarkslab.com/playing-with-the-windows-notification-facility-wnf.html private static MorphicResult <byte[], MorphicUnit> QueryWnfStateData(ExtendedPInvoke.WNF_STATE_NAME stateName) { const uint MAX_BUFFER_LENGTH = 4096; uint bufferSize = MAX_BUFFER_LENGTH; var pointerToBuffer = Marshal.AllocHGlobal((int)bufferSize); try { uint changeStamp; var queryWnfStateDataResult = ExtendedPInvoke.NtQueryWnfStateData(ref stateName, IntPtr.Zero, IntPtr.Zero, out changeStamp, pointerToBuffer, ref bufferSize); if (queryWnfStateDataResult != 0) { return(MorphicResult.ErrorResult()); } var bufferSizeAsInt = (int)bufferSize; var result = new byte[bufferSizeAsInt]; Marshal.Copy(pointerToBuffer, result, 0, bufferSizeAsInt); return(MorphicResult.OkResult(result)); } finally { Marshal.FreeHGlobal(pointerToBuffer); } }
private static MorphicResult <MorphicUnit, MorphicUnit> UpdateWnfStateData(ExtendedPInvoke.WNF_STATE_NAME stateName, byte[] buffer) { var bufferLength = buffer.Length; var pointerToBuffer = Marshal.AllocHGlobal(bufferLength); try { Marshal.Copy(buffer, 0, pointerToBuffer, bufferLength); var updateWnfStateDataResult = ExtendedPInvoke.NtUpdateWnfStateData(ref stateName, pointerToBuffer, (uint)bufferLength, IntPtr.Zero /* null */, IntPtr.Zero, 0, 0); if (updateWnfStateDataResult != 0) { return(MorphicResult.ErrorResult()); } } finally { Marshal.FreeHGlobal(pointerToBuffer); } return(MorphicResult.OkResult()); }