简体   繁体   中英

Can I override a globally defined API in a C++/MFC project?

I need to ensure that message boxes are shown only if my app is running in an interactive desktop. So I was thinking, can I override ::MessageBox and AfxMessageBox with my own implementation (from a C++/MFC project, compiled with VS 2008)?

PS. I do not need global hooks. This needs to work only from my app's code. For instance, if somewhere in the code I have ::MessageBox(...); it should let me call my own method instead of linking to the system API.

The default MessageBox (actually MessageBoxA or MessageBoxW) is defined as an export, so unfortunately you can't just define a local version to link in first. If you have access to all the source that uses MessageBox it's simple to redefine the MessageBox macro after including windows.h though:

#include <windows.h>
#undef MessageBox
#define MessageBox MyMessageBox

If you have 3rd party binaries or something where you don't compile the code this wouldn't work of course.

如果使用MFC,则可以覆盖CWinApp :: DoMessageBox函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM