{ QStringList nameFilters; nameFilters << "ss3*.dll"; QDir mainDir(qApp->applicationDirPath()); QStringList files = mainDir.entryList(nameFilters, QDir::Files, QDir::Name);// QLibrary *myLib = new QLibrary(); for (int i=0; i<files.count(); i++) { HINSTANCE libHandle = LoadLibrary(QString(files.at(i)).toStdWString().c_str()); if (libHandle) { typedef const char* (*MyPrototype)(); qDebug() << "beforeResolved" << files.at(i);// MyPrototype myFunction = (MyPrototype) myLib->resolve(files.at(i), "GetDLLInfo"); MyPrototype myFunction = (MyPrototype) GetProcAddress(libHandle, "GetDLLInfo"); qDebug() << "afterResolved"; if (myFunction) { qDebug() << "functionDetected"; const char* str = myFunction(); if (str) { qDebug() << "parse GetDLLInfo"; short sz = static_cast<unsigned short>(str[0]); qDebug() << "str size= " << sz; QByteArray encodedString(str +1, sz); qDebug() << "str = " << encodedString; QTextCodec *codec = QTextCodec::codecForName("Windows-1251"); QString buff = codec->toUnicode(encodedString) ; qDebug() << files.at(i) << sz << buff; ui->textEdit->append( files.at(i) + " : GetDLLInfo("+ QVariant(sz).toString() +") : " + buff ); } } qDebug() << "afterParse"; } }}
function GetDLLInfo: ShortString; StdCall;...implementationfunction GetDLLInfo: ShortString;begin Result:=sDLLInfo;end;
#ifdef D_SHARED_LIB #define D_SHARED extern "C" __declspec(dllexport) Q_DECL_EXPORT#else #define D_SHARED extern "C" __declspec(dllexport) Q_DECL_IMPORT#endifD_SHARED char* GetDLLInfo();...char* GetDLLInfo(){ QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));qInstallMsgHandler(myMessageOutput);qDebug() << "GetDLLInfo";QString tmp = "Типа плугин"; QTextCodec *codec = QTextCodec::codecForName("Windows-1251"); QByteArray text = codec->fromUnicode( tmp.toStdString().c_str() ); char *data = new char[text.size() + 2]; strcpy(data+1, text.data()); data[0] = static_cast<unsigned short>(text.size()); return data;}
{ QStringList nameFilters; nameFilters << "ss3*.dll"; QDir mainDir(qApp->applicationDirPath()); QStringList files = mainDir.entryList(nameFilters, QDir::Files, QDir::Name);// QLibrary *myLib = new QLibrary(); for (int i=0; i<files.count(); i++) { // myLib->setFileName( files.at(i) );// if (myLib->load()) HINSTANCE libHandle = LoadLibrary(QString(files.at(i)).toStdWString().c_str()); if (libHandle) { typedef __stdcall const char* (*MyPrototype)(); qDebug() << "beforeResolved" << files.at(i);// MyPrototype myFunction = (MyPrototype) myLib->resolve(files.at(i), "GetDLLInfo"); MyPrototype myFunction = (MyPrototype) GetProcAddress(libHandle, "GetDLLInfo@0"); qDebug() << "afterResolved"; if (!myFunction) { qDebug() << "Using GetDLLInfo"; myFunction = (MyPrototype) GetProcAddress(libHandle, "GetDLLInfo"); } else qDebug() << "Using GetDLLInfo@0"; if (myFunction) { qDebug() << "functionDetected"; const char* str = myFunction(); if (str) { qDebug() << "parse GetDLLInfo"; short sz = static_cast<unsigned short>(str[0]); qDebug() << "str size= " << sz; QByteArray encodedString(str +1, sz); qDebug() << "str = " << encodedString; QTextCodec *codec = QTextCodec::codecForName("Windows-1251"); QString buff = codec->toUnicode(encodedString) ; qDebug() << files.at(i) << sz << buff; ui->textEdit->append( files.at(i) + " : GetDLLInfo("+ QVariant(sz).toString() +") : " + buff ); } } qDebug() << "afterParse"; }// else qDebug() << "Can't load DLL: " << files.at(i); /* if (myLib->isLoaded()) { myLib->unload(); } */ }}
struct ShortString { unsigned char Length; char Data[255];};D_SHARED __stdcall ShortString GetDLLInfo();...// function GetDLLInfo: ShortString;ShortString GetDLLInfo(){ QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));qDebug() << "GetDLLInfo";ShortString st;QString tmp = "Типа плугин"; QTextCodec *codec = QTextCodec::codecForName("Windows-1251"); QByteArray text = codec->fromUnicode( tmp.toStdString().c_str() ); char *data = new char[text.size() + 2]; strcpy( st.Data, text.data()); st.Length = text.size(); return st;}
DEF_FILE += ss3plugin-qt-example.def
EXPORTS GetDLLInfo
typedef __stdcall const ShortString (*MyPrototype)(); MyPrototype myFunction = (MyPrototype) GetProcAddress(libHandle, "GetDLLInfo"); if (myFunction) { const ShortString str = myFunction(); if (str.Length>0) { short sz = str.Length; QByteArray encodedString(str.Data, str.Length);
void ShowFormFromDLL(HINSTANCE AppHandle, char* DBName, char* LibName){ QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); HINSTANCE GlobHinstDLL = AppHandle; int argc = 0;char* argv[1]; argv[0]="ss3plugin-qt-example1.dll"; QApplication* pApp = new QApplication(argc, argv); QString m_DBName = DBName, m_LibName = LibName; Widget w(m_DBName, m_LibName); w.show(); pApp->exec(); qDebug() << "ShowFormFromDLL" << DBName << LibName;}
qDebug() << argv[0] << "ShowFormFromDLL" << DBName << LibName; /// Создаём при необходимости свой экземпляр QApplication, без него не работает GUI bool mainQApp = false; QApplication* pApp = (QApplication*) qApp->instance(); if (!pApp) { pApp = new QApplication(argc, argv); mainQApp = true; } Widget w(m_DBName, m_LibName); w.show(); if (mainQApp) { pApp->exec(); delete qApp; /// Удаляем свой экземпляр QApplication }