Hello all,
I am testing a callback method to get events from a USB connected instrument. I looked at many (!!!) posts on Callbacks and found this message:
https://forum.xojo.com/t/use-a-callback-function-from-a-c-lib/28698
with which I based my Declare, with no success.
I can currently connect to the instrument and get all the data I want with the API.
I would just like to add the Callback method, if possible. My problems and questions are written in the text where appropriate.
The API definitions are:
//-------------------------------------------------------------------------------
// c++ API
// NOTE: the API events, functions, and DLL were renamed.
RegisterDeviceEventHandler()
API FPtr_DeviceEventHandler RegisterDeviceEventHandler (
FPtr_DeviceEventHandler handler,
void * context )
Registers a device event handler callback function.
Parameters:
in : handler : a pointer to the event handler callback function. Or NULL if a registered handler should be removed
in : context : a context for the callbacks use. May be NULL
//---------------------------
FPtr_DeviceEventHandler
typedef void(CALLING_CONVENTION * FPtr_DeviceEventHandler) (DeviceHandle devHndl, DeviceEvent event, void *context)
Callback signature of a device event handler function.
Parameters:
in : devHndl : handle to the device
in : event : event type, e.g. eButtonPressed
in : context : the context which was registered with the handler
//---------------------------
// âDeviceEventâ definition
enum {
eArrival = 0x11, /< device plugged-in */
eDeparture = 0x12, /< device unplugged */
eButtonPressed = 0x01, /**< button pressed */
};
typedef UInteger DeviceEvent;
//-------------------------------------------------------------------------------
The c++ demo code is
//-------------------------------------------------------------------------------
// c++ demo code
// This function is called if an event happens
void MyDeviceEventFunction(DeviceHandle devHndl, DeviceEvent event, void *refCon)
{
// increase our event counter
int eventCounter = static_cast<int>(refCon);
*eventCounter += 1;
std::cout << âEvent #â << *eventCounter;
switch(event) {
case eButtonPressed: std::cout << " âButton pressedâ 0x" << std::hex << devHndl << std::dec << std::endl; break;
case eArrival: std::cout << " âDevice attachedâ 0x" << std::hex << devHndl << std::dec << std::endl; break;
case eDeparture: std::cout << " âDevice removedâ 0x" << std::hex << devHndl << std::dec << std::endl; break;
}
}
//---------------------------
// c++ demo
// This function demonstrates the device event registration usage
void DeviceEventHandlerDemo()
{
std::cout << â\n\n------------------------------â << std::endl;
std::cout << " Device Event Handler demonstration" << std::endl;
int eventCounter = 0; // number of events
// register our event handler with eventCounter
RegisterDeviceEventHandler(&MyDeviceEventFunction, &eventCounter);
std::cout << âWe need three events. Press button, and/or dis-/reconnect a deviceâ << std::endl;
while (eventCounter < 3) {
Sleep(100); // sleep for some milliseconds
}
// Unregister our event handler
RegisterDeviceEventHandler(NULL, NULL);
}
//-------------------------------------------------------------------------------
And my Xojo code is:
//-------------------------------------------------------------------------------
// prototype code in Xojo
// Located in the program Window Open event:
// The USB device is verified to be connected and a device handle (devHandle) is already obtained.
// Pragma; required or not? (Note: such a Pragma is not required for the other API calls for this device)
#Pragma X86CallingConvention StdCall
// âdevicedllâ is the external 3rd party Windows library (devicedll.dll); an equivalent macOS Framework could be used
// refcon was included to match the c++ demo but is it required?
// this Declare is NOT valid; a Syntax error is flagged by Xojo; it is accepted (but does nothing) if âReturnâ and text after it is removedâŠ
Soft Declare Function RegisterDeviceEventHandler Lib â@executable_path/âŠ/âŠ/devicedllâ (callback As Ptr, refCon As Ptr) As Ptr Return RegisterDeviceEventHandler(AddressOf MyDeviceEventFunction, refCon)
//-------------------------------------------------------------------------------
// Method: (TO BE TESTED WHEN CALLBACK WORKS!)
MyDeviceEventFunction(devHndl As Integer, devEvent As Integer, refCon As Ptr)
const eArrival = &h11 //< device plugged-in */
const eDeparture = &h12 //< device unplugged */
const eButtonPressed = &h01 //**< button pressed */
// refCon optional?
Select Case devEvent
Case eButtonPressed
MsgBox âButton pressed!â
Case eArrival
MsgBox âDevice attachedfâ
Case eDeparture
MsgBox âDevice removedâ
End Select
//-------------------------------------------------------------------------------
Thank-you!