15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


CDao usage from console app ?

Don O'Hara -- dohara@epix.net
Friday, November 22, 1996

Environment: VC++ 4.2-flat, Windows 95

I am having run-time cleanup problems with code similar
to the following. I can open the db OK, but am
getting  assertion failures in DAOCORE.CPP, line 36,
when the program shuts down.

(i loaded the source from the daosdk, but can't find that module).

Am i missing something needed by mfc console apps??

I'd like to keep this a console app if possible...

thanks!

//==================================

// cn_mfc.cpp

#include  
#include  
#include  

CDaoDatabase        cdb;
CDaoException       exception ;

//==================================
void main()
{
    
   cout <<  "  starting..." << endl ;

   cdb.Open( "d:\\amm\\cn_mfc\\tables\\ammd_95.mdb") ;

   if ( cdb.IsOpen() ) 
   {
        cout << "open !" <<  endl ;
        cdb.Close() ;
   }
   else
        cout << "closed !" <<  endl ;

   cout <<  "  ending " <<  endl ;

   return ;        
}








Neil A. Van Note -- vannote@netaxs.com
Sunday, November 24, 1996

[Mini-digest: 2 responses]

Hello Don,

> Environment: VC++ 4.2-flat, Windows 95
> 
> I am having run-time cleanup problems with code similar
> to the following. I can open the db OK, but am
> getting  assertion failures in DAOCORE.CPP, line 36,
> when the program shuts down.
> 
> (i loaded the source from the daosdk, but can't find that module).
> 
> Am i missing something needed by mfc console apps??
> 
> I'd like to keep this a console app if possible...


Look at AfxDaoTerm(), Considering you don't have a CWinApp
object here, you must call this explicitly before your main
exits. Something like the following works fine...

	if (!AfxGetApp())
		AfxDaoTerm();

Cheers,

	Neil A. Van Note

	R&D Software Engineer
	XRT Financial Systems, Inc.
	
	http://www.xrt.com/
	nvannote@xrt.com
	vannote@netaxs.com


-----From: Mike Blaszczak 

At 18:22 11/22/96 -0500, Don O'Hara wrote:
>Environment: VC++ 4.2-flat, Windows 95

>I am having run-time cleanup problems with code similar
>to the following. I can open the db OK, but am
>getting  assertion failures in DAOCORE.CPP, line 36,
>when the program shuts down.

This assertion usually means that you didn't properly shut down DAO.

>(i loaded the source from the daosdk, but can't find that module).

This file name is actually an MFC file.  You're using CDaoDatabase
and that's an MFC class, not a DAO SDK class.

>Am i missing something needed by mfc console apps??

Your code has lots of problems, and I'm not sure I can address them
all.  You're missing something needed by _any_ MFC-based DAO
application, wether it uses MFC or not.


1) You declare a global instance of CDaoException.  There's no reason
   to do that--you never reference it, and nobody else will ever
   reference it.

2) Ironically, you never bother to check any return codes. 
   CDaoDatabase::Open() can throw an excpetion, and for most people
   on the list who try and run your code, it will--they won't have
   a database file at D:\AMM\CN_MFC\TABLES\AMMD_95.MDB.  Someday,
   your code will break for you and your users--and they'll end up
   looking at nothing more than an "unhandled exception" error
   message.

3) You never call AfxDaoInit() or AfxDaoTerm(). This is probably
   what directly causes the assertion that you're tripping; you're
   shutting down MFC and you haven't shut down DAO.  MFC realizes
   that's a great way to leak resources, and is trying to help you
   by pointing out the fallacy in your code.

4) You never initialize MFC.  You don't need to shut down MFC, but
   if you're using DAO, it would be a good idea to get MFC properly
   initialized.

5) It's rather risky to use MFC objects at the global scope because
   MFC might or might not have been initialized before the objects
   are constructed.

I think that a corrected version of your sample might look
something more like this:

//==================================
// cn_mfc.cpp

#include  
#include  
#include  

CDaoDatabase cdb;

CWinApp theApp; // don't even need to make your own

//==================================
void main()
{
   if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
      return;  // game over, man

   AfxDaoInit();
   cout <<  "  starting..." << endl ;

   try
   {
      cdb.Open( "c:\\dumpsome\\employee.mdb") ;
   }
   catch (CDaoException* pEx)
   {
      char sz[1023];
      pEx->GetErrorMessage(sz, 1023);
      cout << sz;
      return;
   }

   if ( cdb.IsOpen() ) 
   {
      cout << "open !" <<  endl ;
      cdb.Close() ;
   }
   else
      cout << "closed !" <<  endl ;

   cout <<  "  ending " <<  endl ;

   AfxDaoTerm();
   return ;        
}


By properly initializing MFC, and by properly starting and shutting down
DAO, you'll be in much better shape.

.B ekiM
http://www.nwlink.com/~mikeblas/
I'm afraid I've become some sort of speed freak.
These words are my own. I do not speak on behalf of Microsoft.





| Вернуться в корень Архива |