Skip to content

Instantly share code, notes, and snippets.

@mix5003
Last active April 3, 2021 16:21
Show Gist options
  • Select an option

  • Save mix5003/8c233b2a4b4b4e44b04417f121138d92 to your computer and use it in GitHub Desktop.

Select an option

Save mix5003/8c233b2a4b4b4e44b04417f121138d92 to your computer and use it in GitHub Desktop.
this is Artikash/Textractor extension for receive text in threads to clipboard
#include "Extension.h"
#include <functional>
#include <thread>
#include <mutex>
#include <unordered_set>
#include <QMainWindow>
#include <QLayout>
#include <QPushButton>
#include <QListWidget>
#include <QInputDialog>
#include <QKeyEvent>
#include <QTimer>
std::mutex m;
std::unordered_set<int64_t> linkedTextHandles;
struct : QMainWindow
{
void launch()
{
auto centralWidget = new QWidget(this);
auto layout = new QHBoxLayout(centralWidget);
auto linkList = new QListWidget(centralWidget);
auto addLink = new QPushButton("Link", centralWidget);
layout->addWidget(linkList);
layout->addWidget(addLink);
connect(addLink, &QPushButton::clicked, [=]
{
bool ok1, ok2;
int from = QInputDialog::getText(this, "Merge From", "0x", QLineEdit::Normal, "", &ok1, Qt::WindowCloseButtonHint).toInt(&ok2, 16);
if (ok1 && ok2)
{
std::lock_guard<std::mutex> lock(m);
linkedTextHandles.insert(from);
linkList->addItem(QString::number(from, 16));
}
});
Unlink = [=]
{
if (linkList->currentItem())
{
QString link = linkList->currentItem()->text();
linkList->takeItem(linkList->currentRow());
std::lock_guard<std::mutex> lock(m);
linkedTextHandles.erase(link.toInt(nullptr, 16));
}
};
setCentralWidget(centralWidget);
setWindowTitle("MergeThreadToClipboard");
show();
}
void keyPressEvent(QKeyEvent* event) override
{
if (event->key() == Qt::Key_Delete) Unlink();
}
std::function<void()> Unlink;
}*window = nullptr;
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
QTimer::singleShot(0, []
{
std::lock_guard<std::mutex> lock(m);
(window = new std::remove_pointer_t<decltype(window)>)->launch();
});
break;
case DLL_PROCESS_DETACH:
if (lpReserved == NULL) // https://blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683
{
std::lock_guard<std::mutex> lock(m);
delete window;
window = nullptr;
}
break;
}
return TRUE;
}
/**
* Param sentence: sentence received by Textractor (UTF-16).
* Param sentenceInfo: contains miscellaneous info about the sentence (see README).
* Return value: whether the sentence was modified.
* Textractor will display the sentence after all extensions have had a chance to process and/or modify it.
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
*/
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
int64_t textHandle = sentenceInfo["text handle"];
for (int64_t linkedHandle : linkedTextHandles) {
if (textHandle == linkedHandle) {
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sentence.size() + 2) * sizeof(wchar_t));
memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t));
GlobalUnlock(hMem);
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMem);
CloseClipboard();
}
}
return false;
}
@mix5003
Copy link
Author

mix5003 commented Apr 3, 2021

i think it not need anymore. you should use build-in Thread Linker Extentions

@mix5003
Copy link
Author

mix5003 commented Apr 3, 2021

i think this extension didn't help you. this extension solve only Artikash/Textractor#155
if it create hook every new dialog it dose not help. because

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment