Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
7.5 KiB

  1. // DlgAddr.h : Declaration of the CScriptSecurityDialog
  2. #ifndef __SCRIPTSECURITYDIALOG_H_
  3. #define __SCRIPTSECURITYDIALOG_H_
  4. #include "resource.h" // main symbols
  5. /////////////////////////////////////////////////////////////////////////////
  6. // CScriptSecurityDialog
  7. //
  8. // this class is not thread safe
  9. //
  10. class CScriptSecurityDialog :
  11. public CDialogImpl<CScriptSecurityDialog>
  12. {
  13. public:
  14. CScriptSecurityDialog ()
  15. :m_hModule(NULL),
  16. m_psMessageText(NULL)
  17. {
  18. //
  19. // the resource module is used to load the string and the dialog itself
  20. // so keep it around as a data member
  21. //
  22. m_hModule = ::LoadLibrary(_T("tapiui.dll"));
  23. }
  24. ~CScriptSecurityDialog ()
  25. {
  26. if (m_hModule)
  27. {
  28. FreeLibrary(m_hModule);
  29. m_hModule = NULL;
  30. }
  31. }
  32. INT_PTR DoModalWithText(UINT uResourceID, HWND hWndParent = ::GetActiveWindow())
  33. {
  34. //
  35. // this assertion could fail is if the class is used from
  36. // a multithreaded app, and domodalX is called on two different threads
  37. // the class is not thread safe and this should be caught during
  38. // testing.
  39. // another way this assert could fire is if the class itself is
  40. // broken. this, too, is a test-time error.
  41. //
  42. _ASSERTE(NULL == m_psMessageText);
  43. //
  44. // load string from resource module
  45. //
  46. m_psMessageText = SafeLoadString(uResourceID);
  47. //
  48. // if failed, bail out now
  49. //
  50. if (NULL == m_psMessageText)
  51. {
  52. return -1;
  53. }
  54. //
  55. // attempt to display the dialog box
  56. // the string is used in OnInitDialog to set the dialog's text
  57. //
  58. INT_PTR rc = _DoModal(hWndParent);
  59. //
  60. // deallocate string
  61. //
  62. delete m_psMessageText;
  63. m_psMessageText = NULL;
  64. return rc;
  65. }
  66. INT_PTR DoModalWithText(LPTSTR psMessageText, HWND hWndParent = ::GetActiveWindow())
  67. {
  68. //
  69. // this assertion could fail is if the class is used from
  70. // a multithreaded app, and domodalX is called on two different threads
  71. // the class is not thread safe and this should be caught during
  72. // testing.
  73. // another way this assert could fire is if the class itself is
  74. // broken. this, too, is a test-time error.
  75. //
  76. _ASSERTE(NULL == m_psMessageText);
  77. //
  78. // the dialog is modal, so the lifetime of psMessageText is guaranteed
  79. // to exceed the lifetime of the dialog.
  80. //
  81. m_psMessageText = psMessageText;
  82. //
  83. // attempt to display the dialog. the string will be used to set
  84. // the message text in OnInitDialog
  85. //
  86. INT_PTR rc = _DoModal(hWndParent);
  87. //
  88. // no longer need the string + the string cannot be assumed
  89. // valid after we return
  90. //
  91. m_psMessageText = NULL;
  92. return rc;
  93. }
  94. enum { IDD = IDD_TAPI_SECURITY_DIALOG };
  95. public:
  96. BEGIN_MSG_MAP(CScriptSecurityDialog)
  97. MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
  98. COMMAND_ID_HANDLER(ID_YES, OnYes)
  99. COMMAND_ID_HANDLER(ID_NO, OnNo)
  100. END_MSG_MAP()
  101. //
  102. // Attributes
  103. //
  104. private:
  105. //
  106. // the module where the resource is contained
  107. // needed for the prompt string and for the dialog itself
  108. //
  109. HINSTANCE m_hModule;
  110. //
  111. // the prompt text
  112. //
  113. LPTSTR m_psMessageText;
  114. protected:
  115. LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  116. {
  117. //
  118. // m_psMessageText must be set before _DoModal is called
  119. // if m_psMessageText is null here, the error is in the class itself
  120. // and this should be detected during testing
  121. //
  122. _ASSERTE(NULL != m_psMessageText);
  123. //
  124. // display the text that was passed into DoModalWithText as a string
  125. // or a resources
  126. //
  127. SetDlgItemText(IDC_SECURITY_WARNING_TEXT, m_psMessageText);
  128. return TRUE;
  129. }
  130. LRESULT OnYes(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  131. {
  132. //
  133. // see if do not ask in the future is set
  134. //
  135. if (IsDlgButtonChecked(IDC_DONOT_PROMPT_IN_THE_FUTURE))
  136. wID = ID_YES_DONT_ASK_AGAIN;
  137. EndDialog(wID);
  138. return FALSE;
  139. }
  140. LRESULT OnNo(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  141. {
  142. EndDialog(wID);
  143. return FALSE;
  144. }
  145. private:
  146. INT_PTR _DoModal(HWND hWndParent)
  147. {
  148. //
  149. // if the resource dll failed to load, bail out
  150. //
  151. if (NULL == m_hModule)
  152. {
  153. return -1;
  154. }
  155. //
  156. // otherwise, attempt to display the dialog box
  157. //
  158. _ASSERTE(m_hWnd == NULL);
  159. _Module.AddCreateWndData(&m_thunk.cd, (CDialogImplBase*)this);
  160. INT_PTR nRet = ::DialogBoxParam(m_hModule,
  161. MAKEINTRESOURCE(CScriptSecurityDialog::IDD),
  162. hWndParent,
  163. CScriptSecurityDialog::StartDialogProc,
  164. NULL);
  165. m_hWnd = NULL;
  166. return nRet;
  167. }
  168. private:
  169. //
  170. // Load string for this resource. Safe with respect to string size
  171. //
  172. TCHAR *SafeLoadString( UINT uResourceID )
  173. {
  174. TCHAR *pszTempString = NULL;
  175. int nCurrentSizeInChars = 128;
  176. int nCharsCopied = 0;
  177. do
  178. {
  179. if ( NULL != pszTempString )
  180. {
  181. delete pszTempString;
  182. pszTempString = NULL;
  183. }
  184. nCurrentSizeInChars *= 2;
  185. pszTempString = new TCHAR[ nCurrentSizeInChars ];
  186. if (NULL == pszTempString)
  187. {
  188. return NULL;
  189. }
  190. nCharsCopied = ::LoadString( m_hModule,
  191. uResourceID,
  192. pszTempString,
  193. nCurrentSizeInChars
  194. );
  195. if ( 0 == nCharsCopied )
  196. {
  197. delete pszTempString;
  198. return NULL;
  199. }
  200. //
  201. // nCharsCopied does not include the null terminator
  202. // so compare it to the size of the buffer - 1
  203. // if the buffer was filled completely, retry with a bigger buffer
  204. //
  205. } while ( (nCharsCopied >= (nCurrentSizeInChars - 1) ) );
  206. return pszTempString;
  207. }
  208. //
  209. // private, not to be called. the dialog must be created with DoModalWithText
  210. //
  211. HWND Create(HWND hWndParent, LPCTSTR psMessageText = NULL)
  212. {
  213. // this dialog must be created as modal
  214. _ASSERTE(FALSE);
  215. return NULL;
  216. }
  217. //
  218. // private, not to be called. the dialog must be created with DoModalWithText
  219. //
  220. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  221. {
  222. _ASSERTE(FALSE);
  223. return -1;
  224. }
  225. };
  226. #endif //__SCRIPTSECURITYDIALOG_H_