1

I have the following export function in a DLL that I am writing

DLLEXPORT int HttpFilterProc(FilterContext* context, unsigned int eventType, void* eventData) {
    switch (eventType) {
    case kFilterAuthUser:
        return Authenticate(context, eventData);
    default:
        return kFilterNotHandled;
    }
}

This method signature is not in my control so i cannot change it, I want to call into the following method

int Authenticate(FilterContext *context, FilterAuthenticate *authData) {
    FilterRequest requestInfo;
    char temp[255] = " ";
    int errId;
}

I know that for the case kFilterAuthUser that the type of void* eventData will be a FilterAuthenticate typedef struct.

The issue is that I get a compile error with the above code and I do not understand why, I am following this from examples that work so I know that this should work. I am using Visual Studio 2015 and writing a Win32 DLL can anyone explain why I am getting this error and suggest how I can resolve please.

Neil Stevens
  • 3,534
  • 6
  • 42
  • 71

3 Answers3

4

As mentioned in the comment, you need to typecast void* parameter to the desired type (FilterAuthenticate* in your case) before passing it to Authenticate(....).

Do this:

return Authenticate( context, static_cast<FilterAuthenticate*>(eventData) );
CinCout
  • 9,486
  • 12
  • 49
  • 67
  • Whats the difference between `dynamic_case` and `(T *)` – Neil Stevens Jan 21 '16 at 12:57
  • @Neil The former is a `C++` style cast, while the latter is `C` style cast. And its cast, not case! – CinCout Jan 21 '16 at 12:59
  • @Neil : Please refer this for more explanation , http://stackoverflow.com/questions/6855686/what-is-the-difference-between-static-cast-and-reinterpret-cast – Akhil V Suku Jan 21 '16 at 13:01
  • 1
    @Neil: `(T*)`: Dear compiler, you are an idiot, I know better than you. This is a T*. Even if you know it isn't. `dynamic_cast` (which wouldn't work here) - please (possibly at runtime) see if you can take this pointer and convert it to a pointer to a T, using inheritance information. static_cast - If you can do this at compile time without breaking the type system, please treat this as a T. Note that you can static_cast to a T* because the language says you can, but if the parameter wasn't a T* to begin with, bad things may happen. – Tom Tanner Jan 21 '16 at 13:02
  • @Neil more here: http://stackoverflow.com/questions/1609163/what-is-the-difference-between-static-cast-and-c-style-casting – CinCout Jan 21 '16 at 13:07
3

You get the error because you pass a void pointer to a function that expects a pointer of another type. Since void pointers are not implicitly convertible to other pointers in c++, trying to do so is an error.

You can explicitly convert a void pointer to any other pointer though. So, if you know what type of object the pointer actually points to, then you'll need to share you knowledge with the compiler by casting to the correct pointer type with static_cast.

edmz
  • 8,220
  • 2
  • 26
  • 45
eerorika
  • 232,697
  • 12
  • 197
  • 326
2

you can call it like.

   DLLEXPORT int HttpFilterProc(FilterContext* context, unsigned int eventType, void* eventData) {
        switch (eventType) {
        case kFilterAuthUser:
            return Authenticate(context, (FilterAuthenticate *)eventData);
        default:
            return kFilterNotHandled;
        }
    }

You have to cast the void * data to FilterAuthenticate *.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Akhil V Suku
  • 870
  • 2
  • 13
  • 34