AfxWinInit

作为基于 GUI 的应用程序的 CWinApp 初始化的一部分,此函数由由 MFC 提供的 WinMain 函数时,初始化, MFC。

BOOL AFXAPI AfxWinInit(
   HINSTANCE hInstance,
   HINSTANCE hPrevInstance,
   LPTSTR lpCmdLine,
   int nCmdShow 
);

参数

  • hInstance
    当前正在运行的模块的句柄。

  • hPrevInstance
    对应用程序的上一个实例的句柄。 对于基于 Win32 的应用程序,此参数始终是 NULL

  • lpCmdLine
    指向指定命令行的一个 NULL 终止的字符串为应用程序。

  • nCmdShow
    指定 GUI 应用程序的主窗口将如何显示。

备注

对于控制台应用程序,不使用由 MFC 提供的 WinMain 功能,您必须直接调用 AfxWinInit 初始化 MFC。

如果调用 AfxWinInit ,应声明 CWinApp 类的实例。 对于控制台应用程序中,可以选择从 CWinApp 不派生您的类并不直接使用 CWinApp 实例。 ,如果您的 的实现,决定将应用程序的所有功能保留此方法很合适。

备注

在创建程序集时激活上下文,则 MFC 使用用户模块提供的清单资源。激活上下文在 AfxWinInit创建。有关更多信息,请参见 为MFC模块状态的激活上下文支持

示例

#include <afx.h>
#include <afxdb.h>

int _tmain(int /*argc*/, TCHAR* /*argv[]*/, TCHAR* /*envp[]*/)
{
   int nRetCode = 0;

   // initialize MFC and print and error on failure
   if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
   {
      // TODO: change error code to suit your needs
      _tprintf(_T("Fatal Error: MFC initialization failed\n"));
      nRetCode = 1;
   }
   else
   {
      // try to connect to an ODBC database that doesn't exist
      // (this wouldn't work at all without initializing MFC)

      CDatabase db;
      try
      {
         db.Open(_T("This Databsae Doesn't Exist"));

         // we shouldn't realistically get here

         _tprintf_s(_T("Successful!\n")
            _T("Closing ...\n"));
         db.Close();
         _tprintf_s(_T("Closed!"));
      }
      catch (CDBException* pEx)
      {
         // we got an exception! print an error message
         // (this wouldn't work without initializing MFC)

         TCHAR sz[1024];

         _tprintf_s(_T("Error: "));
         if (pEx->GetErrorMessage(sz, 1024))
            _tprintf_s(sz);
         else
            _tprintf_s(_T("No error message was available"));
         _tprintf_s(_T("\n"));

         pEx->Delete();

         nRetCode = 1;
      }
   }

   return nRetCode;
}

要求

Header: afxwin.h

请参见

参考

CWinApp 类

主:启动程序

WinMain

概念

MFC宏和Globals

CWinApp: The Application 类