Improve: ExpandDirectories after GetForegroundSelectedFiles

This commit is contained in:
yuanyuanxiang
2026-01-17 00:15:05 +01:00
parent 39e07adb3b
commit 1611ddd869
5 changed files with 104 additions and 21 deletions

View File

@@ -619,7 +619,7 @@ VOID CScreenManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
}
if (SendClientClipboard(ulLength > 1))
break;
files = GetForegroundSelectedFiles();
files = GetForegroundSelectedFiles(result);
if (!files.empty()) {
char h[100] = {};
memcpy(h, szBuffer + 1, ulLength - 1);
@@ -631,7 +631,7 @@ VOID CScreenManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
memcpy(szBuffer + 1, str.data(), str.size());
SendData(szBuffer, 1 + str.size());
SAFE_DELETE_ARRAY(szBuffer);
}
}
break;
}
case COMMAND_SCREEN_SET_CLIPBOARD: {

View File

@@ -12,41 +12,93 @@
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
static std::vector<std::string> GetDesktopSelectedFiles()
void ExpandDirectory(const std::string& dir, std::vector<std::string>& result) {
std::string searchPath = dir + "\\*";
WIN32_FIND_DATAA fd;
HANDLE hFind = FindFirstFileA(searchPath.c_str(), &fd);
if (hFind == INVALID_HANDLE_VALUE) return;
do {
if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
continue;
std::string fullPath = dir + "\\" + fd.cFileName;
result.push_back(fullPath); // 文件和目录都加入
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
ExpandDirectory(fullPath, result); // 递归
}
} while (FindNextFileA(hFind, &fd));
FindClose(hFind);
}
std::vector<std::string> ExpandDirectories(const std::vector<std::string>& selected) {
std::vector<std::string> result;
for (const auto& path : selected) {
DWORD attr = GetFileAttributesA(path.c_str());
if (attr == INVALID_FILE_ATTRIBUTES) continue;
result.push_back(path); // 先加入自身
if (attr & FILE_ATTRIBUTE_DIRECTORY) {
ExpandDirectory(path, result);
}
}
return result;
}
static std::vector<std::string> GetDesktopSelectedFiles(int& result)
{
CComPtr<IShellWindows> pShellWindows;
if (FAILED(pShellWindows.CoCreateInstance(CLSID_ShellWindows)))
if (FAILED(pShellWindows.CoCreateInstance(CLSID_ShellWindows))) {
result = 101;
return {};
}
CComVariant vLoc(CSIDL_DESKTOP);
CComVariant vEmpty;
long lhwnd;
CComPtr<IDispatch> pDisp;
if (FAILED(pShellWindows->FindWindowSW(&vLoc, &vEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &pDisp)))
if (FAILED(pShellWindows->FindWindowSW(&vLoc, &vEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &pDisp))) {
result = 102;
return {};
}
CComQIPtr<IServiceProvider> pServiceProvider(pDisp);
if (!pServiceProvider)
if (!pServiceProvider) {
result = 103;
return {};
}
CComPtr<IShellBrowser> pShellBrowser;
if (FAILED(pServiceProvider->QueryService(SID_STopLevelBrowser, IID_IShellBrowser, (void**)&pShellBrowser)))
if (FAILED(pServiceProvider->QueryService(SID_STopLevelBrowser, IID_IShellBrowser, (void**)&pShellBrowser))) {
result = 104;
return {};
}
CComPtr<IShellView> pShellView;
if (FAILED(pShellBrowser->QueryActiveShellView(&pShellView)))
if (FAILED(pShellBrowser->QueryActiveShellView(&pShellView))) {
result = 105;
return {};
}
CComPtr<IDataObject> pDataObject;
if (FAILED(pShellView->GetItemObject(SVGIO_SELECTION, IID_IDataObject, (void**)&pDataObject)))
if (FAILED(pShellView->GetItemObject(SVGIO_SELECTION, IID_IDataObject, (void**)&pDataObject))) {
result = 106;
return {};
}
FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = {};
if (FAILED(pDataObject->GetData(&fmt, &stg)))
if (FAILED(pDataObject->GetData(&fmt, &stg))) {
result = 107;
return {};
}
std::vector<std::string> vecFiles;
HDROP hDrop = (HDROP)GlobalLock(stg.hGlobal);
@@ -65,11 +117,13 @@ static std::vector<std::string> GetDesktopSelectedFiles()
}
ReleaseStgMedium(&stg);
vecFiles = ExpandDirectories(vecFiles);
return vecFiles;
}
std::vector<std::string> GetForegroundSelectedFiles()
std::vector<std::string> GetForegroundSelectedFiles(int& result)
{
result = 0;
HRESULT hrInit = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
bool bNeedUninit = SUCCEEDED(hrInit);
@@ -90,7 +144,7 @@ std::vector<std::string> GetForegroundSelectedFiles()
if (hFore == hDesktop || hFore == hWorkerW) {
if (bNeedUninit) CoUninitialize();
return GetDesktopSelectedFiles();
return GetDesktopSelectedFiles(result);
}
// 检查是否是资源管理器窗口
@@ -99,6 +153,7 @@ std::vector<std::string> GetForegroundSelectedFiles()
if (strcmp(szClass, "CabinetWClass") != 0 && strcmp(szClass, "ExploreWClass") != 0) {
if (bNeedUninit) CoUninitialize();
result = 1;
return {};
}
@@ -106,6 +161,7 @@ std::vector<std::string> GetForegroundSelectedFiles()
CComPtr<IShellWindows> pShellWindows;
if (FAILED(pShellWindows.CoCreateInstance(CLSID_ShellWindows))) {
if (bNeedUninit) CoUninitialize();
result = 2;
return {};
}
@@ -130,16 +186,22 @@ std::vector<std::string> GetForegroundSelectedFiles()
continue;
CComPtr<IDispatch> pDoc;
if (FAILED(pBrowser->get_Document(&pDoc)) || !pDoc)
if (FAILED(pBrowser->get_Document(&pDoc)) || !pDoc) {
result = 3;
break;
}
CComQIPtr<IShellFolderViewDual> pView(pDoc);
if (!pView)
if (!pView) {
result = 4;
break;
}
CComPtr<FolderItems> pItems;
if (FAILED(pView->SelectedItems(&pItems)) || !pItems)
if (FAILED(pView->SelectedItems(&pItems)) || !pItems) {
result = 5;
break;
}
long nItems = 0;
pItems->get_Count(&nItems);
@@ -169,6 +231,7 @@ std::vector<std::string> GetForegroundSelectedFiles()
if (bNeedUninit) CoUninitialize();
vecFiles = ExpandDirectories(vecFiles);
return vecFiles;
}

View File

@@ -42,4 +42,4 @@ std::vector<char> BuildMultiStringPath(const std::vector<std::string>& paths);
std::vector<std::string> ParseMultiStringPath(const char* buffer, size_t size);
std::vector<std::string> GetForegroundSelectedFiles();
std::vector<std::string> GetForegroundSelectedFiles(int &result);

View File

@@ -4347,6 +4347,19 @@ void CMy2015RemoteDlg::RemoveRemoteWindow(HWND wnd)
LeaveCriticalSection(&m_cs);
}
void CMy2015RemoteDlg::UpdateActiveRemoteSession(CDialogBase *sess){
EnterCriticalSection(&m_cs);
m_pActiveSession = sess;
LeaveCriticalSection(&m_cs);
}
CDialogBase* CMy2015RemoteDlg::GetActiveRemoteSession() {
EnterCriticalSection(&m_cs);
auto sess = m_pActiveSession;
LeaveCriticalSection(&m_cs);
return sess;
}
LRESULT CALLBACK CMy2015RemoteDlg::LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (g_2015RemoteDlg == NULL)
@@ -4431,7 +4444,7 @@ LRESULT CALLBACK CMy2015RemoteDlg::LowLevelKeyboardProc(int nCode, WPARAM wParam
HWND hFore = ::GetForegroundWindow();
operateWnd = g_2015RemoteDlg->GetRemoteWindow(hFore);
if (!operateWnd)
g_2015RemoteDlg->m_pActiveSession = nullptr;
g_2015RemoteDlg->UpdateActiveRemoteSession(nullptr);
}
// 检测 Ctrl+V
else if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && pKey->vkCode == 'V') {
@@ -4447,6 +4460,7 @@ LRESULT CALLBACK CMy2015RemoteDlg::LowLevelKeyboardProc(int nCode, WPARAM wParam
// [1] 本地 -> 远程
int result;
auto files = GetClipboardFiles(result);
if (files.empty()) files = GetForegroundSelectedFiles(result);
if (!files.empty()) {
// 获取远程目录
auto str = BuildMultiStringPath(files);
@@ -4479,8 +4493,12 @@ LRESULT CALLBACK CMy2015RemoteDlg::LowLevelKeyboardProc(int nCode, WPARAM wParam
Mprintf("【Ctrl+V】 本地剪贴板没有文本或文件: %d \n", result);
}
}
} else if (g_2015RemoteDlg->m_pActiveSession && operateWnd) {
auto screen = (CScreenSpyDlg*)(g_2015RemoteDlg->m_pActiveSession);
} else if (g_2015RemoteDlg->GetActiveRemoteSession() && operateWnd) {
auto screen = (CScreenSpyDlg*)(g_2015RemoteDlg->GetActiveRemoteSession());
if (!screen) {
Mprintf("【Ctrl+V】 [远程 -> 本地] 远程桌面窗口状态已经失效\n");
break;
}
if (!screen->m_bIsCtrl) {
Mprintf("【Ctrl+V】 [远程 -> 本地] 窗口不是控制状态: %s\n", screen->m_IPAddress);
break;
@@ -4494,7 +4512,7 @@ LRESULT CALLBACK CMy2015RemoteDlg::LowLevelKeyboardProc(int nCode, WPARAM wParam
EmptyClipboard();
CloseClipboard();
}
if (g_2015RemoteDlg->m_pActiveSession->m_ContextObject->Send2Client(bToken, sizeof(bToken)))
if (screen->m_ContextObject->Send2Client(bToken, sizeof(bToken)))
Sleep(200);
Mprintf("【Ctrl+V】 从远程拷贝到本地 \n");
} else {
@@ -4512,7 +4530,7 @@ LRESULT CALLBACK CMy2015RemoteDlg::LowLevelKeyboardProc(int nCode, WPARAM wParam
LRESULT CMy2015RemoteDlg::OnSessionActivatedMsg(WPARAM wParam, LPARAM lParam)
{
CDialogBase* pSession = reinterpret_cast<CDialogBase*>(wParam);
m_pActiveSession = pSession;
UpdateActiveRemoteSession(pSession);
return 0;
}

View File

@@ -279,6 +279,8 @@ public:
CDialogBase* GetRemoteWindow(CDialogBase* dlg);
void RemoveRemoteWindow(HWND wnd);
CDialogBase* m_pActiveSession = nullptr; // 当前活动会话窗口指针 / NULL 表示无
void UpdateActiveRemoteSession(CDialogBase* sess);
CDialogBase* GetActiveRemoteSession();
afx_msg LRESULT OnSessionActivatedMsg(WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
HHOOK g_hKeyboardHook = NULL;