Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C++
Reference
Libraries Reference
MFC
Macros and Globals
 AfxWinInit
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
MFC Library Reference 
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.

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.

NoteNote

When it creates an activation context for an assembly, MFC uses a manifest resource provided by the user module. The activation context is created in AfxWinInit. For more information, see Support for Activation Contexts in the MFC Module State.

// 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;
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker