Link to home
Start Free TrialLog in
Avatar of plinius
plinius

asked on

error after return from dll.

I have this code:
////////////////////////
#include "stdafx.h"
typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)

{

HINSTANCE hDLL;               // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
DWORD dwParam1;
UINT  uParam2=0, uReturnVal=0;

hDLL = LoadLibrary("azza");
if (hDLL != NULL)
{
   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"test2");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);
      MessageBox(0,"error","error",0);
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}
return 0;
}
//////////////////////////

which calls function test2 in azza.dll.
When I run the program, the function is executed, but after the execution of the function, I have the following error:


/////////////////
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!

Program: ... Files\Microsoft Visual Studio\MyProjects\user\Debug\user.exe
Module:
File: i386\chkesp.c
Line: 42

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.

(Press Retry to debug the application)
---------------------------
Afbreken   Opnieuw   Negeren  
---------------------------
/////////////////////

I'm new to c++. What does this error means, and how should I solve it ??
Avatar of jkr
jkr
Flag of Germany image

>>The value of ESP was not properly saved across a function call

This means that either the parameter list or the calling convention for "test2()" don't match in both projects.
Avatar of GloomyFriar
GloomyFriar

Show how the test2 function is declared in the dll.
SOLUTION
Avatar of GloomyFriar
GloomyFriar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of plinius

ASKER

this is my dll.
/////////////////
// azza.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

extern "C" __declspec(dllexport)void test2();

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                               )
{
    return TRUE;
}


void test2(){MessageBox(0,"test2","hallo",0);}
////////////////
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of plinius

ASKER

thanks.
Don't want to be picky, but in which way did I 'assist'? I consider the following the answer:

Comment from jkr  Date: 11/13/2003 02:39AM GMT  
>>The value of ESP was not properly saved across a function call

This means that either the parameter list or the calling convention for "test2()" don't match in both projects.  
Avatar of plinius

ASKER

actually, this didn't assisted (I don't know what ESP & calling convention are, since I'm actually Dutch). It was
"
Well, that is the problem:
typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
and
extern "C" __declspec(dllexport)void test2();
simply do _not_ match - use either
typedef void (*LPFNDLLFUNC1)();
or
extern "C" __declspec(dllexport) UINT CALLBACK test2(DWORD,UINT);

"
that helped me.
So, that was still me :o)
Hey, jkr, do you want to say, that my suggestion is wrong?