You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++批量写入JSON条目到剪贴板时被覆盖的问题解决

问题:剪贴板历史仅保留最后一条JSON条目,其余被覆盖

我正在编写程序,将JSON数组中的所有对象复制到剪贴板,但打开剪贴板历史记录时仅显示文件中的最后一个对象,前面的条目不断被覆盖。

初始代码

void loadOldClipboard() {
    // Load the JSON data from our File
    std::ifstream file("pastEntries.json");
    Json::Value root;
    file >> root;
    file.close();
    OpenClipboard(0);
    EmptyClipboard();
    for (const auto& obj : root) {
        std::string entry = obj["clipboardEntry"].asString();
        std::cout << "clipboardEntry: " << entry << std::endl;

        // Allocate global memory for the clipboard data
        HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, entry.size() + 1);
        if (hMem == NULL) {
            // Handle memory allocation error
            CloseClipboard();
            return;
        }

        // Copy the entry to the global memory
        char* pMem = static_cast<char*>(GlobalLock(hMem));
        if (pMem == NULL) {
            // Handle memory locking error
            GlobalFree(hMem);
            CloseClipboard();
            return;
        }
        strcpy_s(pMem, entry.size() + 1, entry.c_str());
        GlobalUnlock(hMem);

        // Set the clipboard data for the current entry
        SetClipboardData(CF_TEXT, hMem);
    }
    CloseClipboard();
}

JSON文件内容

[
    {
        "clipboardEntry": "Entry 1"
    },
    {
        "clipboardEntry": "Entry 2"
    },
    {
        "clipboardEntry": "Entry 3"
    }
]

尝试更新代码

更新:我尝试在每次循环中打开和关闭剪贴板,但问题依旧:

void loadOldClipboard() {
    // Load the JSON data from our File
    std::ifstream file("pastEntries.json");
    Json::Value root;
    file >> root;
    file.close();

    // Allocate global memory for each string and copy it to the clipboard
    for (const auto& text : root) {
        if (!OpenClipboard(0)) {
            std::cerr << "Failed to open clipboard\n";
            return;
        }

        // Empty the clipboard
        EmptyClipboard();

        std::string entry = text["clipboardEntry"].asString();
        std::cout << "clipboardEntry: " << entry << std::endl;
        // Allocate global memory for the clipboard data
        HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, entry.size() + 1);
        if (hMem == NULL) {
            // Handle memory allocation error
            CloseClipboard();
            return;
        }

        // Copy the text to the global memory
        char* pMem = static_cast<char*>(GlobalLock(hMem));
        if (pMem == NULL) {
            // Handle memory locking error
            GlobalFree(hMem);
            CloseClipboard();
            return;
        }
        strcpy_s(pMem, entry.size() + 1, entry.c_str());
        GlobalUnlock(hMem);

        // Set the clipboard data
        SetClipboardData(CF_TEXT, hMem);

        // Close the clipboard
        CloseClipboard();
    }
}

问题解决

更新:问题已解决,按照@Halfix的建议,需要在每次剪贴板操作后添加约0.5-1秒的Sleep()延迟,否则程序无法正常处理。


内容的提问来源于stack exchange,提问作者Mario Rütsche

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 09:43:30