param([Parameter(Mandatory = $true)][string]$StateFile) $ErrorActionPreference = 'Stop' Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing Add-Type -ReferencedAssemblies System.Windows.Forms,System.Drawing,System.Web.Extensions @' using System; using System.IO; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Web.Script.Serialization; using System.Windows.Forms; using System.Drawing; public sealed class ArtemisMenuFixture : Form { [DllImport("user32.dll")] static extern IntPtr CreatePopupMenu(); [DllImport("user32.dll", CharSet=CharSet.Unicode)] static extern bool AppendMenuW(IntPtr menu, uint flags, UIntPtr id, string text); [DllImport("user32.dll")] static extern uint TrackPopupMenu(IntPtr menu, uint flags, int x, int y, int reserved, IntPtr owner, IntPtr rect); [DllImport("user32.dll")] static extern bool DestroyMenu(IntPtr menu); readonly string stateFile; int count, attempts; long result = -1; string error = ""; public ArtemisMenuFixture(string path) { stateFile = path; Text = "ARTEMIS native menu fixture"; Size = new Size(560, 360); StartPosition = FormStartPosition.CenterScreen; var button = new Button { Text="Open native menu", Location=new Point(30,30), Size=new Size(240,55) }; button.Click += delegate { var menu = CreatePopupMenu(); try { attempts++; Save(); if (!AppendMenuW(menu, 0, new UIntPtr(1), "Increment once")) throw new Exception("AppendMenuW failed"); result = TrackPopupMenu(menu, 0x100, Left+45, Top+140, 0, Handle, IntPtr.Zero); if (result == 1) count++; } catch (Exception ex) { error = ex.Message; } finally { DestroyMenu(menu); Save(); } }; Controls.Add(button); Shown += delegate { Save(); }; } void Save() { File.WriteAllText(stateFile, new JavaScriptSerializer().Serialize(new Dictionary { {"pid",Process.GetCurrentProcess().Id}, {"hwnd",Handle.ToInt64()}, {"selected_count",count}, {"menu_attempts",attempts}, {"menu_result",result}, {"menu_error",error} })); } } '@ [System.Windows.Forms.Application]::Run([ArtemisMenuFixture]::new($StateFile))