dll import

here is code in dll file
#include <windows.h>
#include<iostream>
#include<stdio.h>
#include "RunTimeDLL.h"
using namespace std;
// Definition of the function which will be exported to other modules.
int SayRunTimeDLL(int a, int b)
{
int c=a+b;
return c;
}


.h file just has definition of SayRunTimeDLl(int, int)



HERE IS THE CODE i am wrting in main cpp file


#include<Windows.h>
#include<iostream>
#include<conio.h>

using namespace std;

typedef UINT (CALLBACK* asd)(UINT,UINT);

void main()
{
HINSTANCE hinst;
asd a;
UINT c;
hinst=LoadLibrary("C:\\RunTimeDLL.dll");

if (hinst != NULL)
{
a = (asd)GetProcAddress(hinst, "SayRunTimeDLL");
if (!a)
{
// handle the error
FreeLibrary(hinst);
cout << "Error code: " << GetLastError() << "\n";
return;
}
else
{
// call the function
c = a(22,7);
cout << "Value: " << c<< "\n";
}
}
getch();
}




now when i run the code i get error


Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.


can anyone tell me what it means and how to resolve it??
What that means: Functions work by passing arguments in a memory location known as the stack. When the computer "jumps" to the function, it also stores the return address of the code it was executing on the stack. It uses that address to jump back. The ESP register is an internal memory location in the CPU that is used to keep track of where data is placed in the stack.

A calling convention describes how arguments are placed on the stack and whose responsibility it is to remove them. The C calling convention dictates that the calling function should clean up the stack. The Windows API uses a convention known as Standard Call or STDCALL, in which the called function must clean up the stack before it returns.

In your DLL, the C calling convention is used by your SayRunTime function.
In your main programming, you have cast the pointer returned by GetProcAddress with a type defintion using the Macro CALLBACK, which specifies a STDCALL.

So, your main function expects SayRunTime to clean up the stack. SayRunTime expects its caller to clean up the stack. Neither is doing so, thus the contents of ESP are left in a dangerous condition. The run time error checking included in your program has spotted it and has provided a helpful message to alert you to the situation. Without this check, when your main function has return, the return address on the stack would have been incorrectand your program would have inexplicably crashed. (This is also a security risk.)

Try simply removing the CALLBACK macro from your main program, or use the CALLBACK macro in both your header file and your DLL source.

When you need help, try to use the code tags so that others can actually read your formatted code. Note the <> button.
Last edited on
yeah i'll keep that in mind..
thnx man.
Topic archived. No new replies allowed.