-1

I have a C third-party library. In the header there is following function

bool bSetSelectMenuItemCallback(int( *pFunction )( int*  piSelectedIndex, int iItemCount, int iTimeoutSec, const char* pcItemList, const char* pcFormHeader ) );

How to pass C++ function to this callback?

There is ActiveX(ocx) wrapper for this library, and inside it has following event

Event SelectMenuItem(string sFormHeader, long lTimeout, long lItemCount, 
string sItemList, long plStatus, long plSelectedIndex)

We can see there is additional argument plStatus which is the result of the return of int( *pFunction ) as I understand. And you can easily subscribe to this event using something like

void Init() {
    OcxLib ocx = new OcxLib()
    ocx.SelectMenuItem += OnMenu();
}
void OnMenu(string sFormHeader, long lTimeout, long lItemCount,
    string sItemList, long plStatus, long plSelectedIndex) {
    Console.WriteLine("Inside event");
}

I want to do the same but in C++ directly(without using the ocx lib).

Shinigami
  • 1
  • 1
  • 3
  • 4
    `bSetSelectMenuItemCallback(&your_function);`? But `your_function` should match signature. (`int your_function(int*, int, int, const char*, const char*)`). – Jarod42 Aug 30 '19 at 08:03
  • Possible duplicate of [convert std::bind to function pointer](https://stackoverflow.com/questions/13238050/convert-stdbind-to-function-pointer) – darune Aug 30 '19 at 08:36
  • Just curious: how can a `C` library have a function declared `bool` ?? – Adrian Mole Aug 30 '19 at 10:49
  • What have you tried so far? Could you give us an example of your callback function's declaration and your attempt to pass it to `bSetSelectMenuItemCallback`? – JaMiT Aug 30 '19 at 14:12
  • @JaMiT i already solved the issue, Jarod42's advice helped. But before i was doing something like this ```typedef int(*SelectMenuItemFunctionPtr)(int*, int, int, const char*, const char*); SelectMenuItemFunctionPtr callback = OnSelectMenu; u->bSetSelectMenuItemCallback(callback); ``` – Shinigami Aug 30 '19 at 17:29
  • @Shinigami This question appears to still be open. Perhaps you could add the explanation of what you were doing to the question, then add an answer covering how you solved it? – JaMiT Aug 30 '19 at 17:45

1 Answers1

0

solved with

u->bSetSelectMenuItemCallback(&OnSelectedMenu);

int OnSelectedMenu(int* piSelectedIndex, int iItemCount, int iTimeoutSec, const char* pcItemList, const char* pcFormHeader){
    std::cout << "inside callback" << std::endl;
    return 0;
}
Shinigami
  • 1
  • 1
  • 3