Skeketon project in Visual Studio 2015 references InteropServices Runtime Version: v4.0.30319 Version: 4.1.1.0 installed from nuget.org:
System.Runtime.InteropServices by: dotnetframework Microsoft .NET 5.0 This package is compatible with .NET 5.0 or higher. .NET Core 1.0 This package is compatible with .NET Core 1.0 or higher. .NET Standard 1.1 This package targets .NET Standard 1.1. The package is compatible with this framework or higher. .NET Framework 4.5 This package targets .NET Framework 4.5. The package is compatible with this framework or higher. 2,556,296,558 total downloads last updated 11/15/2016 Latest version: 4.3.0
The Class.cs:
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ExplorerExtension
{
[ComVisible(true)]
[Guid("257F2702-F2F4-4549-BE83-EA74F2BDC160")]
public class MyExplorerExtension : IShellExtInit, IContextMenu
{
// Implement IShellExtInit
public void Initialize(IntPtr pidlFolder, IntPtr pDataObj, IntPtr hKeyProgID)
{
// Initialization code here
}
// Implement IContextMenu
public void QueryContextMenu(IntPtr hMenu, uint indexMenu, uint idCmdFirst, uint idCmdLast, uint uFlags)
{
// Add custom menu items here
}
public void InvokeCommand(IntPtr pici)
{
// Handle menu item click here
}
public void GetCommandString(uint idCmd, uint uType, uint pReserved, StringBuilder pszName, uint cchMax)
{
// Provide help text for menu items here
}
}
}
The App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/></startup></configuration>
The Packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Runtime" version="4.3.0" targetFramework="net48" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net471" />
</packages>
Gives the following errors:
Error CS0246 The type or namespace name 'IShellExtInit' could not be found (are you missing a using directive or an assembly reference?) Error CS0246 The type or namespace name 'IContextMenu' could not be found (are you missing a using directive or an assembly reference?)
I found a good example of a shell extension context menu handler on: github
It is a modification of a Microsoft example here
To get it to comple under Visual Studio 2022, I had to add:
#include <string>
to FileContextMenuExt.cpp
and add:
#ifndef FILECONTEXTMENUEXT_H
#define FILECONTEXTMENUEXT_H
...existing code
#endif // FILECONTEXTMENUEXT_H
to FileContextMenuExt.h
Using C++ avoids the issue of managed code called from native code. Using C# was not a requirement. It also allowed me to avoid delving into Microsoft's version and renaming quagmire...