Ojav
Ojav

Reputation: 980

How to use WINAPI function (that isn´t Callback) inside a class?

I want to create a class that uses the WINAPI AddVectoredExceptionHandler.

I googled a bit and everybody says to make it static since the WINAPI is purely C and doesnt know classes.

If I make it static I am not able to use a class member inside the static function.

After that I googled a bit on "how to call non-static method from static method of same class? c++"

But didnt found anything but for Callback WINAPI functions.

https://www.experts-exchange.com/articles/655/How-to-provide-a-CALLBACK-function-into-a-C-class-object.html

How can I use this WINAPI in a class? since it isnt a Callback function? (I also googled Callback function) (Doesnt seem to be one?)

class VEH
{
 public:
    VEH();
private: 
    void functionA(int a);
    /*static*/ LONG ExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo);

};




VEH::VEH()
{
    AddVectoredExceptionHandler(2,(PVECTORED_EXCEPTION_HANDLER)ExceptionHandler); //INVALID TYPE CONVERSION (if non static)
}


void functionA(int a){

}  


LONG ExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo){
functionA(2); //a non-static member reference must be relative to a specific object (if static)
}

Upvotes: 0

Views: 267

Answers (2)

Ojav
Ojav

Reputation: 980

Thanks to Remy Lebeau for providing an Answer to SergeyA who answers my questions but didnt provide a method to do it.

One "hackish" way would be to use a thunk for the actual VEH callback, and then store the class object pointer inside of that thunk. The thunk can then call methods on the class object as needed when itself is called by a vectored exception being raised. – Remy Lebeau 13 hours ago

After googling what a "thunk" is

What is a 'thunk'?

I just followed his comment.

Upvotes: 0

SergeyA
SergeyA

Reputation: 62573

You are out of luck (almost). Since AddVectoredExceptionHandler doesn't seem to be able to store any context beyond the handle, there is no legal C++ way to call a non-static member function from it. (There is a hackish way, but I see no reason to recommend it now). Remember, calling a non-static member function requires to provide a instance of the class as well, and there is simply nowhere you can put this instance.

However, the function AddVectoredExceptionHandler is global. It should not be specific to any specific class instance, and because of that it makes no sense to have a per-object flavor of it. Once the exception happens, it happens - and it is not related to any particular class or object in your program. So static class member function or even global function should be enough for you.

Bottom line: you can't, and you most likely do not need to.

Upvotes: 2

Related Questions