Click here to Skip to main content
15,888,113 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Version of a console application Pin
_Flaviu15-Apr-21 22:55
_Flaviu15-Apr-21 22:55 
GeneralRe: Version of a console application Pin
Richard MacCutchan15-Apr-21 23:03
mveRichard MacCutchan15-Apr-21 23:03 
AnswerRe: Version of a console application Pin
Maximilien16-Apr-21 6:13
Maximilien16-Apr-21 6:13 
GeneralRe: Version of a console application Pin
_Flaviu23-Apr-21 1:29
_Flaviu23-Apr-21 1:29 
QuestionRe: Version of a console application Pin
CPallini16-Apr-21 8:01
mveCPallini16-Apr-21 8:01 
AnswerRe: Version of a console application Pin
_Flaviu23-Apr-21 1:28
_Flaviu23-Apr-21 1:28 
AnswerRe: Version of a console application Pin
Mircea Neacsu16-Apr-21 23:11
Mircea Neacsu16-Apr-21 23:11 
GeneralDLL with a callback Pin
Bodo240715-Apr-21 6:05
Bodo240715-Apr-21 6:05 
Hi!
I try to implement a callback function within a dll, which triggers an assigned function in the main application. I want to trigger this "event" in the main app from within the dll.

Here is my code from the dll:
C++
//a normal function I call from the main application:
extern "C" __declspec(dllexport) void funcA(const unsigned long long a, const unsigned long long b);
//following function should be the callback, triggering an assigned function in the main application
extern "C"
{
    typedef void(__stdcall* callback_t)(unsigned int halfbeast);

    __declspec(dllexport) void public_func_taking_callback(callback_t evHnd)
    {
        evHnd(333);//value just for testing
    }
}


void funcA(
    const unsigned long long a,
    const unsigned long long b)
{
    //some code here... do things with a and b
   if(a>10)                       //trigger the event in the main application
       callback_t(public_func_taking_callback);   
}


Here is the code I use in the main application to load the library and attach to the functions:
C++
typedef void (__stdcall *eventCallback)(unsigned int miau);
typedef void (__stdcall *setCallback)(eventCallback evHnd);
void __stdcall CallB(unsigned int);

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
signed int retval;
unsigned int i;
char txt[7];
unsigned long long a,index;
UnicodeString USt;
HINSTANCE dllHandle=NULL;

//Obtain a handle to the DLL
dllHandle = LoadLibrary(L"test.dll");
if (!dllHandle){
    ShowMessage("Could not load test.dll");
    Form1->Close();
    }
else{    //Lib was loaded, try to get data
    void (__stdcall* funcA)(const unsigned long long a, const unsigned long long b);  
    funcA=(void(__stdcall*)(const unsigned long long a, const unsigned long long b))GetProcAddress(dllHandle,"funcA");

    // DLL function pointer
    setCallback values=(setCallback)GetProcAddress(dllHandle, "public_func_taking_callback");
    if(values != NULL){
        values(&CallB);
        }

    if(!funcA){
        Memo1->Text="No handle to function found.";
        }
    else{
        funcA(1,1);
        funcA(100,100);
        }
    FreeLibrary(dllHandle);
    }

}
//---------------------------------------------------------------------------
void __stdcall CallB(unsigned int halfbeast){
ShowMessage("triggered");
}
//---------------------------------------------------------------------------


What happens is unfortunately not what I wanted:
1) In the very moment the line "values(&CallB);" is executed, CallB is called. This is not what I wanted, I justed wanted to give the callback pointer of the dll to my function CallB.
2) CallB is not called when the line "funcA(100,100);" is executed. Most likely there is something wrong with the function call within the dll, but I cannot see it...
GeneralRe: DLL with a callback Pin
Richard MacCutchan15-Apr-21 6:27
mveRichard MacCutchan15-Apr-21 6:27 
GeneralRe: DLL with a callback Pin
Bodo240715-Apr-21 6:41
Bodo240715-Apr-21 6:41 
GeneralRe: DLL with a callback Pin
Richard MacCutchan15-Apr-21 6:55
mveRichard MacCutchan15-Apr-21 6:55 
GeneralRe: DLL with a callback Pin
Bodo240715-Apr-21 7:33
Bodo240715-Apr-21 7:33 
GeneralRe: DLL with a callback Pin
CPallini15-Apr-21 20:51
mveCPallini15-Apr-21 20:51 
GeneralRe: DLL with a callback Pin
Richard MacCutchan15-Apr-21 22:44
mveRichard MacCutchan15-Apr-21 22:44 
PraiseRe: DLL with a callback Pin
Bodo240715-Apr-21 23:04
Bodo240715-Apr-21 23:04 
GeneralRe: DLL with a callback Pin
CPallini15-Apr-21 23:24
mveCPallini15-Apr-21 23:24 
GeneralRe: DLL with a callback Pin
Richard MacCutchan16-Apr-21 0:17
mveRichard MacCutchan16-Apr-21 0:17 
GeneralRe: DLL with a callback Pin
Bodo240716-Apr-21 3:47
Bodo240716-Apr-21 3:47 
GeneralRe: DLL with a callback Pin
Richard MacCutchan16-Apr-21 4:33
mveRichard MacCutchan16-Apr-21 4:33 
GeneralRe: DLL with a callback Pin
Bodo240716-Apr-21 4:46
Bodo240716-Apr-21 4:46 
GeneralRe: DLL with a callback Pin
Richard MacCutchan16-Apr-21 5:32
mveRichard MacCutchan16-Apr-21 5:32 
Questionmac desing: Pin
prasad 202114-Apr-21 23:09
prasad 202114-Apr-21 23:09 
AnswerRe: mac desing: Pin
Richard MacCutchan14-Apr-21 23:12
mveRichard MacCutchan14-Apr-21 23:12 
AnswerRe: mac desing: Pin
Dave Kreskowiak15-Apr-21 4:35
mveDave Kreskowiak15-Apr-21 4:35 
AnswerRe: mac desing: Pin
David Crow15-Apr-21 5:39
David Crow15-Apr-21 5:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.