I am learning how to write DLL's. My DLL has 3 files, a cpp file, a header file and a defination file.
I will post the code below:
Header File:
extern "C" double __stdcall MyFunc(int a, double b);
extern "C" int CdeclFunc(int a);
CPP File:
#include "pascal.h"
double __stdcall MyFunc(int a, double b)
{
return a*b;
}
int CdeclFunc(int a)
{
return 2 * a;
}
Def File:
LIBRARY pascaldll
EXPORTS
MYFUNC = _MyFunc@12
_MyFunc@12 @1
CdeclFunc @2
Now the DLL compiles fine and I get the DLL file out of it. Now I have an application file to use that
DLL file.
#include <windows.h>
#include <iostream.h>
void main (void)
{
typedef int (* lpFunc1)(int);
typedef double (* lpFunc2)(int,double);
HINSTANCE hLibrary;
lpFunc1 Func1;
lpFunc2 Func2;
int x = 3;
double y = 2.3;
int a;
double b;
hLibrary = LoadLibrary("pascaldll.dll"
;
if (hLibrary != NULL)
{
Func1 = (lpFunc1)GetProcAddress(hLibrary, "CdeclFunc"
;
if (Func1 != NULL)
a = ((Func1)(x));
else
cout<<"Error in Func1 call"<<endl;
Func2 = (lpFunc2)GetProcAddress(hLibrary, MAKEINTRESOURCE(1));
if (Func2 != NULL)
b = ((Func2)(x,y));
else
cout<<"Error in Func2 call"<<endl;
cout << endl << GetLastError();
cout << "b = " <<b <<endl;
cout << "a = " <<a <<endl;
}
else
cout <<"Error in Load Library"<<endl;
FreeLibrary(hLibrary);
}
But I am getting ERROR "The value of ESP was not saved properly across a function call.This is usually
the result of calling a function declared with one calling convention and a function pointer with another
calling convention". If I "Ignore" the error, I get proper results and then the application crashes.
Can somebody help me out here?
I will post the code below:
Header File:
extern "C" double __stdcall MyFunc(int a, double b);
extern "C" int CdeclFunc(int a);
CPP File:
#include "pascal.h"
double __stdcall MyFunc(int a, double b)
{
return a*b;
}
int CdeclFunc(int a)
{
return 2 * a;
}
Def File:
LIBRARY pascaldll
EXPORTS
MYFUNC = _MyFunc@12
_MyFunc@12 @1
CdeclFunc @2
Now the DLL compiles fine and I get the DLL file out of it. Now I have an application file to use that
DLL file.
#include <windows.h>
#include <iostream.h>
void main (void)
{
typedef int (* lpFunc1)(int);
typedef double (* lpFunc2)(int,double);
HINSTANCE hLibrary;
lpFunc1 Func1;
lpFunc2 Func2;
int x = 3;
double y = 2.3;
int a;
double b;
hLibrary = LoadLibrary("pascaldll.dll"
if (hLibrary != NULL)
{
Func1 = (lpFunc1)GetProcAddress(hLibrary, "CdeclFunc"
if (Func1 != NULL)
a = ((Func1)(x));
else
cout<<"Error in Func1 call"<<endl;
Func2 = (lpFunc2)GetProcAddress(hLibrary, MAKEINTRESOURCE(1));
if (Func2 != NULL)
b = ((Func2)(x,y));
else
cout<<"Error in Func2 call"<<endl;
cout << endl << GetLastError();
cout << "b = " <<b <<endl;
cout << "a = " <<a <<endl;
}
else
cout <<"Error in Load Library"<<endl;
FreeLibrary(hLibrary);
}
But I am getting ERROR "The value of ESP was not saved properly across a function call.This is usually
the result of calling a function declared with one calling convention and a function pointer with another
calling convention". If I "Ignore" the error, I get proper results and then the application crashes.
Can somebody help me out here?