I am using a C library which has callback functions such as:
int DownloadStream(LPCTSTR host,DWORD channelNumber,NewImage pNewImage);
typedef int (*NewImage)(BYTE *pData, int nLen,void *returnHandle);
In C i can make call like this:
int CallDownloadStream(LPCTSTR host,DWORD channelNumber)
{
int hr = DownloadStream(host,channelNumber,OnNewImage);
return hr;
}
int OnNewImage(BYTE *pData, int nLen,void *returnHandle)
{
// able to get data
}
What i want is call those function as a member of C++ class such as:
class MyClass
{
public:
int CallDownloadStream(LPCTSTR host,DWORD channelNumber)
{
int hr = DownloadStream(host,channelNumber,OnNewImag);
return hr;
}
int OnNewImage(BYTE *pData, int nLen,void *returnHandle)
{
// able to get data
}
}
I can not able to compile it. Is it possible to use those callback function in that way? If so how can do this?
PS: I have no control over original C style callback functions.
Cannot be done. If the C function does not provide for a user-supplied argument pointer, which is idiomatic in C, then you cannot do this.
Edit: This isn’t strictly true. You can use a static variable, you can use thread-local storage, or you can JIT a function. However, those are not general-case solutions.