[SOLVED] Excel Interface with XLW
-
I used xlw product for a number of years now and it is a great framework. Recently I had to do an add-on that required the usage of Qt. It turned out that, after compiling XLL addon with linked QtCore and instantiation of QCoreApplication, the xll cannot be loaded into excel. Excel complains about the xll having an incompatible format...
i use Qt 4.8 and the dlls are in the same folder as the addon
-
I know it is an old topic but I had a similar issue and I think I have a better solution that could be used for deployment (it works at least for Qt 5.9.3) :
- copy all your dll in the xll folder (Qt and anything else)
- Add the path of your xll in the system PATH (not the user)
- Instantiate an object of QCoreApplication
- Define a search path for libraries on the xll folder (resolved the "driver not loaded" for sql for example).
I noted that when we open an xll, the working directory is not the xll folder but the user folder defined in excel (like <user>/document). So I guess that dll couldn't load on runtime for this reason.
For finding xll path, I didn't find a way to do it with Qt.
#include <QCoreApplication> #include <windows.h> QCoreApplication * g_application = NULL; QString getPath() { char path[100]; HMODULE hm = NULL; if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &getPath, &hm)) { int ret = GetLastError(); fprintf(stderr, "GetModuleHandle returned %d\n", ret); } GetModuleFileNameA(hm, path, sizeof(path)); return QString(path); } class RegisterLibrary { public: int m_argc; char ** m_argv; RegisterLibrary() { m_argc = 1; m_argv = new char*[2]; m_argv[0] = new char[30]; strcpy(m_argv[0], "libExcel"); m_argv[1] = NULL; g_application = new QCoreApplication(m_argc, m_argv); QString filePath = getPath(); QStringList s = filePath.split("\\"); s.removeLast(); QString path = s.join("\\"); QStringList libraries = QCoreApplication::libraryPaths(); libraries.append(path); QCoreApplication::setLibraryPaths(libraries); } virtual ~RegisterLibrary() { delete g_application; delete [] m_argv[0]; delete [] m_argv; g_application = NULL; } }; RegisterLibrary g_library;