remove unused

This commit is contained in:
Leilei Zhang
2025-10-16 12:48:32 +08:00
parent db2b6c72b9
commit 0016160cc5
4 changed files with 79 additions and 154 deletions

View File

@@ -155,4 +155,4 @@ MakeAppx.exe pack /d . /p $(OutDir)ImageResizerContextMenuPackage.msix /nv</Comm
<Error Condition="!Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets'))" />
<Error Condition="!Exists('..\..\..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.231216.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.231216.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
</Target>
</Project>
</Project>

View File

@@ -101,16 +101,16 @@ HRESULT CContextMenuHandler::QueryContextMenu(_In_ HMENU hmenu, UINT indexMenu,
AssocGetPerceivedType(pszExt, &type, &flag, NULL);
free(pszPath);
bool dragDropFlag = false;
// If selected file is an image...
if (type == PERCEIVED_TYPE_IMAGE)
{
HRESULT hr = E_UNEXPECTED;
wchar_t strResizePictures[128] = {};
wchar_t strResizePictures[128] = { 0 };
// If handling drag-and-drop...
if (m_pidlFolder)
{
dragDropFlag = true;
dragDropFlag=true;
wcscpy_s(strResizePictures, ARRAYSIZE(strResizePictures), context_menu_caption_here.c_str());
}
else
@@ -118,15 +118,13 @@ HRESULT CContextMenuHandler::QueryContextMenu(_In_ HMENU hmenu, UINT indexMenu,
wcscpy_s(strResizePictures, ARRAYSIZE(strResizePictures), context_menu_caption.c_str());
}
MENUITEMINFO mii{};
MENUITEMINFO mii;
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STATE | MIIM_STRING;
mii.fType = MFT_STRING;
mii.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_STATE;
mii.wID = idCmdFirst + ID_RESIZE_PICTURES;
mii.dwTypeData = strResizePictures;
mii.cch = ARRAYSIZE(strResizePictures);
mii.fType = MFT_STRING;
mii.dwTypeData = (PWSTR)strResizePictures;
mii.fState = MFS_ENABLED;
HICON hIcon = static_cast<HICON>(LoadImage(g_hInst_imageResizer, MAKEINTRESOURCE(IDI_RESIZE_PICTURES), IMAGE_ICON, 16, 16, 0));
if (hIcon)
{
@@ -141,14 +139,21 @@ HRESULT CContextMenuHandler::QueryContextMenu(_In_ HMENU hmenu, UINT indexMenu,
if (dragDropFlag)
{
// Insert the menu entry at indexMenu+1 since the first entry should be "Copy here"
indexMenu++;
}
else
{
MENUITEMINFO miiExisting{};
miiExisting.cbSize = sizeof(MENUITEMINFO);
// indexMenu gets the first possible menu item index based on the location of the shellex registry key.
// If the registry entry is under SystemFileAssociations for the image formats, ShellImagePreview (in Windows by default) will be at indexMenu=0
// Shell ImagePreview consists of 4 menu items, a separator, Rotate right, Rotate left, and another separator
// Check if the entry at indexMenu is a separator, insert the new menu item at indexMenu+1 if true
MENUITEMINFO miiExisting;
miiExisting.dwTypeData = NULL;
miiExisting.fMask = MIIM_TYPE;
if (GetMenuItemInfo(hmenu, indexMenu, TRUE, &miiExisting) && miiExisting.fType == MFT_SEPARATOR)
miiExisting.cbSize = sizeof(MENUITEMINFO);
GetMenuItemInfo(hmenu, indexMenu, TRUE, &miiExisting);
if (miiExisting.fType == MFT_SEPARATOR)
{
indexMenu++;
}
@@ -168,7 +173,6 @@ HRESULT CContextMenuHandler::QueryContextMenu(_In_ HMENU hmenu, UINT indexMenu,
{
hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 1);
}
return hr;
}
@@ -231,146 +235,83 @@ HRESULT CContextMenuHandler::ResizePictures(CMINVOKECOMMANDINFO* pici, IShellIte
{
// Set the application path based on the location of the dll
std::wstring path = get_module_folderpath(g_hInst_imageResizer);
path += L"\\PowerToys.ImageResizer.exe";
LPTSTR lpApplicationName = path.data();
path = path + L"\\PowerToys.ImageResizer.exe";
LPTSTR lpApplicationName = &path[0];
// Create an anonymous pipe to stream filenames
SECURITY_ATTRIBUTES sa;
HANDLE hReadPipe;
HANDLE hWritePipe;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HRESULT hr = E_FAIL;
GUID pipeGuid{};
if (FAILED(CoCreateGuid(&pipeGuid)))
if (!CreatePipe(&hReadPipe, &hWritePipe, &sa, 0))
{
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
wchar_t guidBuffer[64] = {};
StringFromGUID2(pipeGuid, guidBuffer, ARRAYSIZE(guidBuffer));
std::wstring pipeName = L"\\\\.\\pipe\\PowerToysImageResizer_";
std::wstring guidString(guidBuffer);
if (guidString.length() > 2)
if (!SetHandleInformation(hWritePipe, HANDLE_FLAG_INHERIT, 0))
{
pipeName.append(guidString.substr(1, guidString.length() - 2));
}
else
{
pipeName.append(guidString);
}
HANDLE hNamedPipe = CreateNamedPipeW(
pipeName.c_str(),
PIPE_ACCESS_OUTBOUND,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1,
0,
0,
0,
nullptr);
if (hNamedPipe == INVALID_HANDLE_VALUE)
{
return HRESULT_FROM_WIN32(GetLastError());
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
CAtlFile writePipe(hWritePipe);
CString commandLine;
commandLine.Format(_T("\"%s\""), lpApplicationName);
CString arguments;
// Set the output directory
if (m_pidlFolder)
{
TCHAR szFolder[MAX_PATH];
if (SHGetPathFromIDList(m_pidlFolder, szFolder))
{
arguments.AppendFormat(_T("/d \"%s\""), szFolder);
}
SHGetPathFromIDList(m_pidlFolder, szFolder);
commandLine.AppendFormat(_T(" /d \"%s\""), szFolder);
}
CString pipeArgument(pipeName.c_str());
if (!arguments.IsEmpty())
{
arguments.Append(_T(" "));
}
arguments.Append(pipeArgument);
int nSize = commandLine.GetLength() + 1;
LPTSTR lpszCommandLine = new TCHAR[nSize];
_tcscpy_s(lpszCommandLine, nSize, commandLine);
if (!arguments.IsEmpty())
{
commandLine.Append(_T(" "));
commandLine.Append(arguments);
}
STARTUPINFO startupInfo{};
STARTUPINFO startupInfo;
ZeroMemory(&startupInfo, sizeof(STARTUPINFO));
startupInfo.cb = sizeof(STARTUPINFO);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = pici ? static_cast<WORD>(pici->nShow) : SW_SHOWNORMAL;
PROCESS_INFORMATION processInformation{};
bool launchSucceeded = false;
// Try MSIX sparse package first
std::wstring packageFamilyName;
#if !defined(CIBUILD)
packageFamilyName = L"djwsxzxb4ksa8";
#else
packageFamilyName = L"8wekyb3d8bbwe";
#endif
std::wstring aumidTarget = L"shell:AppsFolder\\Microsoft.PowerToys.SparseApp_" + packageFamilyName + L"!PowerToys.ImageResizerUI";
SHELLEXECUTEINFOW sei{ sizeof(sei) };
sei.fMask = SEE_MASK_FLAG_NO_UI;
sei.lpVerb = L"open";
sei.lpFile = aumidTarget.c_str();
sei.lpParameters = arguments.IsEmpty() ? nullptr : arguments.GetString();
sei.nShow = pici ? static_cast<WORD>(pici->nShow) : SW_SHOWNORMAL;
if (ShellExecuteExW(&sei) && reinterpret_cast<INT_PTR>(sei.hInstApp) > 32)
startupInfo.hStdInput = hReadPipe;
startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if (pici)
{
launchSucceeded = true;
startupInfo.wShowWindow = static_cast<WORD>(pici->nShow);
}
else
{
LPWSTR mutableCommandLine = commandLine.GetBuffer();
BOOL created = CreateProcess(
nullptr,
mutableCommandLine,
nullptr,
nullptr,
FALSE,
0,
nullptr,
nullptr,
&startupInfo,
&processInformation);
commandLine.ReleaseBuffer();
if (!created)
{
hr = HRESULT_FROM_WIN32(GetLastError());
CloseHandle(hNamedPipe);
return hr;
}
CloseHandle(processInformation.hProcess);
CloseHandle(processInformation.hThread);
launchSucceeded = true;
startupInfo.wShowWindow = SW_SHOWNORMAL;
}
if (!launchSucceeded)
{
CloseHandle(hNamedPipe);
return E_FAIL;
}
PROCESS_INFORMATION processInformation;
BOOL connected = ConnectNamedPipe(hNamedPipe, nullptr);
if (!connected && GetLastError() != ERROR_PIPE_CONNECTED)
// Start the resizer
CreateProcess(
NULL,
lpszCommandLine,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&startupInfo,
&processInformation);
delete[] lpszCommandLine;
if (!CloseHandle(processInformation.hProcess))
{
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
if (!CloseHandle(processInformation.hThread))
{
hr = HRESULT_FROM_WIN32(GetLastError());
CloseHandle(hNamedPipe);
return hr;
}
CAtlFile writePipe;
writePipe.Attach(hNamedPipe);
hNamedPipe = INVALID_HANDLE_VALUE;
// psiItemArray is NULL if called from InvokeCommand. This part is used for the MSI installer. It is not NULL if it is called from Invoke (MSIX).
if (!psiItemArray)
@@ -382,49 +323,33 @@ HRESULT CContextMenuHandler::ResizePictures(CMINVOKECOMMANDINFO* pici, IShellIte
CString fileName(i.CurrentItem());
fileName.Append(_T("\r\n"));
hr = writePipe.Write(fileName, fileName.GetLength() * sizeof(TCHAR));
if (FAILED(hr))
{
writePipe.Close();
return hr;
}
writePipe.Write(fileName, fileName.GetLength() * sizeof(TCHAR));
}
}
else
{
// m_pdtobj will be NULL when invoked from the MSIX build as Initialize is never called (IShellExtInit functions aren't called in case of MSIX).
//m_pdtobj will be NULL when invoked from the MSIX build as Initialize is never called (IShellExtInit functions aren't called in case of MSIX).
DWORD fileCount = 0;
// Gets the list of files currently selected using the IShellItemArray
psiItemArray->GetCount(&fileCount);
// Iterate over the list of files
for (DWORD i = 0; i < fileCount; i++)
{
IShellItem* shellItem;
psiItemArray->GetItemAt(i, &shellItem);
LPWSTR itemName;
// Retrieves the entire file system path of the file from its shell item
shellItem->GetDisplayName(SIGDN_FILESYSPATH, &itemName);
CString fileName(itemName);
fileName.Append(_T("\r\n"));
hr = writePipe.Write(fileName, fileName.GetLength() * sizeof(TCHAR));
if (FAILED(hr))
{
writePipe.Close();
CoTaskMemFree(itemName);
shellItem->Release();
return hr;
}
CoTaskMemFree(itemName);
shellItem->Release();
// Write the file path into the input stream for image resizer
writePipe.Write(fileName, fileName.GetLength() * sizeof(TCHAR));
}
}
hr = writePipe.Flush();
if (FAILED(hr))
{
writePipe.Close();
return hr;
}
writePipe.Close();
return S_OK;
hr = S_OK;
return hr;
}
HRESULT __stdcall CContextMenuHandler::GetTitle(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszName)
@@ -529,4 +454,4 @@ HRESULT __stdcall CContextMenuHandler::Invoke(IShellItemArray* psiItemArray, IBi
m_etwTrace.UpdateState(false);
return hr;
}
}

View File

@@ -151,4 +151,4 @@
<Error Condition="!Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets'))" />
</Target>
</Project>
</Project>

View File

@@ -155,4 +155,4 @@ public:
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
{
return new ImageResizerModule();
}
}