scripter.c
上传用户:xhy777
上传日期:2007-02-14
资源大小:24088k
文件大小:15k
源码类别:

系统编程

开发平台:

Visual C++

  1. /******************************************************************************
  2. **
  3. ** Module:          RNAUI.DLL
  4. ** File:            scripter.c
  5. ** Descriptions:    Remote Network Access Scripting dialog
  6. ** Contains:        Scripting tools dialog boxes
  7. **
  8. ** Copyright (c) 1992-1993, Microsoft Corporation, all rights reserved
  9. **
  10. ** History:         Thu 19-Aug-1993 -by-  Viroon  Touranachun [viroont]
  11. **                   Created
  12. **
  13. **                  Tue 08-Nov-1994 -by-  Viroon  Touranachun [viroont]
  14. **                   Moved from sessmgr.c
  15. **
  16. ******************************************************************************/
  17. #include "rnaui.h"
  18. #include "scripter.h"
  19. #include "rnahelp.h"
  20. //**************************************************************************
  21. //  Macros
  22. //**************************************************************************
  23. #define ISMINIMIZED(cmd) (cmd==SW_SHOWMINNOACTIVE || cmd==SW_SHOWMINIMIZED)
  24. //**************************************************************************
  25. //  Global Parameters
  26. //**************************************************************************
  27. #pragma data_seg(DATASEG_READONLY)
  28. char const g_szProfile[] = REGSTR_KEY_PROF;
  29. char const g_szScript[]  = REGSTR_VAL_SCRIPT;
  30. char const g_szMode[]    = REGSTR_VAL_MODE;
  31. char const g_szPlacement[]=REGSTR_VAL_TERM;
  32. char const cszHelpFile[]    = "winhelp.hlp>proc4";
  33. #pragma data_seg()
  34. //****************************************************************************
  35. // BOOL CALLBACK _export ScriptAppletDlgProc (HWND, UINT, WPARAM, LPARAM)
  36. //
  37. // This function handles the modal connection info setting dialog box.
  38. //
  39. // History:
  40. //  Fri 05-May-1995 08:58:47  -by-  Viroon  Touranachun [viroont]
  41. // Created.
  42. //****************************************************************************
  43. BOOL CALLBACK ScriptAppletDlgProc (HWND    hwnd,
  44.                                    UINT    message,
  45.                                    WPARAM  wParam,
  46.                                    LPARAM  lParam)
  47. {
  48.   PCONNENTDLG pConnEntDlg;
  49.   switch (message)
  50.   {
  51.     case WM_INITDIALOG:
  52.       pConnEntDlg = (PCONNENTDLG)(((LPPROPSHEETPAGE)lParam)->lParam);
  53.       SetWindowLong(hwnd, DWL_USER, (LONG)pConnEntDlg);
  54.       // Initilialize the script page
  55.       //
  56.       InitScriptDlg(hwnd);
  57.       break;
  58.     case WM_DESTROY:
  59.       DeInitScriptDlg (hwnd);
  60.       break;
  61.     case WM_HELP:
  62.     case WM_CONTEXTMENU:
  63.       ContextHelp(gaScripter, message, wParam, lParam);
  64.       break;
  65.     case WM_NOTIFY:
  66.       switch(((NMHDR FAR *)lParam)->code)
  67.       {
  68.         case PSN_KILLACTIVE:
  69.           //
  70.           // Update the selected SMM settings
  71.           //
  72.           SetWindowLong(hwnd, DWL_MSGRESULT,
  73.                         (LONG)(CheckScriptDlgData(hwnd) != ERROR_SUCCESS));
  74.           return TRUE;
  75.         case PSN_APPLY:
  76.           //
  77.           // The property sheet information is permanently applied
  78.           //
  79.           SaveScriptDlgData(hwnd);
  80.           return FALSE;
  81.         default:
  82.           break;
  83.       };
  84.       break;
  85.     case WM_COMMAND:
  86.       // Determine the end-user action
  87.       //
  88.       switch (GET_WM_COMMAND_ID(wParam, lParam))
  89.       {
  90.         case IDC_SCRIPT_NAME:
  91.           //
  92.           // Adjust the dialog appearance
  93.           //
  94.           if (GET_WM_COMMAND_CMD(wParam, lParam)==EN_CHANGE)
  95.           {
  96.             BOOL bEnable = (0 < Edit_GetTextLength(GET_WM_COMMAND_HWND(wParam, lParam)));
  97.             EnableWindow(GetDlgItem(hwnd, IDC_DEBUG), bEnable);
  98.             EnableWindow(GetDlgItem(hwnd, IDC_EDIT), bEnable);
  99.           };
  100.           break;
  101.         case IDC_EDIT:
  102.           //
  103.           // Edit script file
  104.           //
  105.           EditScriptFile(hwnd);
  106.           break;
  107.         case IDC_SCRIPT_BROWSE:
  108.           //
  109.           // Browse an existing file
  110.           //
  111.           BrowseScriptFile(hwnd);
  112.           break;
  113. #ifdef SCRPT_HELP_ENABLED
  114.         case IDC_SCRIPT_HELP:
  115.           WinHelp(hwnd, cszHelpFile, HELP_CONTEXT, CREATE_SCRIPT_MAIN);
  116.           break;
  117. #endif // SCRPT_HELP_ENABLED
  118.       };
  119.       break;
  120.     default:
  121.       break;
  122.   }
  123.   return FALSE;
  124. }
  125. //****************************************************************************
  126. // DWORD NEAR PASCAL InitScriptDlg (HWND hwnd)
  127. //
  128. // This function initializes the scripting page.
  129. //
  130. // History:
  131. //  Tue 08-Nov-1994 09:14:13  -by-  Viroon  Touranachun [viroont]
  132. // Created.
  133. //****************************************************************************
  134. DWORD NEAR PASCAL InitScriptDlg (HWND hwnd)
  135. {
  136.   PCONNENTDLG pConnEntDlg;
  137.   WINDOWPLACEMENT wp;
  138.   HKEY  hkey, hkeyEntry;
  139.   DWORD cbSize, dwType;
  140.   UINT  uMode;
  141.   char  szFileName[MAX_PATH];
  142.   // Get the currently selected connection name
  143.   //
  144.   pConnEntDlg = (PCONNENTDLG)GetWindowLong(hwnd, DWL_USER);
  145.   // Assume no assigned script
  146.   //
  147.   szFileName[0] = '';
  148.   uMode       = NORMAL_MODE;
  149.   wp.showCmd  = SW_SHOWMINNOACTIVE;
  150.   // Look up the registry for the current script name
  151.   //
  152.   if (RegOpenKey(HKEY_CURRENT_USER, g_szProfile, &hkey) == ERROR_SUCCESS)
  153.   {
  154.     if (RegOpenKey(hkey, pConnEntDlg->pConnEntry->pszEntry, &hkeyEntry)
  155.         == ERROR_SUCCESS)
  156.     {
  157.       cbSize = sizeof(szFileName);
  158.       if (RegQueryValueEx(hkeyEntry, g_szScript, 0, &dwType, szFileName,
  159.                           &cbSize) != ERROR_SUCCESS)
  160.       {
  161.         szFileName[0] = '';
  162.       };
  163.       cbSize = sizeof(uMode);
  164.       if (RegQueryValueEx(hkeyEntry, g_szMode, 0, &dwType, (LPBYTE)&uMode,
  165.                           &cbSize) != ERROR_SUCCESS)
  166.       {
  167.         uMode = NORMAL_MODE;
  168.       };
  169.       // Get the current window setting
  170.       //
  171.       cbSize = sizeof(wp);
  172.       if (RegQueryValueEx(hkeyEntry, g_szPlacement, 0, &dwType, (LPBYTE)&wp,
  173.                           &cbSize) != ERROR_SUCCESS)
  174.       {
  175.         // It is not there, signify the default values
  176.         //
  177.         wp.showCmd= SW_SHOWMINNOACTIVE;
  178.       };
  179.       RegCloseKey(hkeyEntry);
  180.     };
  181.     RegCloseKey(hkey);
  182.   };
  183.   // Set the name to the script name box
  184.   //
  185.   Edit_SetText(GetDlgItem(hwnd, IDC_SCRIPT_NAME), szFileName);
  186.   // Set the minimized options
  187.   //
  188.   CheckDlgButton(hwnd, IDC_MINIMIZED,
  189.                  ISMINIMIZED(wp.showCmd) ? BST_CHECKED : BST_UNCHECKED);
  190.   // Set the test mode
  191.   //
  192.   CheckDlgButton(hwnd, IDC_DEBUG,
  193.                  uMode == TEST_MODE ? BST_CHECKED : BST_UNCHECKED);
  194.   EnableWindow(GetDlgItem(hwnd, IDC_DEBUG),
  195.                (szFileName[0] == '') ? FALSE : TRUE);
  196.   // Enable/disable edit button
  197.   EnableWindow(GetDlgItem(hwnd, IDC_EDIT), 0 != szFileName[0]);
  198.   // Prepare to browse from the scripts directory
  199.   //
  200.   if (GetWindowsDirectory(szFileName, sizeof(szFileName)))
  201.   {
  202.     LPSTR lpsz = szFileName;
  203.     while((*lpsz != '\') && (*lpsz != ''))
  204.     {
  205.       lpsz = CharNext(lpsz);
  206.     };
  207.     if (*lpsz == '\')
  208.     {
  209.       // Try to use it as the current directory
  210.       //
  211.       if ((LoadString(ghInstance, IDS_INI_SCRIPT_DIR, lpsz,
  212.                       MAX_PATH - (lpsz - szFileName)) == 0) ||
  213.           (!SetCurrentDirectory(szFileName)))
  214.       {
  215.         // Try the short name
  216.         //
  217.         if (LoadString(ghInstance, IDS_INI_SCRIPT_SHORTDIR, lpsz,
  218.                        MAX_PATH - (lpsz - szFileName)))
  219.         {
  220.           SetCurrentDirectory(szFileName);
  221.         };
  222.       };
  223.     };
  224.   };
  225.   return ERROR_SUCCESS;
  226. }
  227. //****************************************************************************
  228. // DWORD NEAR PASCAL DeInitScriptDlg (HWND)
  229. //
  230. // This function initializes the advanced device options box.
  231. //
  232. // History:
  233. //  Mon 01-Mar-1993 13:51:30  -by-  Viroon  Touranachun [viroont]
  234. // Created.
  235. //****************************************************************************
  236. DWORD NEAR PASCAL DeInitScriptDlg (HWND hwnd)
  237. {
  238.   return ERROR_SUCCESS;
  239. }
  240. //****************************************************************************
  241. // DWORD NEAR PASCAL EditScriptFile (HWND)
  242. //
  243. // This function brings up the associated editor for the script.
  244. //
  245. // History:
  246. //  Tue 07-Jun-1995 08:02:00  -by-  Scott Hysom  [scotth]
  247. // Created.
  248. //****************************************************************************
  249. DWORD NEAR PASCAL EditScriptFile(HWND hwnd)
  250. {
  251.   HWND          hCtrl;
  252.   SHELLEXECUTEINFO sei;
  253.   char          szFileName[MAX_PATH];
  254.   DWORD         dwRet;
  255.   hCtrl = GetDlgItem(hwnd, IDC_SCRIPT_NAME);
  256.   Edit_GetText(hCtrl, szFileName, sizeof(szFileName));
  257.   ZeroMemory(&sei, sizeof(sei));
  258.   sei.cbSize = sizeof(sei);
  259.   sei.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_DOENVSUBST;
  260.   sei.hwnd = hwnd;
  261.   sei.lpVerb = "Open";
  262.   sei.lpFile = szFileName;
  263.   sei.nShow = SW_NORMAL;
  264.   sei.hInstApp = ghInstance;
  265.   dwRet = ERROR_SUCCESS;      // assume success
  266.   if (!ShellExecuteEx(&sei))
  267.   {
  268.     char szCommand[MAX_PATH];
  269.     // We failed to open the file, default to Notepad
  270.     lstrcpy(szCommand, "notepad");
  271.     sei.lpFile = szCommand;
  272.     sei.lpParameters = szFileName;
  273.     sei.fMask &= ~SEE_MASK_FLAG_NO_UI;    // show any errors this time
  274.     if (!ShellExecuteEx(&sei))
  275.       dwRet = GetLastError();
  276.   };
  277.   return dwRet;
  278. }
  279. //****************************************************************************
  280. // DWORD NEAR PASCAL BrowseScriptFile (HWND)
  281. //
  282. // This function adjusts the dialog layout.
  283. //
  284. // History:
  285. //  Tue 08-Nov-1994 09:14:13  -by-  Viroon  Touranachun [viroont]
  286. // Created.
  287. //****************************************************************************
  288. DWORD NEAR PASCAL BrowseScriptFile(HWND hwnd)
  289. {
  290.   HWND          hCtrl;
  291.   OPENFILENAME  ofn;
  292.   LPSTR         pszFiles, szFileName, szFilter;
  293.   DWORD         dwRet;
  294.   // Allocate filename buffer
  295.   //
  296.   if ((pszFiles = (LPSTR)LocalAlloc(LPTR, 2*MAX_PATH)) == NULL)
  297.     return ERROR_OUTOFMEMORY;
  298.   szFileName = pszFiles;
  299.   szFilter   = szFileName+MAX_PATH;
  300.   // Start file browser dialog
  301.   //
  302.   LoadString(ghInstance, IDS_FILE_FILTER, szFilter, MAX_PATH);
  303.   *szFileName     = '';
  304.   ofn.lStructSize = sizeof(ofn);
  305.   ofn.hwndOwner   = hwnd;
  306.   ofn.hInstance   = ghInstance;
  307.   ofn.lpstrFilter = szFilter;
  308.   ofn.lpstrCustomFilter = NULL;
  309.   ofn.nMaxCustFilter    = 0;
  310.   ofn.nFilterIndex      = 2;
  311.   ofn.lpstrFile         = szFileName;
  312.   ofn.nMaxFile          = MAX_PATH;
  313.   ofn.lpstrFileTitle    = NULL;
  314.   ofn.nMaxFileTitle     = 0;
  315.   ofn.lpstrInitialDir   = NULL;
  316.   ofn.lpstrTitle        = NULL;
  317.   ofn.Flags             = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  318.   ofn.nFileOffset       = 0;
  319.   ofn.nFileExtension    = 0;
  320.   ofn.lpstrDefExt       = NULL;
  321.   ofn.lCustData         = 0;
  322.   ofn.lpfnHook          = NULL;
  323.   ofn.lpTemplateName    = NULL;
  324.   if (GetOpenFileName(&ofn))
  325.   {
  326.     // Set the filename to a new name
  327.     //
  328.     hCtrl = GetDlgItem(hwnd, IDC_SCRIPT_NAME);
  329.     Edit_SetText(hCtrl, szFileName);
  330.     Edit_SetSel(hCtrl, 0, -1);
  331.     SetFocus(hCtrl);
  332.     EnableWindow(GetDlgItem(hwnd, IDC_DEBUG),
  333.                  (*szFileName == '') ? FALSE : TRUE);
  334.     EnableWindow(GetDlgItem(hwnd, IDC_EDIT), 0 != *szFileName);
  335.     dwRet = ERROR_SUCCESS;
  336.   }
  337.   else
  338.   {
  339.     dwRet = ERROR_OPEN_FAILED;
  340.   };
  341.   LocalFree(pszFiles);
  342.   return dwRet;
  343. }
  344. //****************************************************************************
  345. // DWORD NEAR PASCAL CheckScriptDlgData (HWND)
  346. //
  347. // This function checks the valid data..
  348. //
  349. // History:
  350. //  Tue 08-Nov-1994 09:14:13  -by-  Viroon  Touranachun [viroont]
  351. // Created.
  352. //****************************************************************************
  353. DWORD NEAR PASCAL CheckScriptDlgData(HWND hwnd)
  354. {
  355.   HWND  hCtrl;
  356.   OFSTRUCT of;
  357.   DWORD dwRet = ERROR_SUCCESS;
  358.   // Get the current script name
  359.   //
  360.   hCtrl = GetDlgItem(hwnd, IDC_SCRIPT_NAME);
  361.   Edit_GetText(hCtrl, of.szPathName, sizeof(of.szPathName));
  362.   // Check whether the file exist
  363.   //
  364.   if (of.szPathName[0] != '')
  365.   {
  366.     of.cBytes = sizeof(of);
  367.     if (OpenFile(of.szPathName, &of, OF_EXIST) == HFILE_ERROR)
  368.     {
  369.       if (RuiUserMessage(hwnd,
  370.                          IDS_ERR_FILE_NOT_EXIST,
  371.                          MB_YESNO | MB_ICONEXCLAMATION) == IDNO)
  372.       {
  373.         Edit_SetSel(hCtrl, 0, -1);
  374.         SetFocus(hCtrl);
  375.         dwRet = ERROR_FILE_NOT_FOUND;
  376.       };
  377.     };
  378.   };
  379.   return dwRet;
  380. }
  381. //****************************************************************************
  382. // DWORD NEAR PASCAL SaveScriptDlgData (HWND)
  383. //
  384. // This function saves the data permanently.
  385. //
  386. // History:
  387. //  Tue 08-Nov-1994 09:14:13  -by-  Viroon  Touranachun [viroont]
  388. // Created.
  389. //****************************************************************************
  390. DWORD NEAR PASCAL SaveScriptDlgData(HWND hwnd)
  391. {
  392.   PCONNENTDLG pConnEntDlg;
  393.   HWND  hCtrl;
  394.   WINDOWPLACEMENT wp;
  395.   UINT  uMode;
  396.   HKEY  hkey, hkeyEntry;
  397.   DWORD dwRet;
  398.   char  szPathName[MAX_PATH];
  399.   // Get the current script name
  400.   //
  401.   hCtrl = GetDlgItem(hwnd, IDC_SCRIPT_NAME);
  402.   Edit_GetText(hCtrl, szPathName, sizeof(szPathName));
  403.   // Get the connection entry
  404.   //
  405.   pConnEntDlg = (PCONNENTDLG)GetWindowLong(hwnd, DWL_USER);
  406.   // Write the name to the registry key
  407.   //
  408.   if (RegCreateKey(HKEY_CURRENT_USER, g_szProfile, &hkey) == ERROR_SUCCESS)
  409.   {
  410.     if (RegCreateKey(hkey, pConnEntDlg->pConnEntry->pszEntry, &hkeyEntry)
  411.         == ERROR_SUCCESS)
  412.     {
  413.       DWORD cbSize, dwType;
  414.       // If the name exists, update it. Otherwise, remove it.
  415.       //
  416.       if (szPathName[0] != '')
  417.       {
  418.         RegSetValueEx(hkeyEntry, g_szScript, 0, REG_SZ, szPathName,
  419.                       lstrlen(szPathName)+1);
  420.       }
  421.       else
  422.       {
  423.         RegDeleteValue(hkeyEntry, g_szScript);
  424.       };
  425.       // Get the current window setting
  426.       //
  427.       cbSize = sizeof(wp);
  428.       if (RegQueryValueEx(hkeyEntry, g_szPlacement, 0, &dwType, (LPBYTE)&wp,
  429.                           &cbSize) != ERROR_SUCCESS)
  430.       {
  431.         // The current setting is not there, use the default value
  432.         //
  433.         ZeroMemory(&wp, sizeof(wp));
  434.         wp.showCmd = SW_SHOWMINNOACTIVE;
  435.       };
  436.       if (IsDlgButtonChecked(hwnd, IDC_MINIMIZED))
  437.       {
  438.         wp.showCmd = SW_SHOWMINNOACTIVE;
  439.       }
  440.       else
  441.       {
  442.         // If the user specifed not-minimized but it is minimized
  443.         //
  444.         if (ISMINIMIZED(wp.showCmd))
  445.         {
  446.           // Set to show normal
  447.           //
  448.           wp.showCmd = SW_SHOWNORMAL;
  449.         };
  450.       };
  451.       RegSetValueEx(hkeyEntry, g_szPlacement, 0, REG_BINARY, (LPBYTE)&wp,
  452.                     sizeof(wp));
  453.       // Set the debug mode
  454.       //
  455.       uMode = (IsWindowEnabled(GetDlgItem(hwnd, IDC_DEBUG)) &&
  456.                IsDlgButtonChecked(hwnd, IDC_DEBUG)) ? TEST_MODE : NORMAL_MODE;
  457.       RegSetValueEx(hkeyEntry, g_szMode, 0, REG_BINARY, (LPBYTE)&uMode,
  458.                     sizeof(uMode));
  459.       RegCloseKey(hkeyEntry);
  460.       dwRet = ERROR_SUCCESS;
  461.     }
  462.     else
  463.     {
  464.       dwRet = ERROR_CANNOT_OPEN_PHONEBOOK;
  465.     };
  466.     RegCloseKey(hkey);
  467.   }
  468.   else
  469.   {
  470.     dwRet = ERROR_CANNOT_OPEN_PHONEBOOK;
  471.   };
  472.   return dwRet;
  473. }