/// <summary> /// Retrieves the command string for a specific item from an iContextMenu (Ansi) /// </summary> /// <param name="iContextMenu">the IContextMenu to receive the string from</param> /// <param name="idcmd">the id of the specific item</param> /// <param name="executeString">indicating whether it should return an execute string or not</param> /// <returns>if executeString is true it will return the executeString for the item, /// otherwise it will return the help info string</returns> public static string GetCommandStringA(IContextMenu iContextMenu, uint idcmd, bool executeString) { string info = string.Empty; byte[] bytes = new byte[256]; int index; iContextMenu.GetCommandString( idcmd, (executeString ? ShellFolders.GCS.VERBA : ShellFolders.GCS.HELPTEXTA), 0, bytes, ShellFolders.MAX_PATH); index = 0; while (index < bytes.Length && bytes[index] != 0) { index++; } if (index < bytes.Length) { info = Encoding.Default.GetString(bytes, 0, index); } return(info); }
/// <summary> /// Retrieves the command string for a specific item from an iContextMenu (Unicode) /// </summary> /// <param name="iContextMenu">the IContextMenu to receive the string from</param> /// <param name="idcmd">the id of the specific item</param> /// <param name="executeString">indicating whether it should return an execute string or not</param> /// <returns>if executeString is true it will return the executeString for the item, /// otherwise it will return the help info string</returns> public static string GetCommandStringW(IContextMenu iContextMenu, uint idcmd, bool executeString) { string info = string.Empty; byte[] bytes = new byte[256]; iContextMenu.GetCommandString( idcmd, (executeString ? GCS.VerbW : GCS.HelpTextW), 0, bytes, ShellApi.MaxPath); int index = 0; while (index < bytes.Length - 1 && (bytes[index] != 0 || bytes[index + 1] != 0)) { index += 2; } if (index < bytes.Length - 1) { info = Encoding.Unicode.GetString(bytes, 0, index + 1); } return info; }
/// <summary> /// Retrieves the command string for a specific item from an iContextMenu (Unicode) /// </summary> /// <param name="iContextMenu">the IContextMenu to receive the string from</param> /// <param name="idcmd">the id of the specific item</param> /// <param name="executeString">indicating whether it should return an execute string or not</param> /// <returns>if executeString is true it will return the executeString for the item, /// otherwise it will return the help info string</returns> public static string GetCommandStringW(IContextMenu iContextMenu, uint idcmd, bool executeString) { string info = string.Empty; byte[] bytes = new byte[256]; int index; iContextMenu.GetCommandString( idcmd, (executeString ? ShellFolders.GCS.VERBW : ShellFolders.GCS.HELPTEXTW), 0, bytes, ShellFolders.MAX_PATH); index = 0; while (index < bytes.Length - 1 && (bytes[index] != 0 || bytes[index + 1] != 0)) { index += 2; } if (index < bytes.Length - 1) { info = Encoding.Unicode.GetString(bytes, 0, index); } return(info); }
/// <summary> /// Retrieves the command string for a specific item from an iContextMenu (Ansi) /// </summary> /// <param name="iContextMenu">the IContextMenu to receive the string from</param> /// <param name="idcmd">the id of the specific item</param> /// <param name="executeString">indicating whether it should return an execute string or not</param> /// <returns>if executeString is true it will return the executeString for the item, /// otherwise it will return the help info string</returns> public static string GetCommandStringA(IContextMenu iContextMenu, uint idcmd, bool executeString) { string info = string.Empty; byte[] bytes = new byte[256]; iContextMenu.GetCommandString( idcmd, (executeString ? GCS.VerbA : GCS.HelpTextA), 0, bytes, ShellApi.MaxPath); int index = 0; while (index < bytes.Length && bytes[index] != 0) { index++; } if (index < bytes.Length) { info = Encoding.Default.GetString(bytes, 0, index); } return info; }
/// <summary> /// Retrieves the command string for a specific item from an iContextMenu (Unicode) /// </summary> /// <param name="iContextMenu">the IContextMenu to receive the string from</param> /// <param name="idcmd">the id of the specific item</param> /// <param name="executeString">indicating whether it should return an execute string or not</param> /// <returns>if executeString is true it will return the executeString for the item, /// otherwise it will return the help info string</returns> public static string GetCommandStringW(IContextMenu iContextMenu, uint idcmd, bool executeString) { string info = string.Empty; byte[] bytes = new byte[256]; iContextMenu.GetCommandString( idcmd, (executeString ? ShellAPI.GCS.VERBW : ShellAPI.GCS.HELPTEXTW), 0, bytes, ShellAPI.MAX_PATH); int index = 0; while (index < bytes.Length - 1 && (bytes[index] != 0 || bytes[index + 1] != 0)) { index += 2; } if (index < bytes.Length - 1) { info = Encoding.Unicode.GetString(bytes, 0, index + 1); } return info; }
/// <summary> /// Retrieves the command string for a specific item from an iContextMenu (Ansi) /// </summary> /// <param name="iContextMenu">the IContextMenu to receive the string from</param> /// <param name="idcmd">the id of the specific item</param> /// <param name="executeString">indicating whether it should return an execute string or not</param> /// <returns>if executeString is true it will return the executeString for the item, /// otherwise it will return the help info string</returns> public static string GetCommandStringA(IContextMenu iContextMenu, uint idcmd, bool executeString) { string info = string.Empty; byte[] bytes = new byte[256]; iContextMenu.GetCommandString( idcmd, (executeString ? ShellAPI.GCS.VERBA : ShellAPI.GCS.HELPTEXTA), 0, bytes, ShellAPI.MAX_PATH); int index = 0; while (index < bytes.Length && bytes[index] != 0) { index++; } if (index < bytes.Length) { info = Encoding.Default.GetString(bytes, 0, index); } return info; }
private string GetCommandStringA(IContextMenu iContextMenu, uint idcmd, bool executeString) { var sb = new StringBuilder(MAX_PATH); iContextMenu.GetCommandString( idcmd, executeString ? GCS.VERBA : GCS.HELPTEXTA, 0, sb, MAX_PATH); return(sb.ToString()); }
public static string[] GetVerbs(IContextMenu cm, bool ansi = true) { IntPtr menu = IntPtr.Zero; try { menu = CreateMenu(); // It isn't clear why short.MaxValue, but 0x7FFF is very used around the .NET! int res = cm.QueryContextMenu(menu, 0, 0, short.MaxValue, 0); if (res < 0) { Marshal.ThrowExceptionForHR(res); } //var sb = new StringBuilder(128); int count = GetMenuItemCount(menu); var verbs = new List <string>(count); var handle = default(GCHandle); try { var bytes = new byte[ansi ? 128 : 256]; handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); IntPtr ptr = handle.AddrOfPinnedObject(); for (int i = 0; i < count; i++) { uint id = GetMenuItemID(menu, i); if (id == uint.MaxValue) { continue; } //GetMenuString(menu, (uint)i, sb, sb.Capacity, 0x00000400 /* MF_BYPOSITION */); //string description = sb.ToString(); //sb.Clear(); res = cm.GetCommandString((UIntPtr)id, ansi ? (uint)0x00000002 /* GCS_VALIDATEA */ : 0x00000006 /* GCS_VALIDATEW */, IntPtr.Zero, ptr, bytes.Length); if (res < 0) { continue; } if (res == 0) { res = cm.GetCommandString((UIntPtr)id, ansi ? (uint)0x00000000 /* GCS_VERBA */ : 0x00000004 /* GCS_VERBW */, IntPtr.Zero, ptr, bytes.Length); if (res < 0) { Marshal.ThrowExceptionForHR(res); } verbs.Add(ansi ? Marshal.PtrToStringAnsi(ptr) : Marshal.PtrToStringUni(ptr)); } } } finally { if (handle.IsAllocated) { handle.Free(); } } return(verbs.ToArray()); } finally { if (menu != IntPtr.Zero) { DestroyMenu(menu); } } }