Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DLL Question 1

Status
Not open for further replies.

dbdoshi

Programmer
Feb 6, 2001
1
US
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(&quot;pascaldll.dll&quot;);

if (hLibrary != NULL)
{
Func1 = (lpFunc1)GetProcAddress(hLibrary, &quot;CdeclFunc&quot;);
if (Func1 != NULL)
a = ((Func1)(x));
else
cout<<&quot;Error in Func1 call&quot;<<endl;

Func2 = (lpFunc2)GetProcAddress(hLibrary, MAKEINTRESOURCE(1));
if (Func2 != NULL)
b = ((Func2)(x,y));
else
cout<<&quot;Error in Func2 call&quot;<<endl;

cout << endl << GetLastError();

cout << &quot;b = &quot; <<b <<endl;
cout << &quot;a = &quot; <<a <<endl;
}

else
cout <<&quot;Error in Load Library&quot;<<endl;

FreeLibrary(hLibrary);
}

But I am getting ERROR &quot;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&quot;. If I &quot;Ignore&quot; the error, I get proper results and then the application crashes.
Can somebody help me out here?
 
I think your guess is write.

typedef double (* lpFunc2)(int,double);

But you actually get the function pointer which
calling convention is stdcall.

So, you need to change it like this :

typedef double (__stdcall * lpFunc2)(int,double);

Please try it and tell me the result. If you failed,
I'll make effort to give you more helps.
Hee S. Chung
heesc@netian.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top