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

  1. #if 0  // makefile definitions
  2. DESCRIPTION = RemoveUserAccount from Local Machine
  3. MODULENAME = remove
  4. FILEVERSION = Msi
  5. ENTRY = RemoveUserAccount
  6. UNICODE=1
  7. LINKLIBS = netapi32.lib
  8. !include "..\TOOLS\MsiTool.mak"
  9. !if 0  #nmake skips the rest of this file
  10. #endif // end of makefile definitions
  11.  
  12. // Required headers
  13. #define WINDOWS_LEAN_AND_MEAN  // faster compile
  14. #include <windows.h>
  15. #ifndef RC_INVOKED    // start of source code
  16.  
  17. #include "msiquery.h"
  18. #include "msidefs.h"
  19. #include <windows.h>
  20. #include <basetsd.h>
  21. #include <lm.h>
  22.  
  23. #define UNICODE 1
  24.  
  25. //+-------------------------------------------------------------------------
  26. //
  27. //  Microsoft Windows
  28. //
  29. //  Copyright (C) Microsoft Corporation, 2000
  30. //
  31. //  File: remove.cpp
  32. //
  33. //  Notes: DLL custom action, must be used in conjunction with the DLL
  34. //         custom actions included in process.cpp and create.cpp
  35. //--------------------------------------------------------------------------
  36.  
  37. //-----------------------------------------------------------------------------------------
  38. //
  39. // BUILD Instructions
  40. //
  41. // notes:
  42. //    - SDK represents the full path to the install location of the
  43. //     Windows Installer SDK
  44. //
  45. // Using NMake:
  46. //        %vcbin%\nmake -f remove.cpp include="%include;SDK\Include" lib="%lib%;SDK\Lib"
  47. //
  48. // Using MsDev:
  49. //        1. Create a new Win32 DLL project
  50. //      2. Add remove.cpp to the project
  51. //      3. Add SDK\Include and SDK\Lib directories on the Tools\Options Directories tab
  52. //      4. Add msi.lib and netapi32.lib to the library list in the Project Settings dialog
  53. //          (in addition to the standard libs included by MsDev)
  54. //      5. Add /DUNICODE to the project options in the Project Settings dialog
  55. //
  56. //------------------------------------------------------------------------------------------
  57.  
  58. //////////////////////////////////////////////////////////////////////////////
  59. // RemoveUserAccount
  60. //
  61. //     Attempts to remove a user account on the local machine according
  62. //       to the "instructions" provided in the CustomActionData property
  63. //
  64. //     As a deferred custom action, you do not have access to the database.
  65. //       The only source of infromation comes from a property that another
  66. //       custom action can set to provide the information you need.  This
  67. //       property is written into the script
  68. //
  69. UINT __stdcall RemoveUserAccount(MSIHANDLE hInstall)
  70. {
  71.     // determine mode in which we are called
  72.     BOOL bRollback = MsiGetMode(hInstall, MSIRUNMODE_ROLLBACK); // true for rollback, else regular deferred version (for uninstall)
  73.  
  74.     const int iRemoveError = 25003;
  75.     const int iRemoveWarning = 25004;
  76.  
  77.     // Grab the CustomActionData property
  78.     WCHAR* wszCAData = 0;
  79.     DWORD cchCAData = 0;
  80.     ::MsiGetPropertyW(hInstall, IPROPNAME_CUSTOMACTIONDATA, L"", &cchCAData);
  81.     wszCAData = new WCHAR[++cchCAData];
  82.     UINT uiStat = ::MsiGetPropertyW(hInstall, IPROPNAME_CUSTOMACTIONDATA, wszCAData, &cchCAData);
  83.     if (ERROR_SUCCESS != uiStat)
  84.     {
  85.         if (wszCAData)
  86.             delete [] wszCAData;
  87.         return ERROR_INSTALL_FAILURE; // error -- should never happen
  88.     }
  89.  
  90.     // send ActionData message (template in ActionText table)
  91.     PMSIHANDLE hRec = ::MsiCreateRecord(1);
  92.     ::MsiRecordSetStringW(hRec, 1, wszCAData);
  93.     ::MsiProcessMessage(hInstall, INSTALLMESSAGE_ACTIONDATA, hRec);
  94.  
  95.     //
  96.     // Call the NetUserDel function, 
  97.     //
  98.     NET_API_STATUS nStatus = NetUserDel(NULL /*local machine*/, wszCAData /*user name*/);
  99.     
  100.     if (NERR_Success != nStatus)
  101.     {
  102.         PMSIHANDLE hRecErr = ::MsiCreateRecord(3);
  103.         ::MsiRecordSetStringW(hRecErr, 2, wszCAData);
  104.         if (wszCAData)
  105.             delete [] wszCAData;
  106.         // NERR_UserNotFound is only an error if we are not in Rollback mode
  107.         // In rollback mode, NERR_UserNotFound means cancel button depressed in middle of deferred CA trying to create this account
  108.         if (!bRollback && NERR_UserNotFound == nStatus)
  109.         {
  110.             ::MsiRecordSetInteger(hRecErr, 1, iRemoveWarning);
  111.             // just pop up an OK button
  112.             // OPTIONALLY, could specify multiple buttons and cancel
  113.             // install based on user selection by handling the return value
  114.             // from MsiProcessMessage
  115.             ::MsiProcessMessage(hInstall, INSTALLMESSAGE(INSTALLMESSAGE_WARNING|MB_ICONWARNING|MB_OK), hRecErr);
  116.             return ERROR_SUCCESS; // ignorable error
  117.         }
  118.         else
  119.         {
  120.             ::MsiRecordSetInteger(hRecErr, 1, iRemoveError);
  121.             ::MsiRecordSetInteger(hRecErr, 3, nStatus);
  122.             ::MsiProcessMessage(hInstall, INSTALLMESSAGE_ERROR, hRecErr);
  123.             return ERROR_INSTALL_FAILURE; // error
  124.         }
  125.     }
  126.  
  127.     if (wszCAData)
  128.         delete [] wszCAData;
  129.     return ERROR_SUCCESS;
  130. }
  131.  
  132.  
  133. #else // RC_INVOKED, end of source code, start of resources
  134. // resource definition go here
  135.  
  136. #endif // RC_INVOKED
  137. #if 0 
  138. !endif // makefile terminator
  139. #endif
  140.