-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IShellDispatch2.ShellExecute causes NotSupportedException: BSTR cannot be marshalled to a Variant #862
Comments
@JeremyKuhne I know you've done a lot of work with VARIANT. The documented parameter type for |
When I tried to repro this, I got a different error:
Program.cs using System;
using Windows.Win32.UI.Shell;
using Windows.Win32;
using System.Runtime.InteropServices;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
using IServiceProvider = Windows.Win32.System.Com.IServiceProvider;
ProcessUtils.StartProcessWithoutElevation("notepad.exe", @"c:\temp");
internal static class ProcessUtils
{
public static void StartProcessWithoutElevation(string fileName, string workingDirectory)
{
var shellWindows = (IShellWindows)new ShellWindows();
var serviceProvider = (IServiceProvider)shellWindows.FindWindowSW(
PInvoke.CSIDL_DESKTOP,
pvarLocRoot: null,
ShellWindowTypeConstants.SWC_DESKTOP,
phwnd: out _,
ShellWindowFindWindowOptions.SWFO_NEEDDISPATCH);
serviceProvider.QueryService(PInvoke.SID_STopLevelBrowser, typeof(IShellBrowser).GUID, out object shellBrowserObject);
IShellBrowser shellBrowser = (IShellBrowser)shellBrowserObject;
shellBrowser.QueryActiveShellView(out var shellView);
shellView.GetItemObject((uint)_SVGIO.SVGIO_BACKGROUND, typeof(IDispatch).GUID, out var folderViewAsObject);
var folderView = (IShellFolderViewDual)folderViewAsObject;
var shellDispatch = (IShellDispatch2)folderView.Application;
var fileNameAsBstr = (BSTR)Marshal.StringToBSTR(fileName);
try
{
shellDispatch.ShellExecute(File: fileNameAsBstr, vArgs: null, vDir: workingDirectory, vOperation: "", SHOW_WINDOW_CMD.SW_NORMAL);
}
finally
{
Marshal.FreeBSTR(fileNameAsBstr);
}
}
[ComImport, Guid("00020400-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
private interface IDispatch
{
}
} NativeMethods.txt:
|
I can still repro with exactly the same original message on 0.2.188-beta, but everything is working properly on 0.3.49-beta. It was fixed by #1050. I confirmed that on 0.2.188-beta by using a copy of the generated interface changed to InterfaceIsDual. |
https://learn.microsoft.com/en-us/windows/win32/shell/ishelldispatch2-shellexecute
Repro steps
NativeMethods.txt
content:NativeMethods.json
content (if present):Context
LangVersion
(if explicitly set by project): latestThe text was updated successfully, but these errors were encountered: