This post is a follow-up of a related question posted here by Ran. The accepted answer sticks to the use of the usual a plain old function. This excerpt particularly catch my attention:
With the firm conviction that there should be a way to use a kind of "parameters" adapter (to rephrase get rid of the uneeded Self implicit reference and provide a pointer to a complying adapted callback function), I end up finding this article entitled Callback a class by Peter Morris. To sum up, he uses thunking technique as adaptation trick. (Disclaimer: I never tested the code). I know it's not very clean as a solution but it allows OO design with all the supposed benefits. My Question: Knowing that TCallbackThunk is based on the callback function signature, what would be the answer of the above refered post if doing it as Peter Morris did is the way to go? . | |||||||||||||||||||||
|
You don't really need to go through all that work since To adapt the answer to use Morris's code, you'll first need to make sure the signature of the callback method matches the signature of the API's callback function. Since we're calling
Next, we set up the However, we don't use the way Morris described. His code puts the data structure on the stack. That means we're putting executable code on the stack. Modern processors and operating systems don't allow that anymore — the OS will halt your program. We could get around that by calling
Note that those are 32-bit x86 instructions in that record. I have no idea what the corresponding x86_64 instructions would be. | |||||||||||||||||
|
联系客服