Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Calling a C DLL from C++

Hey all,
I am trying to use the PNG image format in an application. I compiled the PNG DLL and everything went fine. I open the dll just fine with the LoadLibrary call and I get all the functions I want out of the PNG dll with the GetProcAddress function just fine as well.

Everything compiles fine. However, the minute I try to call any of the functions I want to use from the DLL at run-time I get this message:

"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."

These are the reasons I am stumped:

One, I have already made sure that the png.h file (which is the header for the PNG DLL) uses the _cplusplus check.

Two, I even put my own extern "C" call before all of my typedefs that hook into the function declarations that I need in png.h.

Three, the parameters for my typedefs and those given by png.h match up.

Here is some sample code to show you the process I am going through:

#include "png.h"

/* This is my TYPEDEF */
typedef int (CALLBACK *PNG_SIG_CMP)(png_bytep, png_size_t,

/* This is the TYPEDEF as it looks in png.h for this function */
extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig,
png_size_t start,
png_size_t num_to_check));

/* VARS in MY code */
HINSTANCE PNGDLL = LoadLibrary(filename); /* success */
int retval;
png_byte buf[4];

if (PNGDLL != NULL) {
if ((m_fPNGSigCmp = (PNG_SIG_CMP)GetProcAddress(m_cPNGDLL,
"png_sig_cmp")) == 0) {

/* This call is what gives me the run-tme error message above */
retval = m_fPNGSigCmp(buf,

Any ideas on what I am doing wrong here????

Thanks so much.

Sign In or Register to comment.