Casting functions

Hi,

I'm new to c++ and am trying to decipher some code that I've inherited. In a couple of places it's doing things like this:

h=GetModuleHandle("SomeDLL")
ptr1=(void *)((long)SomeFunction-((long)h+0x9e41)-5);

This looks to me like it's casting the function SomeFunction to a long (which I assume gets its address in memory), subtracting some arbitrary value from it (based on the base address of SomeDLL), and casting the result to a pointer.

But I'm not really sure: This all seems a long way from the safe confines of my usual c# world!

Could someone please confirm my analysis, or correct it if I'm wrong?

cheers
ak
[715 byte] By [akamal] at [2007-11-20 11:58:10]
# 1 Re: Casting functions
It seems that your analysis is correct. And I would be careful when using such code as it wouldn't surprise me if the behaviour changes between different compilers, OS's, hotfixes, and ofcourse between different versions of the DLL...

- petter
wildfrog at 2007-11-11 4:01:57 >
# 2 Re: Casting functions
Thanks wildfrog. It is pretty scary code to be honest:
A DLL get's injected into a running app, patches various memory points to inject calls to the DLL's functions and modify the behaviour of the app. It seems this is necessary as the source for the orginal program is lost.

You're right about it being fragile as well. Although there is only one version of both the app and the DLL, changes to some of the injected functions can easily cause the app to crash due to access violations.

Thanks for your help,
ak
akamal at 2007-11-11 4:03:00 >