CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2016
    Posts
    1

    Question How to implement a callback function

    Hello,

    I would like to properly implement a callback function, but I have no idea how. I am trying to make a DLL, for now this DLL should have a number of variables that are stored in a 2D array. Using the set function in the interface I can select a variable in this 2D array (row and column index) and change its value.

    Using the get function I can verify that the change was successful, but I would like to be notified about this change automatically. I have been told this could be done using a callback function, but I have no idea how to implement that.

    Could somebody please provide me with an example and/or explanation for a proper implementation to trigger the callback function if one of the values in the 2D array changes and how to catch the resulting event?

    Many thanks in advance, Nico

    Code:
    #pragma once
    
    #if !defined(DLL)
    #define DLL __declspec(dllimport)
    #endif
    
    #if !defined(IDLL)
    #define IDLL DLL
    #endif
    
    #if defined(__cplusplus)
    extern "C" {
    #endif
    
    	// callback function
    	// long EventType: Not sure what kind of event types are available
    	// long Column: The column.
    	// long Row: The row.
    	// long EventCode: e.g. 0 = decreased, 1 = increased
    	// long EventDataLong: Not sure, but I was told we would need a parameter.
    	// double EventDataDouble : The current value of the variable
    	typedef void (__stdcall *OnExampleParameterChangedEvent)(long EventType,
    		long Column,
    		long Row,
    		long EventCode,
    		long EventDataLong,
    		double EventDataDouble);
    
    	// Attach callback function handler
    	IDLL long __stdcall ExampleAttachParameterChangedEvent(OnExampleParameterChangedEvent ParameterChanged);
    
    
    	// Read the state of a Value.
    	// long Column: The column.
    	// long Row: The row.
    	// long* State: The state of the parameter
    	IDLL long __stdcall ExampleGetValue(long Column, long Row, long* State);
    
    	// Sets the state of a Value.
    	// long Column: The column.
    	// long Row: The row.
    	// long State: The state of the parameter
    	IDLL long __stdcall ExampleSetValue(long Column, long Row, long State);
    
    
    #if defined(__cplusplus)
    }
    #endif

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to implement a callback function

    This is a very simple example of using a callback function
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    void fcb1()
    {
    	cout << "In call 1" << endl;
    }
    
    void fcb2()
    {
    	cout << "In call 2" << endl;
    }
    
    class cbck
    {
    	typedef void(*fc)();
    	string s1;
    	fc fcb;
    
    public:
    	cbck(fc fb = fcb1) : fcb(fb) {}
    
    	string getstr() const
    	{
    		return s1;
    	}
    
    	void setstr(const string& s) {
    		s1 = s;
    		fcb();
    	}
    
    	void setcall(fc fb)
    	{
    		fcb = fb;
    	}
    };
    
    int main()
    {
    	cbck c1;
    
    	c1.setstr("s1");
    	cout << c1.getstr() << endl;
    
    	c1.setcall(fcb2);
    	c1.setstr("s2");
    
    	cout << c1.getstr() << endl;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured