I have implemented and used the code snippet from the post:
Add proper p/invoke signature to PowerShell script
However, the script only works consistently if run from PowerShell ISE or if embedded as a function in a module and then call it implicitly (Module Auto-Loading).
Otherwise, I receive the following error message right before the console crashes:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Runtime.InteropServices.Marshal.PtrToStructureHelper(IntPtr ptr, Object structure, Boolean allowValueClasses)
at System.Runtime.InteropServices.Marshal.PtrToStructure(IntPtr ptr, Type structureType) ...
Can anyone point me in the right direction to solve this issue / bug?