Fixed access violation exception in ShellContextMenu when building a MenuItemInfo for separators (#195)

pull/211/head
dahall 2021-01-21 09:51:54 -07:00
parent affe76ff74
commit cae1050464
2 changed files with 6 additions and 6 deletions

View File

@ -16,14 +16,15 @@ namespace Vanara.Windows.Shell.Tests
[Test]
public void CreateTest()
{
using var menu = new ShellContextMenu(new ShellItem(TestCaseSources.WordDoc));
using var shi = new ShellItem(TestCaseSources.WordDoc);
using var menu = new ShellContextMenu(shi);
var items = menu.GetItems();
for (var i = 0; i < items.Length; i++)
ShowMII(items[i], i);
static void ShowMII(ShellContextMenu.MenuItemInfo mii, int c, int indent = 0)
{
TestContext.WriteLine($"{new string(' ', indent * 3)}{c + 1}) \"{mii.Text}\" (#{mii.Id}) - Type={mii.Type}; State={mii.State}; Verb={mii.Verb}; Tooltip={mii.HelpText}; IconLoc={mii.VerbIconLocation}");
System.Diagnostics.Debug.WriteLine($"{new string(' ', indent * 3)}{c + 1}) \"{mii.Text}\" (#{mii.Id}) - Type={mii.Type}; State={mii.State}; Verb={mii.Verb}; Tooltip={mii.HelpText}; IconLoc={mii.VerbIconLocation}");
for (int j = 0; j < mii.SubMenus.Length; j++)
ShowMII(mii.SubMenus[j], j, indent + 1);
}

View File

@ -331,9 +331,7 @@ namespace Vanara.Windows.Shell
private string GetCommandString(int command, GCS stringType)
{
using var mStr = new SafeCoTaskMemString(4096);
try { ComInterface.GetCommandString((IntPtr)command, stringType, default, mStr, mStr.Size / 2U).ThrowIfFailed(); }
catch { return null; }
return mStr.ToString();
return ComInterface.GetCommandString((IntPtr)command, stringType, default, mStr, (uint)mStr.Capacity).Succeeded ? mStr : null;
}
#if !NET5_0 && !NETCOREAPP3_1
@ -529,7 +527,8 @@ namespace Vanara.Windows.Shell
for (uint i = 0; i < SubMenus.Length; i++)
{
SubMenus[i] = new MenuItemInfo(hMenu, i, scm);
if (scm != null && SubMenus[i].Type.IsFlagSet(MenuItemType.MFT_STRING))
System.Diagnostics.Debug.WriteLine($"Processing submenu {i} ({SubMenus[i].Text})");
if (scm != null && SubMenus[i].Type == MenuItemType.MFT_STRING)
{
var id = SubMenus[i].Id;
SubMenus[i].Verb = scm.GetVerbForCommand(id);