private static string GetCommandString(Shell32.IContextMenu cMenu, uint offset, Shell32.GCS flags = Shell32.GCS.GCS_VERBW) { if (offset > 5000) { // Hackish workaround to avoid an AccessViolationException on some items, // notably the "Run with graphic processor" menu item of NVidia cards return(null); } SafeCoTaskMemString commandString = null; try { commandString = new SafeCoTaskMemString(512); cMenu.GetCommandString(new IntPtr(offset), flags, IntPtr.Zero, commandString, (uint)commandString.Capacity - 1); Debug.WriteLine("Verb {0}: {1}", offset, commandString); return(commandString.ToString()); } catch (Exception ex) when(ex is InvalidCastException || ex is ArgumentException) { // TODO: investigate this.. Debug.WriteLine(ex); return(null); } catch (Exception ex) when(ex is COMException || ex is NotImplementedException) { // Not every item has an associated verb return(null); } finally { commandString?.Dispose(); } }
public void ToStringTest() { const int sz = 100; var s = new SafeCoTaskMemString(new string('x', sz)); Assert.That(s.ToString(), Is.EqualTo(new string('x', sz))); }
private static string GetCommandString(Shell32.IContextMenu cMenu, uint offset, Shell32.GCS flags = Shell32.GCS.GCS_VERBW) { SafeCoTaskMemString commandString = null; try { commandString = new SafeCoTaskMemString(512); cMenu.GetCommandString(new IntPtr(offset), flags, IntPtr.Zero, commandString, (uint)commandString.Capacity - 1); Debug.WriteLine("Verb {0}: {1}", offset, commandString); return(commandString.ToString()); } catch (Exception ex) when(ex is InvalidCastException || ex is ArgumentException) { // TODO: investigate this.. Debug.WriteLine(ex); return(null); } catch (Exception ex) when(ex is COMException || ex is NotImplementedException) { // Not every item has an associated verb return(null); } finally { commandString?.Dispose(); } }
public void PathCchAddBackslashExTest2() { var sb = new SafeCoTaskMemString(TestCaseSources.TempDirWhack, 64); Assert.That(PathCchAddBackslashEx((IntPtr)sb, sb.Capacity, out var end, out var rem), Is.EqualTo((HRESULT)HRESULT.S_FALSE)); Assert.That(sb.ToString(), Is.EqualTo(TestCaseSources.TempDirWhack)); Assert.That(end, Is.EqualTo(sb.DangerousGetHandle().Offset(sb.Length * 2))); sb = new SafeCoTaskMemString(TestCaseSources.TempDir, 64); Assert.That(PathCchAddBackslashEx((IntPtr)sb, sb.Size, out end, out rem), Is.EqualTo((HRESULT)0)); Assert.That(sb.ToString(), Is.EqualTo(TestCaseSources.TempDirWhack)); Assert.That(end, Is.EqualTo(sb.DangerousGetHandle().Offset(sb.Length * 2))); }
public void PathCchRemoveBackslashExTest() { var sb = new SafeCoTaskMemString(@"C:\Temp\", 64); Assert.That(PathCchRemoveBackslashEx((IntPtr)sb, sb.Capacity, out var end, out var rem), Is.EqualTo((HRESULT)0)); Assert.That(sb.ToString(), Is.EqualTo(@"C:\Temp")); Assert.That(end, Is.EqualTo(sb.DangerousGetHandle().Offset(14))); sb = new SafeCoTaskMemString(@"C:\Temp", 64); Assert.That(PathCchRemoveBackslashEx((IntPtr)sb, sb.Capacity, out end, out rem), Is.EqualTo((HRESULT)HRESULT.S_FALSE)); Assert.That(sb.ToString(), Is.EqualTo(@"C:\Temp")); Assert.That(end, Is.EqualTo(sb.DangerousGetHandle().Offset(14))); }