Using an external lib, API question

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
grosnombril
Newbie
Posts: 8
Joined: Tue Nov 14, 2006 5:26 pm

Using an external lib, API question

Post by grosnombril » Tue Nov 14, 2006 8:09 pm

I have created a tiny DLL library with Visual C++, the library consists of basic math functions (code appended).

I tried to use the Add API to add two variables to test the feature, I got the "Unable to load DLL" return when calling it, I know the library was loaded properly since its ID is non-zero.

iffileexists>c:\math.dll
Let>tataDLL=c:\math.dll
LibLoad>mathDll,mathlib
let>returnVal=0
LibFunc>mathlib,Add,returnVal,9,5

Any idea ???

---------------------------

#include "MathFuncsDll.h"

#include

using namespace std;

namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}

double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}

double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}

double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw new invalid_argument("b cannot be zero!");
}

return a / b;
}
}



// MathFuncsDll.h

namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a + b
static __declspec(dllexport) double Add(double a, double b);

// Returns a - b
static __declspec(dllexport) double Subtract(double a, double b);

// Returns a * b
static __declspec(dllexport) double Multiply(double a, double b);

// Returns a / b
// Throws DivideByZeroException if b is 0
static __declspec(dllexport) double Divide(double a, double b);
};
}

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Nov 14, 2006 8:18 pm

If you got "Unable to load DLL" and you know the DLL module reference is ok and the DLL is loaded, then it can only be it can't find the export.

Issue might be that you are not using the stdcall calling convention. You're also using doubles. Try changing to using stdcall and integers.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Wed Nov 15, 2006 12:45 am

You are defining tataDLL, then loading mathDll.

try this instead:


iffileexists>c:\math.dll
Let>mathDll=c:\math.dll
LibLoad>mathDll,mathlib

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Nov 15, 2006 8:26 am

Oh yeh! See how I missed the obvious :oops:
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

grosnombril
Newbie
Posts: 8
Joined: Tue Nov 14, 2006 5:26 pm

More about it...

Post by grosnombril » Thu Nov 16, 2006 3:36 pm

I was a "victim" of renaming variables after a cut & paste. I still cannot get any library functions working.

This is EXACTLY what I am trying to run:

iffileexists>c:\testdll\lcc\testdll.dll
Let>tataDLL=c:\testdll\lcc\testdll.dll
let>returnVal=0
LibLoad>tataDLL,tatalib
LibFunc>tatalib,Add,returnVal,9,5

Please help ;)

Mario
------------------------
From a DLL generated using:
#include
/*------------------------------------------------------------------------
Procedure: LibMain ID:1
Purpose: Dll entry point.Called when a dll is loaded or
unloaded by a process, and when new threads are
created or destroyed.
Input: hDllInst: Instance handle of the dll
fdwReason: event: attach/detach
lpvReserved: not used
Output: The return value is used only when the fdwReason is
DLL_PROCESS_ATTACH. True means that the dll has
sucesfully loaded, False means that the dll is unable
to initialize and should be unloaded immediately.
Errors:
------------------------------------------------------------------------*/
BOOL WINAPI __declspec(dllexport) LibMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// The DLL is being loaded for the first time by a given process.
// Perform per-process initialization here. If the initialization
// is successful, return TRUE; if unsuccessful, return FALSE.


break;
case DLL_PROCESS_DETACH:
// The DLL is being unloaded by a given process. Do any
// per-process clean up here, such as undoing what was done in
// DLL_PROCESS_ATTACH. The return value is ignored.


break;
case DLL_THREAD_ATTACH:
// A thread is being created in a process that has already loaded
// this DLL. Perform any per-thread initialization here. The
// return value is ignored.

break;
case DLL_THREAD_DETACH:
// A thread is exiting cleanly in a process that has already
// loaded this DLL. Perform any per-thread clean up here. The
// return value is ignored.

break;
}
return TRUE;
}

double WINAPI __declspec(dllexport) Add(double a, double b)
{
return a+b;
}

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Nov 16, 2006 3:46 pm

Have you tried changing to stdcall calling convention and replacing the double types with integers? LibFunc only supports integers for numeric values.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

grosnombril
Newbie
Posts: 8
Joined: Tue Nov 14, 2006 5:26 pm

Finally found the problem.

Post by grosnombril » Thu Nov 16, 2006 5:00 pm

I was building with underscores and seems like it did not like it.

Keep in mind last time I wrote a DLL was for my univeristy thesis, back in 1997 (feeling old now).

Many thanks, now I can focus on the "real" stuff.


Mario Hebert

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts