有关MFC的afx_module_state类的作用,visual c++
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有关MFC的afx_module_state类的作用,visual c++相关的知识,希望对你有一定的参考价值。
winmain的初始化会用到AFX_MODULE_STATE,类似的还有AFX_THREAD_STATE, 字面上很好理解,但不知其在MFC中的作用。
我是这样理解的:这个类用于保存全局变量, 由于在initinstance或RUN()中操作的类或变量会是局部变量,而函数结束后这些变量就会被释放,无法形成正常的程序。所以需要用AFX_MODULE_STATE将他们设成全局的-——————是不是这个意思?望高手解释
module 就是一个 PE文件,AFX_MODULE_STATE 管理 PE文件被运行方式载入内存后的信息,比如 模块名称模块基址资源地址是否DLL,是否系统模块,还有主线程对象,导入dll链表,OLE控制信息等。这些信息大部分都是从PE文件中来——看看PE文件结构就行了。
AFX_THREAD_STATE 线程状态信息, 每个程序至少有一个线程,MFC给每个MFC模式创建的线程绑定一个 AFX_THREAD_STATE 对象(注:MFC线程才有,API方式创建的是没有的),里面核心信息就是 几个CHandleMap指针对象,管理DC/HWND/GDI/HMENU的,具体的为什么要放在这里就不展开描述了,经常用MFC的会体会的到。其他的几个成员辅助使用,tooltip,hittest,flyby,是程序运行期间为某些功能需要辅助使用的变量,非MFC程序也很容易实现这些功能的。 参考技术B 如果我没记错的话,这个宏一般是不会直接调用的,事实上这个宏是MFC用来构造消息映射表时用到的辅助宏,它埋在类识别宏或动态识别宏中的,具体的实现你可以参看《深入浅出MFC》的第三至八章,里面有具体的构造解说
如何从VS2008代码分析中排除MFC代码
【中文标题】如何从VS2008代码分析中排除MFC代码【英文标题】:How to exclude MFC code from VS2008 Code Analysis 【发布时间】:2009-06-08 10:08:57 【问题描述】:我有一个 C++/CLI 项目,它使用 CWinFormsControl 和 CWinFormsView 在我的 MFC 视图中创建 .NET 控件。为此,我需要#include "afxwinforms.h"。
当我对项目运行代码分析时,我总是收到有关我包含的一些 MFC 类的警告。例子:
3>Running Code Analysis...
3>c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwinforms.h(87) : warning: CA1704 : Microsoft.Naming : In method 'CWin32Window::CWin32Window(HWND__*)', correct the spelling of 'Wnd' in parameter name 'hWnd' or remove it entirely if it represents any sort of Hungarian notation.
3>c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwinforms.h(87) : warning: CA1704 : Microsoft.Naming : In method 'CWin32Window::CWin32Window(HWND__*)', correct the spelling of 'h' in parameter name 'hWnd' or remove it entirely if it represents any sort of Hungarian notation.
3>warning: CA1051 : Microsoft.Design : Because field 'CWinFormsEventsHelper::m_pControl' is visible outside of its declaring type, change its accessibility to private and add a property, with the same accessibility as the field has currently, to provide access to it.
3>warning: CA1051 : Microsoft.Design : Because field 'CWinFormsEventsHelper::m_pSink' is visible outside of its declaring type, change its accessibility to private and add a property, with the same accessibility as the field has currently, to provide access to it.
3>c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwinforms.inl(60) : warning: CA1704 : Microsoft.Naming : In method 'CWinFormsEventsHelper::OnHandleCreated(Object^, EventArgs^)', consider providing a more meaningful name than parameter name 'o'.
3>c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwinforms.inl(60) : warning: CA2109 : Microsoft.Security : Consider making 'CWinFormsEventsHelper::OnHandleCreated(Object^, EventArgs^)' not externally visible.
3>c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwinforms.inl(67) : warning: CA1704 : Microsoft.Naming : In method 'CWinFormsEventsHelper::OnHandleDestroyed(Object^, EventArgs^)', consider providing a more meaningful name than parameter name 'o'.
3>c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwinforms.inl(67) : warning: CA2109 : Microsoft.Security : Consider making 'CWinFormsEventsHelper::OnHandleDestroyed(Object^, EventArgs^)' not externally visible.
3>c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxwinforms.inl(54) : warning: CA1704 : Microsoft.Naming : Correct the spelling of 'Unadvise' in member name 'CWinFormsEventsHelper::Unadvise(IHandleEvents*)' or remove it entirely if it represents any sort of Hungarian notation.
3>warning: CA1823 : Microsoft.Performance : It appears that field 'context_node_base::_Needs_Context' is never used or is only ever assigned to. Use this field or remove it.
3>warning: CA1812 : Microsoft.Performance : 'context_node<char const *,System::String ^>' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static methods, consider adding a private constructor to prevent the compiler from generating a default constructor.
3>c:\program files\microsoft visual studio 9.0\vc\include\msclr\marshal.h(366) : warning: CA2201 : Microsoft.Usage : 'context_node<char const *,System::String ^>::context_node<char const *,System::String ^>(const char*&, String^)' creates an exception of type 'OutOfMemoryException', an exception type that is reserved by the runtime and should never be raised by managed code. If this exception instance might be thrown, use a different exception type.
显然我想取消这些 MFC 警告,以便更清楚地看到我自己的警告。但是怎么做?我不想为了解决这个问题而禁用我自己的代码中的任何警告。
【问题讨论】:
【参考方案1】:通过将以下内容添加到 stdafx.h,所有 MFC 警告都被删除(通过从错误列表窗口抑制警告,进入项目抑制文件,然后将它们移动到 stdafx 中找到)
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes", Scope="member", Target="msclr.interop.context_node<char const *,System::String ^>.#.ctor(modopt(System.Runtime.CompilerServices.IsConst),modopt(System.Runtime.CompilerServices.IsSignUnspecifiedByte)System.SByte*modopt(System.Runtime.CompilerServices.IsImplicitlyDereferenced)*,System.String)");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields", Scope="member", Target="Microsoft.VisualC.MFC.CWinFormsEventsHelper.#m_pControl");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields", Scope="member", Target="Microsoft.VisualC.MFC.CWinFormsEventsHelper.#m_pSink");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope="member", Target="msclr.interop.context_node_base.#_Needs_Context");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Scope="type", Target="msclr.interop.context_node<char const *,System::String ^>");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="Wnd", Scope="member", Target="Microsoft.VisualC.MFC.CWin32Window.#.ctor(HWND__*)");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="h", Scope="member", Target="Microsoft.VisualC.MFC.CWin32Window.#.ctor(HWND__*)");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="o", Scope="member", Target="Microsoft.VisualC.MFC.CWinFormsEventsHelper.#OnHandleCreated(System.Object,System.EventArgs)");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", Scope="member", Target="Microsoft.VisualC.MFC.CWinFormsEventsHelper.#OnHandleCreated(System.Object,System.EventArgs)");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="o", Scope="member", Target="Microsoft.VisualC.MFC.CWinFormsEventsHelper.#OnHandleDestroyed(System.Object,System.EventArgs)");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", Scope="member", Target="Microsoft.VisualC.MFC.CWinFormsEventsHelper.#OnHandleDestroyed(System.Object,System.EventArgs)");
CA_GLOBAL_SUPPRESS_MESSAGE("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="Unadvise", Scope="member", Target="Microsoft.VisualC.MFC.CWinFormsEventsHelper.#Unadvise(Microsoft.VisualC.MFC.IHandleEvents*)");
#include <afxwinforms.h> // MFC Windows Forms support
下一次:在发布 SO 问题之前必须三思而后行:D
【讨论】:
【参考方案2】:以这种方式包含 afxwinforms.h:
#pragma warning(push)
//disable whatever you need with #pragma warning( disable: warning number)
#include <afxwinforms.h>
#pragma warning(pop)
【讨论】:
您无法使用#pragma 抑制代码分析警告,但您的回答确实让我考虑得更仔细,这就是我给您+1 的原因以上是关于有关MFC的afx_module_state类的作用,visual c++的主要内容,如果未能解决你的问题,请参考以下文章