I created a test xll which references a dll. The xll and dll files are both in the same folder. This is the c++ function I'm trying to call in Excel
CPP
- Code: Select all
double // squares a number using the dll
squareInCpp(double &arg)
{
libHandle = LOADLIB("the_DLL_Name.dll"); // LOADLIB = LoadLibraryA
if (!libHandle) {
double err = GetLastError(); // <---------------- ERROR -------------------------
return err;
}
//You can stop reading here, since this code won't be reached
typedef double (*PtrFunc_square_in_Cpp) (double &arg);
PtrFunc_square_in_Cpp squareInCpp = (PtrFunc_square_in_Cpp)LOADSYMBOL(libHandle, "square_in_Cpp"); // LOADSYMBOL = GetProcAdress
if( !squareInCpp )
return -1;
else
{
double res = squareInCpp(arg);
CLOSELIB(libHandle);
return res;
}
}
Header
- Code: Select all
...
#include <windows.h>
#include <direct.h>
HINSTANCE libHandle;
#define LOADLIB(X) LoadLibraryA(X) // <------
#define LOADSYMBOL GetProcAddress // <--------
#define LASTERROR() GetLastError()
#define CLOSELIB(X) FreeLibrary(X)
- The function always returns with error code 126. But the dll is in the same folder together with the xll, how can he not find the file?
- I analysed the xll with DependancyWalker, it says that MSVCR90D.DLL is missing. I don't know if this is important, since DependancyWalker always claims that - even when I analyse the example xlls delivered by the xlw download and which are working fine.
- By the way, I tried using my posted code with an exe file, it worked. Do I have to use loadlibrary in a different way in the xlw framework?
Thanks and best regards
Sean