Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions src/Files.App.OpenDialog/FilesOpenDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,43 @@ IShellItem* CloneShellItem(IShellItem* psi)
return item;
}

std::string wstring_to_utf8_hex(const std::wstring& input)
{
std::string output;

int cbNeeded = WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, NULL, 0, NULL, NULL);
if (cbNeeded > 0)
{
char* utf8 = new char[cbNeeded];
if (WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, utf8, cbNeeded, NULL, NULL) != 0)
{
for (char* p = utf8; *p; p++)
{
char onehex[5];
sprintf_s(onehex, sizeof(onehex), "%%%02.2X", (unsigned char)*p);
output.append(onehex);
}
}

delete[] utf8;
}

return output;
}

std::wstring str2wstr(const std::string& str)
{
int cbNeeded = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
if (cbNeeded > 0)
{
std::wstring wstrTo(cbNeeded, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], cbNeeded);
return wstrTo;
}

return L"";
}

template <typename T>
CComPtr<T> AsInterface(CComPtr<IFileOpenDialog> dialog)
{
Expand Down Expand Up @@ -118,19 +155,27 @@ STDAPICALL CFilesOpenDialog::Show(HWND hwndOwner)
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.lpFile = L"files.exe";

PWSTR pszPath = NULL;
WCHAR szBuf[MAX_PATH];
TCHAR args[1024] = { 0 };
ExpandEnvironmentStrings(L"%LOCALAPPDATA%\\Microsoft\\WindowsApps\\files.exe", szBuf, MAX_PATH - 1);

HANDLE closeEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("FILEDIALOG"));

if (_initFolder && SUCCEEDED(_initFolder->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &pszPath)))
{
TCHAR args[1024];
wsprintf(args, L"-directory \"%s\" -outputpath \"%s\"", pszPath, _outputPath.c_str());
swprintf(args, _countof(args) - 1, L"\"%s\" -directory \"%s\" -outputpath \"%s\"", szBuf, pszPath, _outputPath.c_str());
wcout << L"Invoking: " << args << endl;
ShExecInfo.lpParameters = args;
CoTaskMemFree(pszPath);
}
else
{
swprintf(args, _countof(args) - 1, L"\"%s\" -outputpath \"%s\"", szBuf, _outputPath.c_str());
}

std::wstring uriWithArgs = L"files-uwp:?cmd=" + str2wstr(wstring_to_utf8_hex(args));
ShExecInfo.lpFile = uriWithArgs.c_str();
ShExecInfo.nShow = SW_SHOW;
ShellExecuteEx(&ShExecInfo);

Expand Down
55 changes: 50 additions & 5 deletions src/Files.App.SaveDialog/FilesSaveDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ IShellItem* CloneShellItem(IShellItem* psi)
return item;
}

std::string wstring_to_utf8_hex(const std::wstring& input)
{
std::string output;

int cbNeeded = WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, NULL, 0, NULL, NULL);
if (cbNeeded > 0)
{
char* utf8 = new char[cbNeeded];
if (WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, utf8, cbNeeded, NULL, NULL) != 0)
{
for (char* p = utf8; *p; p++)
{
char onehex[5];
sprintf_s(onehex, sizeof(onehex), "%%%02.2X", (unsigned char)*p);
output.append(onehex);
}
}

delete[] utf8;
}

return output;
}

std::wstring str2wstr(const std::string& str)
{
int cbNeeded = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
if (cbNeeded > 0)
{
std::wstring wstrTo(cbNeeded, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], cbNeeded);
return wstrTo;
}

return L"";
}

template <typename T>
CComPtr<T> AsInterface(CComPtr<IFileSaveDialog> dialog)
{
Expand Down Expand Up @@ -394,26 +431,34 @@ HRESULT __stdcall CFilesSaveDialog::Show(HWND hwndOwner)
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.lpFile = L"files.exe";

PWSTR pszPath = NULL;
WCHAR szBuf[MAX_PATH];
TCHAR args[1024] = { 0 };
ExpandEnvironmentStrings(L"%LOCALAPPDATA%\\Microsoft\\WindowsApps\\files.exe", szBuf, MAX_PATH - 1);

HANDLE closeEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("FILEDIALOG"));

if (_initFolder && SUCCEEDED(_initFolder->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &pszPath)))
{
TCHAR args[1024];
if (!_initName.empty())
{
wsprintf(args, L"-directory \"%s\" -outputpath \"%s\" -select \"%s\"", pszPath, _outputPath.c_str(), _initName.c_str());
swprintf(args, _countof(args) - 1, L"\"%s\" -directory \"%s\" -outputpath \"%s\" -select \"%s\"", szBuf, pszPath, _outputPath.c_str(), _initName.c_str());
}
else
{
wsprintf(args, L"-directory \"%s\" -outputpath \"%s\"", pszPath, _outputPath.c_str());
swprintf(args, _countof(args) - 1, L"\"%s\" -directory \"%s\" -outputpath \"%s\"", szBuf, pszPath, _outputPath.c_str());
}
wcout << L"Invoking: " << args << endl;
ShExecInfo.lpParameters = args;
CoTaskMemFree(pszPath);
}
else
{
swprintf(args, _countof(args) - 1, L"\"%s\" -outputpath \"%s\"", szBuf, _outputPath.c_str());
}

std::wstring uriWithArgs = L"files-uwp:?cmd=" + str2wstr(wstring_to_utf8_hex(args));
ShExecInfo.lpFile = uriWithArgs.c_str();
ShExecInfo.nShow = SW_SHOW;
ShellExecuteEx(&ShExecInfo);

Expand Down
42 changes: 25 additions & 17 deletions src/Files.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,11 @@ static bool ProcessPathPredicate(Process p)

var OpenTabInExistingInstance = ApplicationData.Current.LocalSettings.Values.Get("OpenTabInExistingInstance", true);
var activatedArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
var commandLineArgs = GetCommandLineArgs(activatedArgs);

// WINUI3: When launching from commandline the argument is not ICommandLineActivatedEventArgs (#10370)
var isCommadLineLaunch = activatedArgs.Data is ILaunchActivatedEventArgs args &&
args.Arguments is not null &&
(CommandLineParser.SplitArguments(args.Arguments, true)[0].EndsWith($"files.exe", StringComparison.OrdinalIgnoreCase)
|| CommandLineParser.SplitArguments(args.Arguments, true)[0].EndsWith($"files", StringComparison.OrdinalIgnoreCase));

if (activatedArgs.Data is ICommandLineActivatedEventArgs || isCommadLineLaunch)
if (commandLineArgs is not null)
{
var cmdLineArgs = activatedArgs.Data as ICommandLineActivatedEventArgs;
var cmdLineLaunchArgs = activatedArgs.Data as ILaunchActivatedEventArgs;
var parsedCommands = CommandLineParser.ParseUntrustedCommands(cmdLineArgs?.Operation.Arguments ?? cmdLineLaunchArgs!.Arguments);
var parsedCommands = CommandLineParser.ParseUntrustedCommands(commandLineArgs);

if (parsedCommands is not null)
{
Expand All @@ -124,11 +117,8 @@ args.Arguments is not null &&
if (!Constants.UserEnvironmentPaths.ShellPlaces.ContainsKey(command.Payload.ToUpperInvariant()))
{
OpenShellCommandInExplorer(command.Payload, Environment.ProcessId);

// Exit
return;
}

break;

default:
Expand All @@ -154,7 +144,6 @@ args.Arguments is not null &&
else if (activatedArgs.Data is ILaunchActivatedEventArgs tileArgs)
{
if (tileArgs.Arguments is not null &&
!tileArgs.Arguments.Contains($"files.exe", StringComparison.OrdinalIgnoreCase) &&
FileExtensionHelpers.IsExecutableFile(tileArgs.Arguments))
{
if (File.Exists(tileArgs.Arguments))
Expand All @@ -165,7 +154,7 @@ args.Arguments is not null &&
}
}

if (OpenTabInExistingInstance && !isCommadLineLaunch)
if (OpenTabInExistingInstance && commandLineArgs is null)
{
if (activatedArgs.Data is ILaunchActivatedEventArgs launchArgs)
{
Expand All @@ -180,8 +169,7 @@ args.Arguments is not null &&
else if (activatedArgs.Data is IProtocolActivatedEventArgs protocolArgs)
{
var parsedArgs = protocolArgs.Uri.Query.TrimStart('?').Split('=');
if ((parsedArgs.Length == 2 && parsedArgs[0] == "cmd") ||
parsedArgs.Length == 1) // Treat Win+E & Open file location as command line launch
if (parsedArgs.Length == 1)
{
var activePid = ApplicationData.Current.LocalSettings.Values.Get("INSTANCE_ACTIVE", -1);
var instance = AppInstance.FindOrRegisterForKey(activePid.ToString());
Expand Down Expand Up @@ -219,6 +207,26 @@ args.Arguments is not null &&
});
}

/// <summary>
/// Gets command line args from AppActivationArguments
/// Command line args can be ILaunchActivatedEventArgs, ICommandLineActivatedEventArgs or IProtocolActivatedEventArgs
/// </summary>
private static string? GetCommandLineArgs(AppActivationArguments activatedArgs)
{
// WINUI3: When launching from commandline the argument is not ICommandLineActivatedEventArgs (#10370)
var cmdLaunchArgs = activatedArgs.Data is ILaunchActivatedEventArgs launchArgs &&
launchArgs.Arguments is not null &&
CommandLineParser.SplitArguments(launchArgs.Arguments, true).FirstOrDefault() is string arg0 &&
(arg0.EndsWith($"files.exe", StringComparison.OrdinalIgnoreCase) ||
arg0.EndsWith($"files", StringComparison.OrdinalIgnoreCase)) ? launchArgs.Arguments : null;
var cmdProtocolArgs = activatedArgs.Data is IProtocolActivatedEventArgs protocolArgs &&
protocolArgs.Uri.Query.TrimStart('?').Split('=') is string[] parsedArgs &&
parsedArgs.Length == 2 && parsedArgs[0] == "cmd" ? Uri.UnescapeDataString(parsedArgs[1]) : null;
var cmdLineArgs = activatedArgs.Data is ICommandLineActivatedEventArgs cmdArgs ? cmdArgs.Operation.Arguments : null;

return cmdLaunchArgs ?? cmdProtocolArgs ?? cmdLineArgs;
}

/// <summary>
/// Gets invoked when the application is activated.
/// </summary>
Expand Down