Is it possible to invoke a private method of a class exported from a DLL?
Will it be hidden from people who would like to use it but are not supposed to?
thanks :)
问题:
回答1:
Yes, it's possible, but you need to use dirty casting tricks or rely on semi-undefined behaviour, but for certainty you can call an exported function, no matter it's private/public status.
The language does not provide security against malicious attackers. It will help everyone play by the rules, but it will not guard against those who try to break the system.
For instance:
- use
GetProcAddress()
to get the function's address, cast it to the right member function type, and call it. - create a modified header file of the class, declaring everything as public (or just add a static function,
void crowbar()
), compile against that. (Undefined behaviour, since you're violating the One Defintion Rule, but it will probably work...)
Do not rely on C++ private keyword for security.
回答2:
If it appears in the DLL's export table, it can be invoked by using GetProcAddress
and calling the returned function pointer. There are some technical hurdles to get the right calling convention, but it is possible (most likely some assembly language will be required).
Strictly speaking, any function for which the compiler generates an out-of-line instance can be called by any native code. Being exported by a DLL just makes it far easier to find the address of the code for the function.