使用 Node.js C++ Addon 获取资源管理器当前目录路径

本示例演示如何使用 Win32 API 和 Node.js C++ Addon 获取当前处于前台的资源管理器窗口的目录路径,并将路径返回为 char* 类型。

实现思路

  1. 创建 IShellWindows 对象,枚举所有打开的资源管理器窗口。
  2. 使用相关接口获取每个资源管理器窗口的窗口句柄和当前文件夹的 PIDL。
  3. 如果窗口句柄等于 GetForegroundWindow() 返回的句柄,则将 PIDL 转换为路径。

代码实现

#include <node.h>
#include <windows.h>
#include <shlobj.h>

#pragma comment(lib, "shell32.lib")

using namespace v8;

// 获取当前前台窗口句柄
HWND GetForegroundWindowHandle()
{
    return GetForegroundWindow();
}

// 获取资源管理器路径
char* GetExplorerPath()
{
    // 创建 IShellWindows 对象
    IShellWindows* psw;
    HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_IShellWindows, (void**)&psw);
    if (FAILED(hr))
    {
        return NULL;
    }

    // 枚举所有资源管理器窗口
    VARIANT vEmpty = {};
    VARIANT vIndex;
    vIndex.vt = VT_I4;
    IDispatch* pdisp;
    IWebBrowserApp* pwb;
    char* path = NULL;
    long lCount;
    hr = psw->get_Count(&lCount);
    if (FAILED(hr))
    {
        return NULL;
    }
    for (vIndex.lVal = 0; vIndex.lVal < lCount; vIndex.lVal++)
    {
        hr = psw->Item(vIndex, &pdisp);
        if (FAILED(hr))
        {
            continue;
        }

        // 获取窗口句柄和 PIDL
        hr = pdisp->QueryInterface(IID_IWebBrowserApp, (void**)&pwb);
        if (FAILED(hr))
        {
            continue;
        }
        HWND hwnd;
        hr = pwb->get_HWND((LONG_PTR*)&hwnd);
        if (FAILED(hr))
        {
            continue;
        }
        if (hwnd != GetForegroundWindowHandle())
        {
            continue;
        }
        IDispatch* pdispDoc;
        hr = pwb->get_Document(&pdispDoc);
        if (FAILED(hr))
        {
            continue;
        }
        IShellFolderViewDual* psvd;
        hr = pdispDoc->QueryInterface(IID_IShellFolderViewDual, (void**)&psvd);
        if (FAILED(hr))
        {
            continue;
        }
        BSTR bstrPath;
        hr = psvd->get_Folder(&bstrPath);
        if (FAILED(hr))
        {
            continue;
        }
    
        // 转换 PIDL 为路径
        LPITEMIDLIST pidl;
        ULONG chEaten;
        ULONG dwAttributes;
        SHParseDisplayName(bstrPath, NULL, &pidl, 0, &dwAttributes);
        SHGetPathFromIDList(pidl, path);
        CoTaskMemFree(pidl);
        SysFreeString(bstrPath);
    
        // 转换路径为 char*
        int len = WideCharToMultiByte(CP_ACP, 0, path, -1, NULL, 0, NULL, NULL);
        path = new char[len];
        WideCharToMultiByte(CP_ACP, 0, path, -1, path, len, NULL, NULL);
        break;
    }
    psw->Release();
    return path;
}

void GetExplorerPath(const FunctionCallbackInfo<Value>& args)
{
    Isolate* isolate = args.GetIsolate();
    char* path = GetExplorerPath();
    if (path != NULL)
    {
        args.GetReturnValue().Set(String::NewFromUtf8(isolate, path));
        delete path;
    }
    else
    {
        args.GetReturnValue().SetNull();
    }
}

void Init(Local<Object> exports)
{
    NODE_SET_METHOD(exports, "getExplorerPath", GetExplorerPath);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

使用方法

  1. 将代码保存为 .cpp 文件,例如 explorer_path.cpp。
  2. 使用 node-gyp 编译为 node.js 模块。
  3. 在 Node.js 代码中引入模块并调用 getExplorerPath() 函数,即可获取当前处于前台的资源管理器窗口的目录路径。
const explorerPath = require('./explorer_path');

const path = explorerPath.getExplorerPath();

console.log(path); // 输出当前资源管理器窗口的路径

注意

  • 该代码仅适用于 Windows 操作系统。
  • 该代码依赖于 Win32 API 和 COM 库。
  • 在使用该代码之前,需要确保已正确安装 Node.js 和 node-gyp。
  • 该代码仅获取当前处于前台的资源管理器窗口的路径,如果有多个资源管理器窗口,则可能无法获取到期望的路径。
  • 使用完该函数后,需要手动释放 path 指针。
  • 该代码仅提供参考,需要根据实际情况进行修改和完善。

总结

本示例演示了如何使用 Node.js C++ Addon 获取当前处于前台的资源管理器窗口的目录路径,可以用于各种场景,例如自动化脚本、文件处理等。

Node.js C++ 获取资源管理器当前目录路径

原文地址: https://www.cveoy.top/t/topic/oFgO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录