访问 Windows 2000 特定 API






4.56/5 (9投票s)
2000 年 2 月 29 日

101741
在访问新的 Windows 2000 API 时遇到问题?本文可能有所帮助。
为了访问 Win2000 特定的 API 等,需要在应用程序的 stdafx.h 文件中包含以下 #define(在任何其他 #include 之前)
#define _WIN32_WINNT 0x0500
使用最新的平台 SDK 时,这也会导致 OPENFILENAME 结构定义的改变;特别是它会变得更大。这是因为 "CommDlg.h" 中的以下行:typedef struct tagOFNA { DWORD lStructSize; ... ... LPCSTR lpTemplateName; #ifdef _MAC LPEDITMENU lpEditInfo; LPCSTR lpstrPrompt; #endif #if (_WIN32_WINNT >= 0x0500) void * pvReserved; DWORD dwReserved; DWORD FlagsEx; #endif // (_WIN32_WINNT >= 0x0500) } OPENFILENAMEA, *LPOPENFILENAMEA;
这意味着当编译 CFileDialog
类时,其 m_ofn 成员的大小将与 MFC DLL 中的大小不同。这是因为 CFileDialog
被定义为
class CFileDialog : public CCommonDialog { DECLARE_DYNAMIC(CFileDialog) public: // Attributes OPENFILENAME m_ofn; // open file parameter block ... ... };
当在代码中使用 CFileDialog
并在其销毁时,将使用错误的内存成员变量偏移量,特别是 m_strFilter 将无法正确销毁。
解决此问题的办法是在应用程序的 stdafx.h 中,在 #include <afxext.h> 之前 #undef _WIN32_WINNT
#define _WIN32_WINNT 0x0500 // allow Win2000 specific calls #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #undef _WIN32_WINNT // allow CFileDialog to build with the correct size #include <afxext.h> // MFC extensions #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls #ifndef _AFX_NO_AFXCMN_SUPPORT #include <afxcmn.h> // MFC support for Windows Common Controls #endif // _AFX_NO_AFXCMN_SUPPORT
现在,<afxext.h> 将(间接)#include <CommDlg.h>
并定义 CFileDIalog
,而不会包含冒犯性的 _WIN32_WINNT
。
注意:这仅适用于 VC_EXTRALEAN
阻止 <afxwin.h> 较早包含 CommDlg.h 的情况。