user2198999
user2198999

Reputation: 11

How to call DLL APIs

I am attempting to write an application in VC++ version 8. I have a DLL, using dependency walker I got the list of APIs available with DLL.

I am able to load DLL.But when i tried to call some of the APIs in DLL, I will then get this debug 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."

typedef void ( WINAPI *RECEIVE_CALLBACK)int*, int );

typedef void (WINAPI *MYPROC)(RECEIVE_CALLBACK);

     .....
     .....

handleDll = LoadLibraryW((LPCWSTR)L"Example.dll");

ProcAdd = (MYPROC) GetProcAddress(handleDll, "_DLLAPI_Call1@8"); 

(ProcAdd) ( (RECEIVE_CALLBACK) ReceiveFunc); 

When the last line is executed i am getting the above error. What could be the reason?

Upvotes: 1

Views: 625

Answers (1)

Hans Passant
Hans Passant

Reputation: 941218

Your MYPROC function pointer declaration is wrong. This function takes two arguments, not one. You can tell from the @8 part of the name, that says that the argument values require 8 bytes of stack space. Calling it with one argument, a 4 byte pointer, will always imbalance the stack.

You'll need to fix your MYPROC declaration.

Upvotes: 4

Related Questions