This commit is contained in:
恍兮惚兮 2024-03-29 21:22:16 +08:00
parent 0349a4a28e
commit 975f049797
7 changed files with 115 additions and 156 deletions

View File

@ -68,7 +68,6 @@
#define MenuPluginRankDown L"Down"
#define MenuPluginEnable L"Enable"
#define DefaultFont L"Arial"
#define CantLoadQtLoader L"Can't Load QtLoader.dll"
#define LblPluginNotify L"Qt Plugins Will be Loaded at Start Only."
#define LblPluginRemove L"Remove Plugins Will be Useful after Restart."
#define InvalidPlugin L"Invalid Plugin!"

View File

@ -68,7 +68,6 @@
#define MenuPluginRankDown L"下移"
#define MenuPluginEnable L"使用"
#define DefaultFont L"微软雅黑"
#define CantLoadQtLoader L"无法加载QtLoader.dll"
#define LblPluginNotify L"依赖于Qt的插件仅会在启动时载入。"
#define LblPluginRemove L"移除插件要在重启后生效。"
#define InvalidPlugin L"插件无效!"

View File

@ -1,4 +1,4 @@
add_executable(LunaHost WIN32 confighelper.cpp controls.cpp main.cpp processlistwindow.cpp LunaHost.cpp window.cpp luna.rc pluginmanager.cpp Plugin/pluginexample.cpp app.manifest)
add_executable(LunaHost WIN32 confighelper.cpp controls.cpp main.cpp processlistwindow.cpp LunaHost.cpp window.cpp luna.rc pluginmanager.cpp Plugin/pluginexample.cpp QtLoader.cpp app.manifest)
target_precompile_headers(LunaHost REUSE_FROM pch)
set_target_properties(LunaHost PROPERTIES OUTPUT_NAME "LunaHost${bitappendix}")
target_link_libraries(LunaHost comctl32 pch host ${YY_Thunks_for_WinXP})

View File

@ -4,15 +4,16 @@ target_precompile_headers(ToClipboard REUSE_FROM pch)
set_target_properties(ToClipboard PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/plugin${bitappendix}")
endif()
include(QtUtils.cmake)
msvc_registry_search()
if(Qt5_DIR)
find_qt5(Core Widgets)
#set(CMAKE_AUTOMOC ON)
add_library(QtLoader MODULE QtLoader.cpp)
#qt5_wrap_cpp(MOC_SOURCES QtLoader.h)
#target_sources(QtLoader PRIVATE ${MOC_SOURCES})
target_link_libraries(QtLoader Qt5::Widgets Qt5::Core)
set_target_properties(QtLoader PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/plugin${bitappendix}")
if(0)
include(QtUtils.cmake)
msvc_registry_search()
if(Qt5_DIR)
find_qt5(Core Widgets)
#set(CMAKE_AUTOMOC ON)
add_library(QtLoader MODULE QtLoader.cpp)
#qt5_wrap_cpp(MOC_SOURCES QtLoader.h)
#target_sources(QtLoader PRIVATE ${MOC_SOURCES})
target_link_libraries(QtLoader Qt5::Widgets Qt5::Core)
set_target_properties(QtLoader PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/plugin${bitappendix}")
endif()
endif()

View File

@ -1,28 +0,0 @@
#include<Windows.h>
#include<QApplication>
#include<QFont>
#include<QDir>
#include<thread>
#include<mutex>
#include<Shlwapi.h>
#include<filesystem>
extern "C" __declspec(dllexport) HMODULE* QtLoadLibrary(LPCWSTR* dlls,int num){
auto hdlls=new HMODULE[num];
auto mutex=CreateSemaphoreW(0,0,1,0);
std::thread([=](){
for(int i=0;i<num;i++)
QApplication::addLibraryPath(QString::fromStdWString(std::filesystem::path(dlls[i]).parent_path()));
int _=0;
QApplication app(_, nullptr);
app.setFont(QFont("MS Shell Dlg 2", 10));
for(int i=0;i<num;i++)
hdlls[i]=LoadLibraryW(dlls[i]);
ReleaseSemaphore(mutex,1,0);
app.exec();
}).detach();
WaitForSingleObject(mutex,INFINITE);
return hdlls;
}

89
LunaHost/GUI/QtLoader.cpp Normal file
View File

@ -0,0 +1,89 @@
#include<Windows.h>
#include<thread>
#include<mutex>
#include<Shlwapi.h>
#include<filesystem>
#ifndef _WIN64
#define THISCALL __thiscall
#define _CDECL __cdecl
#define fnQString_fromStdWString "?fromStdWString@QString@@SA?AV1@ABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z"
#define fnQCoreApplication_addLibraryPath "?addLibraryPath@QCoreApplication@@SAXABVQString@@@Z"
#define fnQString_dtor "??1QString@@QAE@XZ"
#define fnQApplication_ctor "??0QApplication@@QAE@AAHPAPADH@Z"
#define fnQFont_ctor "??0QFont@@QAE@ABVQString@@HH_N@Z"
#define fnQApplication_setFont "?setFont@QApplication@@SAXABVQFont@@PBD@Z"
#define fnQFont_dtor "??1QFont@@QAE@XZ"
#define fnQApplication_exec "?exec@QApplication@@SAHXZ"
#define fnQApplication_dtor "??1QApplication@@UAE@XZ"
#else
#define THISCALL __fastcall
#define _CDECL __fastcall
#define fnQString_fromStdWString "?fromStdWString@QString@@SA?AV1@AEBV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z"
#define fnQCoreApplication_addLibraryPath "?addLibraryPath@QCoreApplication@@SAXAEBVQString@@@Z"
#define fnQString_dtor "??1QString@@QEAA@XZ"
#define fnQApplication_ctor "??0QApplication@@QEAA@AEAHPEAPEADH@Z"
#define fnQFont_ctor "??0QFont@@QEAA@AEBVQString@@HH_N@Z"
#define fnQApplication_setFont "?setFont@QApplication@@SAXAEBVQFont@@PEBD@Z"
#define fnQFont_dtor "??1QFont@@QEAA@XZ"
#define fnQApplication_exec "?exec@QApplication@@SAHXZ"
#define fnQApplication_dtor "??1QApplication@@UEAA@XZ"
#endif
extern "C" __declspec(dllexport) std::vector<HMODULE>* QtLoadLibrary(std::vector<std::wstring>* dlls){
auto Qt5Widgets=LoadLibrary(L"Qt5Widgets.dll");
auto Qt5Gui=LoadLibrary(L"Qt5Gui.dll");
auto Qt5Core=LoadLibrary(L"Qt5Core.dll");
if(Qt5Core==0||Qt5Gui==0||Qt5Widgets==0)return new std::vector<HMODULE>{};
auto QString_fromStdWString=GetProcAddress(Qt5Core,fnQString_fromStdWString);
auto QCoreApplication_addLibraryPath=GetProcAddress(Qt5Core,fnQCoreApplication_addLibraryPath);
auto QString_dtor=GetProcAddress(Qt5Core,fnQString_dtor);
auto QApplication_ctor=GetProcAddress(Qt5Widgets,fnQApplication_ctor);
auto QFont_ctor=GetProcAddress(Qt5Gui,fnQFont_ctor);
auto QFont_dtor=GetProcAddress(Qt5Gui,fnQFont_dtor);
auto QApplication_setFont=GetProcAddress(Qt5Widgets,fnQApplication_setFont);
auto QApplication_exec=GetProcAddress(Qt5Widgets,fnQApplication_exec);
auto QApplication_dtor=GetProcAddress(Qt5Widgets,fnQApplication_dtor);
if(QString_fromStdWString==0||QCoreApplication_addLibraryPath==0||QString_dtor==0||QApplication_ctor==0||QFont_ctor==0||QFont_dtor==0||QApplication_setFont==0||QApplication_exec==0||QApplication_dtor==0)return {};
auto hdlls=new std::vector<HMODULE>;
auto mutex=CreateSemaphoreW(0,0,1,0);
std::thread([=](){
static void* qapp; //必须static
void* qstring;
void* qfont;
for(int i=0;i<dlls->size();i++){
auto dirname=std::filesystem::path(dlls->at(i)).parent_path().wstring();
((void* (_CDECL*)(void*,void*))QString_fromStdWString)(&qstring,&dirname);
((void(_CDECL *)(void*))QCoreApplication_addLibraryPath)(&qstring);
((void(THISCALL*)(void*))QString_dtor)(&qstring);
//QCoreApplication_addLibraryPath(QString_fromStdWString(std::filesystem::path(collectQtplugs[i]).parent_path()));
}
int _=0;
((void*(THISCALL*)(void*,int*,char**,int))QApplication_ctor)(&qapp,&_,0,331266);
std::wstring font=L"MS Shell Dlg 2";
((void* (_CDECL*)(void*,void*))QString_fromStdWString)(&qstring,&font);
((void*(THISCALL*)(void*,void*,int,int,bool))QFont_ctor)(&qfont,&qstring,10,-1,0);
((void(_CDECL*)(void*,void*))QApplication_setFont)(&qfont,0);
((void(THISCALL*)(void*))QFont_dtor)(&qfont);
((void(THISCALL*)(void*))QString_dtor)(&qstring);
for(int i=0;i<dlls->size();i++){
hdlls->push_back(LoadLibrary(dlls->at(i).c_str()));
}
ReleaseSemaphore(mutex,1,0);
((void(*)())QApplication_exec)();
((void(THISCALL*)(void*))QApplication_dtor)(&qapp);
}).detach();
WaitForSingleObject(mutex,INFINITE);
return hdlls;
}

View File

@ -26,96 +26,8 @@ std::optional<std::wstring>SelectFile(HWND hwnd,LPCWSTR lpstrFilter){
else return {};
}
#ifndef _WIN64
#define THISCALL __thiscall
#define _CDECL __cdecl
#define fnQString_fromStdWString "?fromStdWString@QString@@SA?AV1@ABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z"
#define fnQCoreApplication_addLibraryPath "?addLibraryPath@QCoreApplication@@SAXABVQString@@@Z"
#define fnQString_dtor "??1QString@@QAE@XZ"
#define fnQApplication_ctor "??0QApplication@@QAE@AAHPAPADH@Z"
#define fnQFont_ctor "??0QFont@@QAE@ABVQString@@HH_N@Z"
#define fnQApplication_setFont "?setFont@QApplication@@SAXABVQFont@@PBD@Z"
#define fnQFont_dtor "??1QFont@@QAE@XZ"
#define fnQApplication_exec "?exec@QApplication@@SAHXZ"
#define fnQApplication_dtor "??1QApplication@@UAE@XZ"
#else
#define THISCALL __fastcall
#define _CDECL __fastcall
#define fnQString_fromStdWString "?fromStdWString@QString@@SA?AV1@AEBV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z"
#define fnQCoreApplication_addLibraryPath "?addLibraryPath@QCoreApplication@@SAXAEBVQString@@@Z"
#define fnQString_dtor "??1QString@@QEAA@XZ"
#define fnQApplication_ctor "??0QApplication@@QEAA@AEAHPEAPEADH@Z"
#define fnQFont_ctor "??0QFont@@QEAA@AEBVQString@@HH_N@Z"
#define fnQApplication_setFont "?setFont@QApplication@@SAXAEBVQFont@@PEBD@Z"
#define fnQFont_dtor "??1QFont@@QEAA@XZ"
#define fnQApplication_exec "?exec@QApplication@@SAHXZ"
#define fnQApplication_dtor "??1QApplication@@UEAA@XZ"
#endif
std::vector<HMODULE> QtLoadLibrarys(std::vector<std::wstring>&collectQtplugs)
{
std::vector<HMODULE> modules;
auto Qt5Widgets=LoadLibrary(L"Qt5Widgets.dll");
auto Qt5Gui=LoadLibrary(L"Qt5Gui.dll");
auto Qt5Core=LoadLibrary(L"Qt5Core.dll");
if(Qt5Core==0||Qt5Gui==0||Qt5Widgets==0)return {};
auto QString_fromStdWString=GetProcAddress(Qt5Core,fnQString_fromStdWString);
auto QCoreApplication_addLibraryPath=GetProcAddress(Qt5Core,fnQCoreApplication_addLibraryPath);
auto QString_dtor=GetProcAddress(Qt5Core,fnQString_dtor);
auto QApplication_ctor=GetProcAddress(Qt5Widgets,fnQApplication_ctor);
auto QFont_ctor=GetProcAddress(Qt5Gui,fnQFont_ctor);
auto QFont_dtor=GetProcAddress(Qt5Gui,fnQFont_dtor);
auto QApplication_setFont=GetProcAddress(Qt5Widgets,fnQApplication_setFont);
auto QApplication_exec=GetProcAddress(Qt5Widgets,fnQApplication_exec);
auto QApplication_dtor=GetProcAddress(Qt5Widgets,fnQApplication_dtor);
if(QString_fromStdWString==0||QCoreApplication_addLibraryPath==0||QString_dtor==0||QApplication_ctor==0||QFont_ctor==0||QFont_dtor==0||QApplication_setFont==0||QApplication_exec==0||QApplication_dtor==0)return {};
auto mutex=CreateSemaphoreW(0,0,1,0);
std::thread([=,&modules](){
static void* qapp; //必须static
void* qstring;
void* qfont;
for(int i=0;i<collectQtplugs.size();i++){
auto dirname=std::filesystem::path(collectQtplugs[i]).parent_path().wstring();
((void* (_CDECL*)(void*,void*))QString_fromStdWString)(&qstring,&dirname);
((void(_CDECL *)(void*))QCoreApplication_addLibraryPath)(&qstring);
((void(THISCALL*)(void*))QString_dtor)(&qstring);
//QCoreApplication_addLibraryPath(QString_fromStdWString(std::filesystem::path(collectQtplugs[i]).parent_path()));
}
int _=0;
((void*(THISCALL*)(void*,int*,char**,int))QApplication_ctor)(&qapp,&_,0,331266);
std::wstring font=L"MS Shell Dlg 2";
((void* (_CDECL*)(void*,void*))QString_fromStdWString)(&qstring,&font);
((void*(THISCALL*)(void*,void*,int,int,bool))QFont_ctor)(&qfont,&qstring,10,-1,0);
((void(_CDECL*)(void*,void*))QApplication_setFont)(&qfont,0);
((void(THISCALL*)(void*))QFont_dtor)(&qfont);
((void(THISCALL*)(void*))QString_dtor)(&qstring);
for(int i=0;i<collectQtplugs.size();i++){
modules.push_back(LoadLibrary(collectQtplugs[i].c_str()));
}
ReleaseSemaphore(mutex,1,0);
((void(*)())QApplication_exec)();
((void(THISCALL*)(void*))QApplication_dtor)(&qapp);
}).detach();
WaitForSingleObject(mutex,INFINITE);
return modules;
}
typedef HMODULE*(*QtLoadLibrary_t)(LPWSTR*,int);
QtLoadLibrary_t loadqtloader(const std::filesystem::path&pluginpath){
auto QtLoaderPath=pluginpath/"QtLoader.dll";
auto helper=LoadLibrary(QtLoaderPath.c_str());
if(helper==0)return 0;
auto QtLoadLibrary = (QtLoadLibrary_t)GetProcAddress(helper, "QtLoadLibrary");
return QtLoadLibrary;
}
typedef std::vector<HMODULE>*(* QtLoadLibrary_t)(std::vector<std::wstring>* dlls);
void Pluginmanager::loadqtdlls(std::vector<std::wstring>&collectQtplugs){
if(collectQtplugs.size()==0)return;
auto pluginpath=std::filesystem::current_path()/(x64?"plugin64":"plugin32");
@ -126,33 +38,20 @@ void Pluginmanager::loadqtdlls(std::vector<std::wstring>&collectQtplugs){
for(auto&p:collectQtplugs){
envs+=L";";envs+=std::filesystem::path(p).parent_path();
}
SetEnvironmentVariableW(L"PATH",envs.c_str());
HMODULE* modules;
if(std::false_type::value){
auto vmodules=QtLoadLibrarys(collectQtplugs);
if(vmodules.empty())return;
modules=vmodules.data();
}
else{
auto QtLoadLibrary = loadqtloader(pluginpath);
if(!QtLoadLibrary){
MessageBoxW(host->winId,CantLoadQtLoader,L"Error",0);
return ;
}
std::vector<wchar_t*>saves;
for(auto&p:collectQtplugs){
auto str=new wchar_t[p.size()+1];
wcscpy(str,p.c_str());
saves.emplace_back(str);
}
modules=QtLoadLibrary(saves.data(),collectQtplugs.size());
for(auto str:saves)delete str;
}
SetEnvironmentVariableW(L"PATH",envs.c_str());
auto QtLoadLibrary = (QtLoadLibrary_t)GetProcAddress(GetModuleHandle(0), "QtLoadLibrary");
auto modules=QtLoadLibrary(&collectQtplugs);
if(modules->empty())return;
for(int i=0;i<collectQtplugs.size();i++){
OnNewSentenceS[collectQtplugs[i]]=GetProcAddress(modules[i],"OnNewSentence");
OnNewSentenceS[collectQtplugs[i]]=GetProcAddress(modules->at(i),"OnNewSentence");
}
delete modules;
}
Pluginmanager::Pluginmanager(LunaHost* _host):host(_host),configs(_host->configs){
try {