DLL Function / Load Library Problem
I'm having a bit of a problem calling a function in a DLL I created. Now it works fine when I call a function which returns void and has no arguments, but when I try to call this function (the following is from the DLL):
Code:
extern "C" _declspec (dllexport) int AddNums (int a, int b)
{
return (a + b);
}
I get this runtime error:
Quote:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually the result of calling a function delcared with one calling convention with a function pointer declared witha a different calling convention.
I can understand what's it's saying, but at the same time I can't because the function pointer... well, see for yourself. Here's the program trying to call the function:
Code:
#include "stdafx.h"
typedef void (WINAPI* DLL_NumberList) ();
typedef int (WINAPI* DLL_AddNums) (int, int);
DLL_NumberList NumberList;
DLL_AddNums AddNums;
int main(int argc, char **argv[])
{
HINSTANCE hLib = LoadLibrary ("DLLProj.dll");
char cModName[_MAX_PATH];
int iAns = 0;
if (hLib == NULL)
{
std::cerr << "Unable to load library!" << std::endl;
getchar ();
return 1;
}
memset (cModName, '\0', _MAX_PATH);
GetModuleFileName ((HMODULE) hLib, (LPSTR) cModName, _MAX_PATH);
std::cout << "Library Loaded: " << cModName << std::endl;
NumberList = (DLL_NumberList) GetProcAddress ((HMODULE) hLib, "NumberList");
AddNums = (DLL_AddNums) GetProcAddress ((HMODULE) hLib, "AddNums");
if (NumberList == NULL)
{
std::cerr << "Unable to load function NumberList!" << std::endl;
FreeLibrary ((HMODULE) hLib);
getchar ();
return 1;
}
if (AddNums == NULL)
{
std::cerr << "Unable to load function AddNums!" << std::endl;
FreeLibrary ((HMODULE) hLib);
getchar ();
return 1;
}
NumberList ();
iAns = AddNums (10, 10);
std::cout << "Answer: " << iAns << std::endl;
FreeLibrary ((HMODULE) hLib);
return 0;
}
Now NumberList is loaded / called fine, I just get that runtime error when I try to call AddNums ("iAns = AddNums (10,10);").
UPDATE: Ah strange: When I get the error dialog, if I press Ignore the answer still appears (correctly) in the console (Answer: 20), but the same ESP error persists.