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;
}

要求

标头: afxwin.h

请参见

参考

CWinApp 类

main:程序启动

WinMain

概念

MFC 宏和全局函数

CWinApp:应用程序类