Reputation: 3914
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.
Upvotes: 1
Views: 15445
Reputation: 912
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 *
.
Upvotes: 2
Reputation: 238461
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
.
Upvotes: 3
Reputation: 9619
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) );
Upvotes: 4