home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sysmgmt / msi / apitstdb / apitstdb.cpp next >
C/C++ Source or Header  |  1997-10-13  |  3KB  |  102 lines

  1. #pragma message("Test of Msi External Database Access - Resequences InstallSequence table.  Copyright (c) 1997 Microsoft Corp.")
  2. #if 0  // makefile definitions, to build: %vcbin%\nmake -fApiTstDb.cpp
  3. DESCRIPTION = Test of Msi external database access
  4. MODULENAME = ApiTstDb
  5. SUBSYSTEM = console
  6. FILEVERSION = 0.20
  7. !include <MsiTool.Mak>
  8. !if 0  #nmake skips the rest of this file
  9. #endif // end of makefile definitions
  10.  
  11. // test of external database access
  12. #define WINDOWS_LEAN_AND_MEAN  // faster compile
  13. #include <windows.h>
  14. #ifndef RC_INVOKED    // start of source code
  15. #include <stdio.h>   // printf/wprintf
  16. #include <tchar.h>   // define UNICODE=1 on nmake command line to build UNICODE
  17. #include "MsiQuery.h"
  18.  
  19. // routine to check return status for ERROR_SUCCESS, and throw exception if not
  20. TCHAR*  g_szErrorContext = 0;
  21. void CheckMsi(UINT iStat, TCHAR* szContext)
  22. {
  23.     if (iStat != ERROR_SUCCESS)
  24.     {
  25.         g_szErrorContext = szContext;
  26.         throw iStat;
  27.     }
  28. }
  29.  
  30. void SmoothSequence(MSIHANDLE hDatabase, int iSpacing)
  31. {
  32.     PMSIHANDLE hView;
  33.     CheckMsi(MsiDatabaseOpenView(hDatabase, TEXT("SELECT Action, Sequence FROM InstallSequence ORDER BY Sequence"),&hView),TEXT("OpenView"));
  34.     CheckMsi(MsiViewExecute(hView, 0), TEXT("Execute View"));
  35.     int iLast = 0;
  36.     for (;;)
  37.     {
  38.         PMSIHANDLE hRecord;
  39.         UINT iStat = MsiViewFetch(hView, &hRecord);
  40.         if (iStat == ERROR_NO_MORE_ITEMS)
  41.             break;
  42.         CheckMsi(iStat, TEXT("Fetch"));
  43.         TCHAR szAction[60];
  44.         int iSequence, iNew;
  45.         DWORD cchAction = sizeof(szAction)/sizeof(TCHAR);
  46.         MsiRecordGetString(hRecord, 1, szAction, &cchAction);
  47.         iSequence = MsiRecordGetInteger(hRecord, 2);
  48.         if (iSequence > 0)
  49.         {
  50.             iLast += iSpacing;
  51.             iNew = iLast;
  52.             CheckMsi(MsiRecordSetInteger(hRecord, 2, iNew), TEXT("SetInteger"));
  53.             CheckMsi(MsiViewModify(hView, MSIMODIFY_UPDATE, hRecord), TEXT("UpdateView"));
  54.         }
  55.         else
  56.             iNew = iSequence;
  57.         _tprintf(TEXT("%3i %3i  %s\n"), iSequence, iNew, szAction);
  58.     }
  59. }
  60.  
  61. extern "C" int __cdecl _tmain(int argc, TCHAR* argv[])
  62. {
  63.     if (argc < 2)
  64.     {
  65.         _tprintf(TEXT("Usage: ApiTstDb {database path} [{sequence spacing}]"));
  66.         return 1;
  67.     }
  68.     int iSpacing = 10;
  69.     if (argc >= 3)
  70.     {
  71.         int i = _ttoi(argv[2]);
  72.         if (i > 0)
  73.             iSpacing = i;
  74.     }
  75.     try
  76.     {
  77.         PMSIHANDLE hDatabase;
  78.         CheckMsi(MsiOpenDatabase(argv[1], MSIDBOPEN_TRANSACT, &hDatabase),TEXT("OpenDatabase"));
  79.         SmoothSequence(hDatabase, iSpacing);
  80.         MsiDatabaseCommit(hDatabase);
  81.         TCHAR rgchBuf[MAX_PATH];
  82.         GetTempPath(MAX_PATH, rgchBuf);
  83.         CheckMsi(MsiDatabaseExport(hDatabase, TEXT("InstallSequence"), rgchBuf, TEXT("InstallSequence.idt")),TEXT("ExportTable"));
  84.     }
  85.     catch (UINT iError)
  86.     {
  87.         _tprintf(TEXT("%s error %i"), g_szErrorContext, iError);
  88.         return 1;
  89.     }
  90.     int iOpenHandles = MsiCloseAllHandles();  // diagnostic check only
  91.     if (iOpenHandles != 0)
  92.         _tprintf(TEXT("%i Handle(s) not closed"), iOpenHandles);
  93.     return 0;
  94. }
  95.  
  96. #else // RC_INVOKED, end of source code, start of resources
  97. // resource definition go here
  98. #endif // RC_INVOKED
  99. #if 0 
  100. !endif // makefile terminator
  101. #endif
  102.