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