Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / MFC

The Win32 Foundation Classes (WFC) - Version 45

Rate me:
Please Sign up or sign in to vote.
4.93/5 (40 votes)
16 May 2000 470.3K   12.7K   280  
The Win32 Foundation Classes (WFC) are a library of C++ classes that extend Microsoft Foundation Classes (MFC) beyond mere GUI applications, and provide extensive support for system and NT specific applications
#include "ado2xml.h"
#pragma hdrstop

/*
** Author: Samuel R. Blackburn
** Internet: wfc@pobox.com
**
** You can use it any way you like as long as you don't try to sell it.
**
** Any attempt to sell WFC in source code form must have the permission
** of the original author. You can produce commercial executables with
** WFC but you can't sell WFC.
**
** Copyright, 2000, Samuel R. Blackburn
**
** $Workfile: output_schema.cpp $
** $Revision: 2 $
** $Modtime: 3/21/00 4:49a $
** $Reuse Tracing Code: 1 $
*/

#if ! defined( WFC_STL )

#if defined( _DEBUG )
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif // _DEBUG

#endif // WFC_STL

bool output_schema( _ConnectionPtr& connection_p, const CString& table_name, const CString& where_clause )
{
   WFCTRACEINIT( TEXT( "output_schema()" ) );

   if ( table_name.GetLength() <= 0 )
   {
      WFCTRACE( TEXT( "table_name is empty!" ) );
      return( false );
   }

   HRESULT result = S_OK;

   _RecordsetPtr recordset_p = NULL;

   try
   {
      result = recordset_p.CreateInstance( __uuidof( Recordset ) );
   }
   catch( _com_error &exception )
   {
      WFCTRACEVAL( TEXT( "COM exception caught at line " ), __LINE__ );
      print_COM_error( exception );
   }
   catch( ... )
   {
      WFCTRACEVAL( TEXT( "Exception caught at line " ), __LINE__ );
   }

   if ( FAILED( result ) )
   {
      WFCTRACEVAL( TEXT( "Failed at line " ), __LINE__ );
      return( false );
   }

   CString select_string( TEXT( "SELECT * FROM [" ) );

   select_string += table_name;
   select_string += TEXT( "]" );

   if ( where_clause.GetLength() > 0 )
   {
      select_string += TEXT( " " );
      select_string += where_clause;
   }

   _bstr_t select_string_as_bstring( select_string );

   try
   {
      result = recordset_p->Open( select_string_as_bstring,
                                  _variant_t( (IDispatch *) connection_p ),
                                  adOpenForwardOnly,
                                  adLockReadOnly,
                                  adCmdText );
   }
   catch( _com_error &exception )
   {
      WFCTRACEVAL( TEXT( "COM exception caught at line " ), __LINE__ );
      print_COM_error( exception );
   }
   catch( ... )
   {
      WFCTRACEVAL( TEXT( "Exception caught at line " ), __LINE__ );
   }

   if ( FAILED( result ) )
   {
      WFCTRACEVAL( TEXT( "Failed at line " ), __LINE__ );
      recordset_p->Close();
      return( false );
   }

   WFCTRACEVAL( TEXT( "Number of fields in table is " ), recordset_p->Fields->Count );

   _tprintf( TEXT( "<ADO2XML:SCHEMA>\n" ) );
   _tprintf( TEXT( "   <TABLE>\n" ) );

   CString output_string( table_name );
                  
   normalize_string( output_string );

   _tprintf( TEXT( "      <NAME>%s</NAME>\n" ), (LPCTSTR) output_string );

   // Now call output_fields_schema( recordset_p->Fields ) which will
   // call output_field_shcema( Fields[ index ] );

   int field_index = 0;

   DWORD data_type = 0;

   CString data_type_name;

   try
   {
      while( field_index < recordset_p->Fields->Count )
      {
         // We must cast the field_index from an int to a short because
         // Microsoft doesn't know how to convert an int to a variant but they
         // do know how to convert a short to a variant. Go figure.

         try
         {
            data_type_name = (LPCTSTR) recordset_p->Fields->Item[ (short) field_index ]->GetName();
         }
         catch( _com_error &exception )
         {
            WFCTRACEVAL( TEXT( "COM exception caught at line " ), __LINE__ );
            print_COM_error( exception );
         }
         catch( ... )
         {
            WFCTRACEVAL( TEXT( "Exception caught at line " ), __LINE__ );
         }

         WFCTRACEVAL( TEXT( "Field name is " ), data_type_name );

         _tprintf( TEXT( "      <FIELD>\n" ) );

         normalize_string( data_type_name );

         _tprintf( TEXT( "         <NAME>%s</NAME>\n" ), (LPCTSTR) data_type_name );
         _tprintf( TEXT( "         <INDEX>%lu</INDEX>\n" ), (unsigned long) field_index );

         try
         {
            data_type = recordset_p->Fields->Item[ (short) field_index ]->GetType();
         }
         catch( _com_error &exception )
         {
            WFCTRACEVAL( TEXT( "COM exception caught at line " ), __LINE__ );
            print_COM_error( exception );
         }
         catch( ... )
         {
            WFCTRACEVAL( TEXT( "Exception caught at line " ), __LINE__ );
         }

         output_types( data_type );

         // Now output size information

         switch( data_type )
         {
            case adEmpty:

               break;

            case adTinyInt:
            case adSmallInt:
            case adInteger:
            case adBigInt:
            case adUnsignedTinyInt:
            case adUnsignedSmallInt:
            case adUnsignedInt:
            case adUnsignedBigInt:
            case adSingle:
            case adDouble:
            case adCurrency:
            case adDecimal:
            case adNumeric:
            case adBoolean:
            case adError:
            case adUserDefined:
            case adVariant:
            case adIDispatch:
            case adIUnknown:
            case adGUID:
            case adDate:
            case adDBDate:
            case adDBTime:
            case adDBTimeStamp:
            case adBSTR:
            case adChar:
            case adVarChar:
            case adLongVarChar:
            case adWChar:
            case adVarWChar:
            case adLongVarWChar:
            case adBinary:
            case adVarBinary:
            case adLongVarBinary:
            case adChapter:
            case adFileTime:
            case adDBFileTime:
            case adPropVariant:
            case adVarNumeric:
            default:

               _tprintf( TEXT( "         <SIZE>%lu</SIZE>\n" ), (unsigned long) recordset_p->Fields->Item[ (short) field_index ]->GetDefinedSize() );

               break;
         }

         _tprintf( TEXT( "      </FIELD>\n" ) );

         field_index++;
      }
   }
   catch( _com_error &exception )
   {
      WFCTRACEVAL( TEXT( "COM exception caught at line " ), __LINE__ );
      print_COM_error( exception );
   }
   catch( ... )
   {
      WFCTRACEVAL( TEXT( "Exception caught at line " ), __LINE__ );
   }

   try
   {
      recordset_p->Close();
   }
   catch( _com_error &exception )
   {
      WFCTRACEVAL( TEXT( "COM exception caught at line " ), __LINE__ );
      print_COM_error( exception );
   }
   catch( ... )
   {
      WFCTRACEVAL( TEXT( "Exception caught at line " ), __LINE__ );
   }

   _tprintf( TEXT( "   </TABLE>\n" ) );
   _tprintf( TEXT( "</ADO2XML:SCHEMA>\n" ) );

   return( true );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
I'm just a simple little NT programmer. Most of the work I do is remote controlling equipment in real time. I started out using Windows 3.0. Then came 3.1 and then NT. I started using NT but unfortunately, Microsoft didn't. I started using MFC but unfortunately, Microsoft didn't (and still doesn't) put any real support for NT into MFC so I wrote a bunch of C++ classes to make my life easier. Like all class libraries, mine grew. Now I'm giving it away, I call it Win32 Foundation Classes.

Check out Sam's homepage at www.SamBlackburn.com/wfc/.

Comments and Discussions