views:

152

answers:

3

This is C4055 Warning message.

'conversion' : from data pointer 'type1' to function pointer 'type2'
A data pointer is cast (possibly incorrectly) to a function pointer.
This is a level 1 warning under /Za and a level 4 warning under /Ze.

How do we resolve this warning?(By correct way, not a trick)

Edit:

This is a code snippet has warning.

typedef NTSTATUS (*t_ObRegisterCallbacks)
(
  IN POB_CALLBACK_REGISTRATION CallBackRegistration,
  OUT PVOID *RegistrationHandle
);

t_ObRegisterCallbacks   g_ObRegisterCallbacks = NULL;

void foo()
{
  g_ObRegisterCallbacks = (t_ObRegisterCallbacks)MmGetSystemRoutineAddress(&regName); //C4055
}
//warning C4055: 'type cast' : from data pointer 'PVOID' to function pointer 't_ObRegisterCallbacks'
+1  A: 

An explicit cast.

For more details, show the code that generates the warning.

Eli Bendersky
@sbi: casts to function pointers sometimes need to be explicit to pass the compiler, it depends on the code.
Eli Bendersky
Sorry Guys. I've attached the codes to following answer.
Benjamin
@Eli: "...it depends on the code" Um, I thought that was my argument?
sbi
+5  A: 

Casting between data and function pointers is not allowed. You can use the union hack for type conversion: union type containing both pointers, then write as one type and read as another. Needless to say, this is unportable as far as the standard is concerned, but it makes compilers happy and quite often works in practice.

Functions that return function pointers as data pointers (dlsym being one popular function of the kind) are broken by design and you can only get around that by tricks such as the union hack.

Tronic
+5  A: 

The WDK header files are not that clean. The return type for MmGetSystemRoutineAddress() should have been declare FARPROC instead of PVOID. Still, that doesn't matter on any machine for which you'd write device drivers with the WDK, a void* is convertible to a function address without problems, data and code pointers have the same size on 32- and 64-bit platforms. It is going to be a cold day in hell when we ever get that segmented memory model misery back.

I recommend you simply turn off the warning with #pragma warning(disable:4055)

Hans Passant
Thanks for your clear answer.
Benjamin