Error implementing callback from DLL

Posted By: BigM

Error implementing callback from DLL - 05/28/10 00:56

Hello all,

I'm developing a DLL (in C++) that exports a function which is then called from lite-C. In the minimum working example below the only argument to the function is a pointer back to a lite-C callback function.

Puzzlingly, I get a run time error at the end of main, after all functions have returned. Here's the code:

Code:
//************** TesdDll.dll
#include "stdafx.h"
#include <Windows.h>
typedef void __stdcall callback(void);

extern "C" __declspec(dllexport) void fnTesdDLL(callback* cback){
	MessageBox(NULL, "We're in!", "TestDLL.dll",0);
	(*cback)();
}

//************** main.c
#include <acknex.h>
#include <windows.h>
void __stdcall fnTesdDLL(void*);
#define PRAGMA_API fnTesdDLL;TesdDLL!fnTesdDLL

void __stdcall cbk(){
	MessageBox(NULL, "We're back", "cbk",0);
}

void main(){
	fnTesdDLL(cbk);
	MessageBox(NULL, "We're in main", "main",0);
}



As you can see, I inserted some calls to MessageBox for debugging purposes. All three MessageBox calls are executed, the error surfacing only at the end of main().

The actual error (E1513) reads "Crash in SYS". Before I updated to the latest lite-C version the error read "Crash in cbk". Indeed, the problem seems to have something to do with the return from the callback: if I call fnTesdDLL(cbk) from within a function X then the error will pop at the end of X, not main.

Can anyone help?
Posted By: Nidhogg

Re: Error implementing callback from DLL - 05/28/10 14:38

This might be better of in Higher languages forum
Posted By: DJBMASTER

Re: Error implementing callback from DLL - 05/28/10 17:32

Hi, I've looked at your code and the problem seems to be the function prototype for the DLL function. Change this...
Code:
void __stdcall fnTesdDLL(void*);
#define PRAGMA_API fnTesdDLL;TesdDLL!fnTesdDLL


to simply this...
Code:
function fnTesdDLL(void*);


I have tested myself using my own callbacks and using your prototype singature gave me the crash. Use the lite-c function type.
Posted By: BigM

Re: Error implementing callback from DLL - 05/30/10 23:03

Thanks a lot for looking into it, DJBMASTER.

However, your solution did not work for me: I would always get "Empty prototype called in main".

I did get it to work by reading the manual blush blush
The case is well documented and has only to do with the use of __stdcall: because the DLL exported function is not also __stdcall the stack gets messed up.


Cheers
Posted By: DJBMASTER

Re: Error implementing callback from DLL - 05/31/10 01:48

hmm that may have been why just using 'function' worked in my case. This was my dll function...
Code:
typedef void __stdcall callback(void);
...
DLLFUNC void TestFunction(callback* c)
{
	(*c)();
}


without all the 'extern' stuff.
© 2024 lite-C Forums