|
| 1 | +param ( |
| 2 | + [Parameter(Mandatory = $true)] |
| 3 | + [int]$DisplayCount |
| 4 | +) |
| 5 | + |
| 6 | +Add-Type -TypeDefinition @" |
| 7 | +using System; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using System.Threading; |
| 10 | +
|
| 11 | +public class ParsecVdd { |
| 12 | + const uint GENERIC_READ = 0x80000000; |
| 13 | + const uint GENERIC_WRITE = 0x40000000; |
| 14 | + const uint FILE_SHARE_READ = 0x00000001; |
| 15 | + const uint FILE_SHARE_WRITE = 0x00000002; |
| 16 | + const uint OPEN_EXISTING = 3; |
| 17 | + const uint FILE_FLAG_NO_BUFFERING = 0x20000000; |
| 18 | + const uint FILE_FLAG_OVERLAPPED = 0x40000000; |
| 19 | + const uint FILE_FLAG_WRITE_THROUGH = 0x80000000; |
| 20 | + const uint DIGCF_PRESENT = 0x00000002; |
| 21 | + const uint DIGCF_DEVICEINTERFACE = 0x00000010; |
| 22 | + const uint IOCTL_ADD = 0x0022e004; |
| 23 | + const uint IOCTL_UPDATE = 0x0022a00c; |
| 24 | + static readonly Guid VDD_ADAPTER_GUID = new Guid("00b41627-04c4-429e-a26e-0265cf50c8fa"); |
| 25 | +
|
| 26 | + [StructLayout(LayoutKind.Sequential)] |
| 27 | + struct SP_DEVICE_INTERFACE_DATA { |
| 28 | + public int cbSize; |
| 29 | + public Guid interfaceClassGuid; |
| 30 | + public uint flags; |
| 31 | + public IntPtr reserved; |
| 32 | + } |
| 33 | +
|
| 34 | + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] |
| 35 | + struct SP_DEVICE_INTERFACE_DETAIL_DATA_A { |
| 36 | + public int cbSize; |
| 37 | + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] |
| 38 | + public string DevicePath; |
| 39 | + } |
| 40 | +
|
| 41 | + [StructLayout(LayoutKind.Sequential)] |
| 42 | + struct OVERLAPPED { |
| 43 | + public IntPtr Internal; |
| 44 | + public IntPtr InternalHigh; |
| 45 | + public IntPtr Pointer; |
| 46 | + public IntPtr hEvent; |
| 47 | + } |
| 48 | +
|
| 49 | + [DllImport("setupapi.dll", SetLastError = true)] |
| 50 | + static extern IntPtr SetupDiGetClassDevsA( |
| 51 | + ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, uint Flags); |
| 52 | +
|
| 53 | + [DllImport("setupapi.dll", SetLastError = true)] |
| 54 | + static extern bool SetupDiEnumDeviceInterfaces( |
| 55 | + IntPtr DeviceInfoSet, IntPtr DeviceInfoData, |
| 56 | + ref Guid InterfaceClassGuid, uint MemberIndex, |
| 57 | + ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData); |
| 58 | +
|
| 59 | + [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Ansi)] |
| 60 | + static extern bool SetupDiGetDeviceInterfaceDetailA( |
| 61 | + IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, |
| 62 | + ref SP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData, |
| 63 | + uint DeviceInterfaceDetailDataSize, out uint RequiredSize, IntPtr DeviceInfoData); |
| 64 | +
|
| 65 | + [DllImport("setupapi.dll")] |
| 66 | + static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet); |
| 67 | +
|
| 68 | + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)] |
| 69 | + static extern IntPtr CreateFileA( |
| 70 | + string lpFileName, uint dwDesiredAccess, uint dwShareMode, |
| 71 | + IntPtr lpSecurityAttributes, uint dwCreationDisposition, |
| 72 | + uint dwFlagsAndAttributes, IntPtr hTemplateFile); |
| 73 | +
|
| 74 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 75 | + static extern IntPtr CreateEvent( |
| 76 | + IntPtr lpEventAttributes, bool bManualReset, |
| 77 | + bool bInitialState, string lpName); |
| 78 | +
|
| 79 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 80 | + static extern bool DeviceIoControl( |
| 81 | + IntPtr hDevice, uint dwIoControlCode, |
| 82 | + byte[] lpInBuffer, int nInBufferSize, |
| 83 | + out int lpOutBuffer, int nOutBufferSize, |
| 84 | + IntPtr lpBytesReturned, ref OVERLAPPED lpOverlapped); |
| 85 | +
|
| 86 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 87 | + static extern bool GetOverlappedResultEx( |
| 88 | + IntPtr hFile, ref OVERLAPPED lpOverlapped, |
| 89 | + out uint lpNumberOfBytesTransferred, |
| 90 | + int dwMilliseconds, bool bAlertable); |
| 91 | +
|
| 92 | + [DllImport("kernel32.dll")] |
| 93 | + static extern bool CloseHandle(IntPtr hObject); |
| 94 | +
|
| 95 | + public static IntPtr OpenHandle() { |
| 96 | + var guid = VDD_ADAPTER_GUID; |
| 97 | + var devInfo = SetupDiGetClassDevsA( |
| 98 | + ref guid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); |
| 99 | + if (devInfo == new IntPtr(-1)) return new IntPtr(-1); |
| 100 | +
|
| 101 | + var ifaceData = new SP_DEVICE_INTERFACE_DATA(); |
| 102 | + ifaceData.cbSize = Marshal.SizeOf(ifaceData); |
| 103 | +
|
| 104 | + for (uint i = 0; SetupDiEnumDeviceInterfaces( |
| 105 | + devInfo, IntPtr.Zero, ref guid, i, ref ifaceData); i++) { |
| 106 | + var detail = new SP_DEVICE_INTERFACE_DETAIL_DATA_A(); |
| 107 | + detail.cbSize = IntPtr.Size == 8 ? 8 : 6; |
| 108 | + uint needed; |
| 109 | + SetupDiGetDeviceInterfaceDetailA( |
| 110 | + devInfo, ref ifaceData, ref detail, |
| 111 | + (uint)Marshal.SizeOf(detail), out needed, IntPtr.Zero); |
| 112 | + var handle = CreateFileA( |
| 113 | + detail.DevicePath, |
| 114 | + GENERIC_READ | GENERIC_WRITE, |
| 115 | + FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 116 | + IntPtr.Zero, OPEN_EXISTING, |
| 117 | + FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED | FILE_FLAG_WRITE_THROUGH, |
| 118 | + IntPtr.Zero); |
| 119 | + if (handle != IntPtr.Zero && handle != new IntPtr(-1)) { |
| 120 | + SetupDiDestroyDeviceInfoList(devInfo); |
| 121 | + return handle; |
| 122 | + } |
| 123 | + } |
| 124 | +
|
| 125 | + SetupDiDestroyDeviceInfoList(devInfo); |
| 126 | + return new IntPtr(-1); |
| 127 | + } |
| 128 | +
|
| 129 | + static int IoControl(IntPtr vdd, uint code, byte[] input) { |
| 130 | + var inBuf = new byte[32]; |
| 131 | + if (input != null) Array.Copy(input, inBuf, Math.Min(input.Length, inBuf.Length)); |
| 132 | + var ov = new OVERLAPPED(); |
| 133 | + ov.hEvent = CreateEvent(IntPtr.Zero, true, false, null); |
| 134 | + int outBuf = 0; |
| 135 | + DeviceIoControl(vdd, code, inBuf, inBuf.Length, out outBuf, 4, IntPtr.Zero, ref ov); |
| 136 | + uint transferred; |
| 137 | + GetOverlappedResultEx(vdd, ref ov, out transferred, 5000, false); |
| 138 | + if (ov.hEvent != IntPtr.Zero) CloseHandle(ov.hEvent); |
| 139 | + return outBuf; |
| 140 | + } |
| 141 | +
|
| 142 | + public static void Update(IntPtr vdd) { IoControl(vdd, IOCTL_UPDATE, null); } |
| 143 | +
|
| 144 | + public static int AddDisplay(IntPtr vdd) { |
| 145 | + int idx = IoControl(vdd, IOCTL_ADD, null); |
| 146 | + Update(vdd); |
| 147 | + return idx; |
| 148 | + } |
| 149 | +
|
| 150 | + public static void Keepalive(IntPtr vdd) { |
| 151 | + while (true) { Update(vdd); Thread.Sleep(100); } |
| 152 | + } |
| 153 | +} |
| 154 | +"@ |
| 155 | + |
| 156 | +$vdd = [ParsecVdd]::OpenHandle() |
| 157 | +if ($vdd -eq [IntPtr]::new(-1)) { |
| 158 | + Write-Error "Failed to open the Parsec VDD device handle." |
| 159 | + exit 1 |
| 160 | +} |
| 161 | + |
| 162 | +for ($i = 1; $i -le $DisplayCount; $i++) { |
| 163 | + $idx = [ParsecVdd]::AddDisplay($vdd) |
| 164 | + Write-Information "Added virtual display at index $idx" |
| 165 | +} |
| 166 | + |
| 167 | +Write-Information "Keeping $DisplayCount virtual display(s) alive..." |
| 168 | +[ParsecVdd]::Keepalive($vdd) |
0 commit comments