home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / Chip_2001-06_cd1.bin / zkuste / vbasic / Data / Utility / MSISDK15.msi / CustAct1.cpp < prev    next >
C/C++ Source or Header  |  2000-10-05  |  12KB  |  224 lines

  1. #pragma message("Simple Custom Action DLL.  Copyright (c) 1997 - 2000 Microsoft Corp.")
  2. #if 0  // makefile definitions
  3. DESCRIPTION = Custom Action Test DLL
  4. MODULENAME = CustAct1
  5. FILEVERSION = 0.20
  6. ENTRY = Action1,Action129,Action193,Action257,Action513,Action769,Action1025,Action1281,Action1537,KitchenSink,GPFault,DllRegisterServer,DllUnregisterServer
  7. !include "..\TOOLS\MsiTool.mak"
  8. !if 0  #nmake skips the rest of this file
  9. #endif // end of makefile definitions
  10.  
  11. //+-------------------------------------------------------------------------
  12. //
  13. //  Microsoft Windows
  14. //
  15. //  Copyright (C) Microsoft Corporation, 1997 - 2000
  16. //
  17. //  File:       custact1.cpp
  18. //
  19. //--------------------------------------------------------------------------
  20.  
  21. //-----------------------------------------------------------------------------------------
  22. //
  23. // BUILD Instructions
  24. //
  25. // notes:
  26. //    - SDK represents the full path to the install location of the
  27. //     Windows Installer SDK
  28. //
  29. // Using NMake:
  30. //        %vcbin%\nmake -f custact1.cpp include="%include;SDK\Include" lib="%lib%;SDK\Lib"
  31. //
  32. // Using MsDev:
  33. //        1. Create a new Win32 DLL project
  34. //      2. Add custact1.cpp to the project
  35. //      3. Add SDK\Include and SDK\Lib directories on the Tools\Options Directories tab
  36. //      4. Add msi.lib to the library list in the Project Settings dialog
  37. //          (in addition to the standard libs included by MsDev)
  38. //
  39. //------------------------------------------------------------------------------------------
  40.  
  41. // test of external database access
  42. #define WINDOWS_LEAN_AND_MEAN  // faster compile
  43. #include <windows.h>  // included for both CPP and RC passes
  44. #ifndef RC_INVOKED    // start of CPP source code
  45. #include <stdio.h>    // printf/wprintf
  46. #include <tchar.h>    // define UNICODE=1 on nmake command line to build UNICODE
  47. #include "MsiQuery.h" // must be in this directory or on INCLUDE path
  48.  
  49. HRESULT __stdcall DllRegisterServer()
  50. {
  51.     Beep(1000, 500);
  52.     return 0;
  53. }
  54.  
  55. HRESULT __stdcall DllUnregisterServer()
  56. {
  57.     Beep(500, 500);
  58.     return 0;
  59. }
  60.  
  61. static UINT Action(MSIHANDLE hInstall, int iType, const TCHAR* szPass)
  62. {
  63.     TCHAR szProperty[] = TEXT("TESTACTION");
  64.     TCHAR szValue[200];
  65.     DWORD cchValue = sizeof(szValue)/sizeof(TCHAR);
  66.     if (MsiGetProperty(hInstall, szProperty, szValue, &cchValue) != ERROR_SUCCESS || szValue[0] == 0)
  67.         lstrcpy(szValue, TEXT("(none)"));
  68.     if (iType & 128)  // asyncronous action, cannot call MsiProcessMessage without blocking install
  69.     {
  70.         TCHAR szMessage[256];
  71.         wsprintf(szMessage, TEXT("Action %i, called from %s, TESTACTION = %s\rOK to succeed, CANCEL to fail"),
  72.                  iType, szPass, szValue);
  73.         return ::MessageBox(0, szMessage, TEXT("Installer custom action test"), MB_OKCANCEL);
  74.     }
  75.     PMSIHANDLE hrec = ::MsiCreateRecord(34);
  76.     ::MsiRecordSetString(hrec, 0, TEXT("Called from [2], TESTACTION = [3]\rOK to succeed, CANCEL to fail"));
  77.     ::MsiRecordSetInteger(hrec, 1, iType);  // not really an error code
  78.     ::MsiRecordSetString(hrec, 2, szPass);
  79.     ::MsiRecordSetString(hrec, 3, szValue);
  80.     return ::MsiProcessMessage(hInstall, INSTALLMESSAGE(INSTALLMESSAGE_USER + MB_OKCANCEL), hrec)
  81.         == IDOK ? ERROR_SUCCESS : ERROR_INSTALL_USEREXIT;
  82. }
  83.  
  84. UINT __stdcall Action1   (MSIHANDLE hInstall) { return Action(hInstall,   1, TEXT("Always run")); }
  85. UINT __stdcall Action129 (MSIHANDLE hInstall) { return Action(hInstall, 129, TEXT("Always, asynch in sequence")); }
  86. UINT __stdcall Action193 (MSIHANDLE hInstall) { return Action(hInstall, 193, TEXT("Always, asynch for session")); }
  87. UINT __stdcall Action257 (MSIHANDLE hInstall) { return Action(hInstall, 257, TEXT("Once, client preferred")); }
  88. UINT __stdcall Action513 (MSIHANDLE hInstall) { return Action(hInstall, 513, TEXT("Once per process")); }
  89. UINT __stdcall Action769 (MSIHANDLE hInstall) { return Action(hInstall, 769, TEXT("Client 2nd sequence")); }
  90. UINT __stdcall Action1025(MSIHANDLE hInstall) { return Action(hInstall,1025, TEXT("Execution script")); }
  91. UINT __stdcall Action1281(MSIHANDLE hInstall) { return Action(hInstall,1281, TEXT("Rollback script")); }
  92. UINT __stdcall Action1537(MSIHANDLE hInstall) { return Action(hInstall,1537, TEXT("Commit script")); }
  93.  
  94. UINT __stdcall GPFault(MSIHANDLE hInstall)
  95. {
  96.     if (::MessageBox(0, TEXT("OK to GPFault,  CANCEL to skip"), TEXT("GPFault Text"), MB_OKCANCEL)
  97.             == IDCANCEL)
  98.         return ERROR_SUCCESS;
  99.     TCHAR* sz = (TCHAR*)0;
  100.     sz[0] = 0;
  101.     return ERROR_INSTALL_FAILURE;
  102. }
  103.  
  104. void CheckError(UINT ui)
  105. {
  106.     if (ERROR_SUCCESS != ui)
  107.         MessageBox(0, TEXT("FAILURE"), 0, 0);
  108. }
  109.  
  110. UINT __stdcall KitchenSink(MSIHANDLE hInstall) 
  111.     char  rgchAnsi[100];
  112.     WCHAR rgchWide[100];
  113.     DWORD cchBufAnsi = sizeof(rgchAnsi)/sizeof(char);
  114.     DWORD cchBufWide = sizeof(rgchWide)/sizeof(WCHAR);
  115.     MSIHANDLE h;
  116.     
  117.     CheckError(MsiGetPropertyA(hInstall, "ProductName", rgchAnsi, &cchBufAnsi));
  118.     if (0 != lstrcmpA(rgchAnsi, "TestDb") || cchBufAnsi != sizeof("TestDb")/sizeof(char)-1)
  119.         CheckError(E_FAIL);
  120.  
  121.     CheckError(MsiGetPropertyW(hInstall, L"ProductName", rgchWide, &cchBufWide));
  122.     if (0 != lstrcmpW(rgchWide, L"TestDb") || cchBufWide != sizeof(L"TestDb")/sizeof(WCHAR)-1)
  123.         CheckError(E_FAIL);
  124.  
  125.     h = MsiCreateRecord(4);
  126.     CheckError(h == 0 ? E_FAIL : S_OK);
  127.     CheckError(MsiCloseHandle(h));
  128.  
  129.     CheckError(MsiCloseAllHandles());
  130.  
  131.     PMSIHANDLE hDatabase;
  132.     hDatabase = MsiGetActiveDatabase(hInstall);
  133.     CheckError(hDatabase == 0 ? E_FAIL : S_OK);
  134.  
  135.     PMSIHANDLE hView;
  136.     CheckError(MsiDatabaseOpenViewA(hDatabase, "SELECT `Value` FROM `Property` WHERE `Property`=?", &hView));
  137.     PMSIHANDLE hRecord = MsiCreateRecord(1);
  138.     CheckError(MsiRecordSetStringA(hRecord, 1, "ProductName"));
  139.     CheckError(MsiViewExecute(hView, hRecord));
  140.     PMSIHANDLE hResults;
  141.     CheckError(MsiViewFetch(hView, &hResults));
  142.     
  143.     cchBufAnsi = sizeof(rgchAnsi)/sizeof(char);
  144.     CheckError(MsiRecordGetStringA(hResults, 1, rgchAnsi, &cchBufAnsi));
  145.     if (0 != lstrcmpA(rgchAnsi, "TestDb") || cchBufAnsi != sizeof("TestDb")/sizeof(char) - 1)
  146.         CheckError(E_FAIL);
  147.  
  148.     CheckError(MsiSetMode(hInstall, MSIRUNMODE_REBOOTATEND, TRUE));
  149.     if (TRUE != MsiGetMode(hInstall, MSIRUNMODE_REBOOTATEND))
  150.         CheckError(E_FAIL);
  151.     CheckError(MsiSetMode(hInstall, MSIRUNMODE_REBOOTATEND, FALSE));
  152.     if (FALSE != MsiGetMode(hInstall, MSIRUNMODE_REBOOTATEND))
  153.         CheckError(E_FAIL);
  154.  
  155.     INSTALLSTATE installed, action;
  156.     CheckError(MsiSetFeatureStateA(hInstall, "QuickTest", INSTALLSTATE_SOURCE));
  157.     CheckError(MsiGetFeatureStateA(hInstall, "QuickTest", &installed, &action));
  158.     if (installed != INSTALLSTATE_ABSENT || action != INSTALLSTATE_SOURCE)
  159.         CheckError(E_FAIL);
  160.     
  161.  
  162.  
  163.  
  164.     return ERROR_SUCCESS;
  165.  
  166. /*
  167.     HRESULT         __stdcall ViewGetError(unsigned long hView, ichar* szColumnNameBuffer,unsigned long* pcchBuf, int *pMsidbError);
  168.     
  169.     HRESULT         __stdcall ViewModify(unsigned long hView, long eUpdateMode, unsigned long hRecord);
  170.     HRESULT         __stdcall ViewClose(unsigned long hView);
  171.     HRESULT         __stdcall OpenDatabase(const ichar* szDatabasePath, const ichar* szPersist,unsigned long *phDatabase);
  172.     HRESULT         __stdcall DatabaseCommit(unsigned long hDatabase);
  173.     HRESULT         __stdcall DatabaseGetPrimaryKeys(unsigned long hDatabase, const ichar * szTableName,unsigned long *phRecord);
  174.     HRESULT         __stdcall RecordIsNull(unsigned long hRecord, unsigned int iField, boolean *pfIsNull);
  175.     HRESULT         __stdcall RecordDataSize(unsigned long hRecord, unsigned int iField,unsigned int* puiSize);
  176.     HRESULT         __stdcall RecordSetInteger(unsigned long hRecord, unsigned int iField, int iValue);
  177.     HRESULT         __stdcall RecordSetString(unsigned long hRecord,    unsigned int iField, const ichar* szValue);
  178.     HRESULT         __stdcall RecordGetInteger(unsigned long hRecord, unsigned int iField, int *piValue);
  179.     HRESULT         __stdcall RecordGetString(unsigned long hRecord, unsigned int iField, ichar* szValueBuf,unsigned long *pcchValueBuf);
  180.     HRESULT         __stdcall RecordGetFieldCount(unsigned long hRecord,unsigned int* piCount);
  181.     HRESULT         __stdcall RecordSetStream(unsigned long hRecord, unsigned int iField, const ichar* szFilePath);
  182.     HRESULT         __stdcall RecordReadStream(unsigned long hRecord, unsigned int iField, char *szDataBuf,unsigned long *pcbDataBuf);
  183.     HRESULT         __stdcall RecordClearData(unsigned long hRecord);
  184.     HRESULT         __stdcall GetSummaryInformation(unsigned long hDatabase, const ichar*  szDatabasePath, unsigned int    uiUpdateCount, unsigned long *phSummaryInfo);
  185.     HRESULT         __stdcall SummaryInfoGetPropertyCount(unsigned long hSummaryInfo,    unsigned int *puiPropertyCount);
  186.     HRESULT         __stdcall SummaryInfoSetProperty(unsigned long hSummaryInfo,unsigned intuiProperty, unsigned intuiDataType, int iValue, FILETIME *pftValue, const ichar* szValue); 
  187.     HRESULT         __stdcall SummaryInfoGetProperty(unsigned long hSummaryInfo,unsigned intuiProperty,unsigned int *puiDataType, int *piValue, FILETIME *pftValue, ichar*  szValueBuf,unsigned long *pcchValueBuf);
  188.     HRESULT         __stdcall SummaryInfoPersist(unsigned long hSummaryInfo);
  189.     
  190.     HRESULT         __stdcall SetProperty(unsigned long hInstall, const ichar* szName, const ichar* szValue);
  191.     HRESULT         __stdcall GetLanguage(unsigned long hInstall,unsigned short* pLangId);
  192.     HRESULT         __stdcall GetMode(unsigned long hInstall, long eRunMode, boolean* pfSet); 
  193.     HRESULT         __stdcall SetMode(unsigned long hInstall, long eRunMode, boolean fState);
  194.     HRESULT         __stdcall FormatRecord(unsigned long hInstall, unsigned long hRecord, ichar* szResultBuf,unsigned long *pcchResultBuf);
  195.     HRESULT         __stdcall DoAction(unsigned long hInstall, const ichar* szAction);    
  196.     HRESULT         __stdcall Sequence(unsigned long hInstall, const ichar* szTable, int iSequenceMode);   
  197.     HRESULT         __stdcall ProcessMessage(unsigned long hInstall, long eMessageType, unsigned long hRecord, int* piRes);
  198.     HRESULT         __stdcall EvaluateCondition(unsigned long hInstall, const ichar* szCondition, int *piCondition);
  199.     HRESULT         __stdcall GetComponentState(unsigned long hInstall, const ichar* szComponent, long *piInstalled, long *piAction);
  200.     HRESULT         __stdcall SetComponentState(unsigned long hInstall, const ichar*     szComponent, long iState);
  201.     HRESULT         __stdcall GetFeatureCost(unsigned long hInstall, const ichar* szFeature, int iCostTree, long iState, int *piCost);
  202.     HRESULT         __stdcall SetInstallLevel(unsigned long hInstall, int iInstallLevel);
  203.     HRESULT         __stdcall GetFeatureValidStates(unsigned long hInstall, const ichar* szFeature,unsigned long *dwInstallStates);
  204.     HRESULT         __stdcall DatabaseIsTablePersistent(unsigned long hDatabase, const ichar* szTableName, int *piCondition);
  205.     HRESULT         __stdcall ViewGetColumnInfo(unsigned long hView, long eColumnInfo,unsigned long *phRecord);
  206.     HRESULT         __stdcall GetLastErrorRecord(unsigned long *phRecord);
  207.     HRESULT         __stdcall GetSourcePath(unsigned long hInstall, const ichar* szFolder, ichar* szPathBuf, unsigned long *pcchPathBuf);
  208.     HRESULT         __stdcall GetTargetPath(unsigned long hInstall, const ichar* szFolder, ichar* szPathBuf, unsigned long *pcchPathBuf); 
  209.     HRESULT         __stdcall SetTargetPath(unsigned long hInstall, const ichar* szFolder, const ichar* szFolderPath);
  210.     HRESULT         __stdcall VerifyDiskSpace(unsigned long hInstall);
  211.  
  212. */
  213.  
  214. }
  215.  
  216. #else // RC_INVOKED, end of CPP source code, start of resource definitions
  217. // resource definition go here
  218. #endif // RC_INVOKED
  219. #if 0  // required at end of source file, to hide makefile terminator
  220. !endif // makefile terminator
  221. #endif
  222.  
  223.