Solved

error after return from dll.

Posted on 2003-11-12
10
652 Views
Last Modified: 2012-06-27
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 ??
0
Comment
Question by:plinius
  • 4
  • 3
  • 3
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9736684
>>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.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9738657
Show how the test2 function is declared in the dll.
0
 
LVL 6

Assisted Solution

by:GloomyFriar
GloomyFriar earned 65 total points
ID: 9738672
Try to do so:

in dll:
extern "C"  UINT __declspec(dllexport) test2(DWORD,UINT);

in exe:
typedef UINT (__cdecl *LPFNDLLFUNC1)(DWORD,UINT);
0
 

Author Comment

by:plinius
ID: 9738778
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);}
////////////////
0
 
LVL 86

Accepted Solution

by:
jkr earned 75 total points
ID: 9739691
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);


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:plinius
ID: 9740370
thanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9740419
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.  
0
 

Author Comment

by:plinius
ID: 9742637
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9743051
So, that was still me :o)
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9746697
Hey, jkr, do you want to say, that my suggestion is wrong?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 60
Fast access to array, using indexes, smart iterative search 2 107
Getting IP address 8 71
computer science syllabus 3 70
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now