Export (0) Print
Expand All
TRY
Expand Minimize

AfxWinInit

This function is called by the MFC-supplied WinMain function, as part of the CWinApp initialization of a GUI-based application, to initialize MFC.

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

Parameters

hInstance
The handle of the currently running module.
hPrevInstance
A handle to a previous instance of the application. For a Win32-based application, this parameter is always NULL.
lpCmdLine
Points to a null-terminated string specifying the command line for the application.
nCmdShow
Specifies how the main window of a GUI application would be shown.

Remarks

For a console application, which does not use the MFC-supplied WinMain function, you must call AfxWinInit directly to initialize MFC.

If you call AfxWinInit yourself, you should declare an instance of a CWinApp class. For a console application, you might choose not to derive your own class from CWinApp and instead use an instance of CWinApp directly. This technique is appropriate if you decide to leave all functionality for your application in your implementation of main.

The TEAR sample shows how to make a console application using MFC.

Example

// this file must be compiled with the /GX and /MT options:
//      cl /GX /MT thisfile.cpp

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

int main()
{
   // try to initialize MFC

   if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
   {
      cerr << "MFC failed to initialize!" << endl;
      return 1;
   }

   // 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("This Databsae Doesn't Exist");

      // we shouldn't realistically get here

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

      char sz[1024];

      cout << "Error: ";
      if (pEx->GetErrorMessage(sz, 1024))
         cout << sz;
      else
         cout << "No error message was available";
      cout << endl;

      pEx->Delete();
      return 1;
   }

   return 0;
}

See Also

MFC Macros and Globals | CWinApp | CWinApp: The Application Class | main | WinMain

Show:
© 2014 Microsoft