Is it possible to Invoke an exported “private” met

2019-07-19 06:03发布

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 :)

2条回答
Bombasti
2楼-- · 2019-07-19 06:43

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.

查看更多
叼着烟拽天下
3楼-- · 2019-07-19 07:03

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.

查看更多
登录 后发表回答