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

  1. #if 0  // makefile definitions
  2. DESCRIPTION = CreateUserAccount on Local Machine
  3. MODULENAME = create
  4. FILEVERSION = Msi
  5. ENTRY = CreateUserAccount
  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 <stdlib.h>
  22. #include <lm.h>
  23.  
  24. #define UNICODE 1
  25.  
  26. //+-------------------------------------------------------------------------
  27. //
  28. //  Microsoft Windows
  29. //
  30. //  Copyright (C) Microsoft Corporation, 2000
  31. //
  32. //  File:       create.cpp
  33. //
  34. //  Notes: DLL custom action, must be used in conjunction with the DLL
  35. //         custom actions included in process.cpp and remove.cpp
  36. //--------------------------------------------------------------------------
  37.  
  38. //-----------------------------------------------------------------------------------------
  39. //
  40. // BUILD Instructions
  41. //
  42. // notes:
  43. //    - SDK represents the full path to the install location of the
  44. //     Windows Installer SDK
  45. //
  46. // Using NMake:
  47. //        %vcbin%\nmake -f create.cpp include="%include;SDK\Include" lib="%lib%;SDK\Lib"
  48. //
  49. // Using MsDev:
  50. //        1. Create a new Win32 DLL project
  51. //      2. Add create.cpp to the project
  52. //      3. Add SDK\Include and SDK\Lib directories on the Tools\Options Directories tab
  53. //      4. Add msi.lib and netapi32.lib to the library list in the Project Settings dialog
  54. //          (in addition to the standard libs included by MsDev)
  55. //      5. Add /DUNICODE to the project options in the Project Settings dialog
  56. //
  57. //------------------------------------------------------------------------------------------
  58.  
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CreateUserAccount
  62. //
  63. //     Attempts to create a user account on the local machine according
  64. //       to the "instructions" provided in the CustomActionData property
  65. //
  66. //     As a deferred custom action, you do not have access to the database.
  67. //       The only source of infromation comes from a property that another
  68. //       custom action can set to provide the information you need.  This
  69. //       property is written into the script
  70. //
  71. UINT __stdcall CreateUserAccount(MSIHANDLE hInstall)
  72. {
  73.     const WCHAR* wszSep = L"\001";
  74.     const int iCreationError = 25001;
  75.     const int iCreationDuplicate = 25002;
  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.     USER_INFO_1 ui;
  91.     DWORD dwLevel = 1; // represents USER_INFO_1 struct
  92.     DWORD dwError = 0;
  93.     NET_API_STATUS nStatus;
  94.  
  95.     //
  96.     // Parse CustomActionDataProperty
  97.     //
  98.     WCHAR* wszUserName = wcstok(wszCAData, wszSep);
  99.     WCHAR* wszPassWd   = wcstok(NULL, wszSep);
  100.     WCHAR* pwch = wcstok(NULL, wszSep);
  101.     if (!pwch)
  102.     {
  103.         if (wszCAData)
  104.             delete [] wszCAData;
  105.         return ERROR_INSTALL_FAILURE; // error -- should never happen
  106.     }
  107.     int iUserFlags = wcstol(pwch, 0, 10);
  108.     
  109.     //
  110.     // Set up the USER_INFO_1 structure.
  111.     //  USER_PRIV_USER: name identifies a user, 
  112.     //    rather than an administrator or a guest.
  113.     //  UF_SCRIPT: required for LAN Manager 2.0 and
  114.     //    Windows NT/Windows 2000.
  115.     //
  116.     ui.usri1_name = wszUserName;
  117.     ui.usri1_password = wszPassWd;
  118.     ui.usri1_priv = USER_PRIV_USER;
  119.     ui.usri1_flags = UF_SCRIPT | iUserFlags;
  120.     ui.usri1_home_dir = NULL;
  121.     ui.usri1_comment = NULL;
  122.     ui.usri1_script_path = NULL;
  123.  
  124.     // Send ActionData message (template in ActionText table)
  125.     PMSIHANDLE hRec = ::MsiCreateRecord(1);
  126.     ::MsiRecordSetStringW(hRec, 1, wszUserName);
  127.     ::MsiProcessMessage(hInstall, INSTALLMESSAGE_ACTIONDATA, hRec);
  128.  
  129.     //
  130.     // Call the NetUserAdd function, specifying level 1.
  131.     //
  132.     nStatus = NetUserAdd(NULL /*local machine*/, dwLevel, (LPBYTE)&ui, &dwError);  
  133.  
  134.     if (nStatus != NERR_Success)
  135.     {
  136.         PMSIHANDLE hRecErr = ::MsiCreateRecord(3);
  137.         ::MsiRecordSetInteger(hRecErr, 1, (nStatus == NERR_UserExists) ? iCreationDuplicate : iCreationError);
  138.         ::MsiRecordSetStringW(hRecErr, 2, wszUserName);
  139.         ::MsiRecordSetInteger(hRecErr, 3, nStatus);
  140.         ::MsiProcessMessage(hInstall, INSTALLMESSAGE_ERROR, hRecErr);
  141.         if (wszCAData)
  142.             delete [] wszCAData;
  143.  
  144.         return ERROR_INSTALL_FAILURE; // error
  145.     }
  146.     if (wszCAData)
  147.         delete [] wszCAData;
  148.     return ERROR_SUCCESS;
  149. }
  150.  
  151. #else // RC_INVOKED, end of source code, start of resources
  152. // resource definition go here
  153.  
  154. #endif // RC_INVOKED
  155. #if 0 
  156. !endif // makefile terminator
  157. #endif
  158.