home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / DBLOGIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  5.5 KB  |  165 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // Callback.C
  4. #include "snipit.h"
  5.  
  6. static const DBIPATH szTblName      = "security";
  7. static const char szTblType[]       = szDBASE;
  8. pCHAR  CallBackData3;
  9.  
  10. CBRType DBIFN _export CallBackFunc3(CBType ecbType, UINT32 iClientData,
  11.                                     pVOID pCbInfo);
  12.                                   
  13. //=====================================================================
  14. //  Function:
  15. //          LoginCallback();
  16. //
  17. //  Description:
  18. //          This example shows how to access a dBASE table which is missing
  19. //          it's .MDX file. This is accomplished with the use of the
  20. //          cbINPUTREQ callback. Note that this method can also be used
  21. //          to access FOX and CLIPPER tables.
  22. //=====================================================================
  23. void
  24. LoginCallback (void)
  25. {
  26.     hDBIDb      hDb;                        // Handle to the database
  27.     hDBICur     hCur;                       // Handle to the table
  28.     UINT16      uNumOfRecs = 5;             // No. of records to display
  29.     CBLoginDesc CbInfo;                     // Variable which is used within
  30.                                             //   the callback
  31.     DBIResult   rslt;                       // Return value from IDAPI
  32.                                             //   functions
  33.  
  34.     Screen("*** Open Table CallBack Example ***\r\n");
  35.  
  36.     BREAK_IN_DEBUGGER();
  37.  
  38.     Screen("    Initializing IDAPI...");
  39.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  40.     {
  41.         Screen("*** End of Example ***");
  42.         return;
  43.     }
  44.  
  45.     Screen("    Setting the database directory...");
  46.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  47.     ChkRslt(rslt, "SetDirectory");
  48.  
  49.     Screen("    Attempt to open the %s table: \r\n"
  50.            "        Error Expected: 'Encrypted dBASE tables not supported'...",
  51.            szTblName);
  52.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  53.                         NULL, NULL, 0, dbiREADONLY, dbiOPENSHARED,
  54.                         xltFIELD, FALSE, NULL, &hCur);
  55.     ChkRslt(rslt, "OpenTable");
  56.  
  57.     if (hCur)
  58.     {
  59.         Screen("    Display the %s table...", szTblName);
  60.         DisplayTable(hCur, uNumOfRecs);
  61.  
  62.         Screen("\r\n    Close the %s table...", szTblName);
  63.         DbiCloseCursor(&hCur);
  64.     }
  65.  
  66.     Screen("\r\n    Register the Login Callback...");
  67.  
  68.     // Allocate enough space for the data passed to the callback function.
  69.     CallBackData3 = (pCHAR)malloc(100 * sizeof(CHAR));
  70.     if (CallBackData3 == NULL)
  71.     {
  72.         CloseDbAndExit(&hDb);
  73.         Screen("\r\n*** End of Example ***");
  74.         return;
  75.     }
  76.  
  77.     // Register the callback.
  78.     rslt = DbiRegisterCallBack(NULL, cbDBASELOGIN, (UINT32) CallBackData3,
  79.                                sizeof(CBLoginDesc), &CbInfo, CallBackFunc3);
  80.     if(ChkRslt(rslt, "RegisterCallback") != DBIERR_NONE)
  81.     {
  82.         free(CallBackData3);
  83.         CloseDbAndExit(&hDb);
  84.         Screen("\r\n*** End of Example ***");
  85.         return;
  86.     }
  87.  
  88.     Screen("\r\n    Open the %s table...", szTblName);
  89.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  90.                         NULL, NULL, 0, dbiREADONLY, dbiOPENSHARED,
  91.                         xltFIELD, FALSE, NULL, &hCur);
  92.     ChkRslt(rslt, "OpenTable");
  93.  
  94.     if (hCur)
  95.     {
  96.         Screen("    Display the Table...");
  97.         DisplayTable(hCur, uNumOfRecs);
  98.  
  99.         Screen("\r\n    Close the %s table...", szTblName);
  100.         rslt = DbiCloseCursor(&hCur);
  101.         ChkRslt(rslt, "CloseCursor");
  102.     }
  103.  
  104.     // Unregister the callback by passing in NULL for the function.
  105.     rslt = DbiRegisterCallBack(NULL, cbINPUTREQ, NULL, 0, NULL, NULL);
  106.     if (ChkRslt(rslt, "RegisterCallBack") != DBIERR_NONE)
  107.     {
  108.         free(CallBackData3);
  109.         CloseDbAndExit(&hDb);
  110.         Screen("\r\n*** End of Example ***");
  111.         return;
  112.     }
  113.  
  114.     free(CallBackData3);
  115.  
  116.     Screen("\r\n    Close the database and exit IDAPI...");
  117.     CloseDbAndExit(&hDb);
  118.  
  119.     Screen("\r\n*** End of Example ***");
  120. }
  121.  
  122. //======================================================================
  123. //  Name:   CallBackFunc3(ecbType, iClientData, pCbInfo)
  124. //
  125. //  Input:  ecbType     - Callback type
  126. //          iClientData - Pointer to client information that is passed into
  127. //                        the callback function
  128. //          pCbInfo     - The callback structure that holds the information
  129. //                        about the current state
  130. //
  131. //  Return: The action that should be taken
  132. //
  133. //  Description:
  134. //          This function will be called from the BDE during the process
  135. //          of opening the table.
  136. //======================================================================
  137. CBRType DBIFN
  138. CallBackFunc3 (CBType ecbType, UINT32 iClientData, pVOID pCbInfo)
  139. {
  140.     CBLoginDesc *eCBInputDesc; // Variable to contain passed-in
  141.                                //   information
  142.  
  143.     // Set to stop an unused variable warning.
  144.     iClientData = iClientData;
  145.  
  146.     switch (ecbType)
  147.     {
  148.         // In case this is a restructure progress callback, display the
  149.         //   information.
  150.         case cbDBASELOGIN:
  151.  
  152.             eCBInputDesc = (CBLoginDesc far *)pCbInfo;
  153.  
  154.             strcpy(eCBInputDesc->szUserName, "TEST");
  155.             strcpy(eCBInputDesc->szGroupName, "TEST");
  156.             strcpy(eCBInputDesc->szUserPassword, "TEST");
  157.             break;
  158.  
  159.         default:
  160.             Screen("### In the callback function");
  161.     }
  162.  
  163.     return cbrCHKINPUT;
  164. }
  165.