Source code of Windows XP (NT5)
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.

519 lines
15 KiB

  1. // CertCtl.cpp : Implementation of the CCertmapCtrl OLE control class.
  2. #include "stdafx.h"
  3. #include "certmap.h"
  4. #include "CertCtl.h"
  5. #include "CertPpg.h"
  6. extern "C"
  7. {
  8. #include <wincrypt.h>
  9. #include <sslsp.h>
  10. }
  11. // persistence and mapping includes
  12. #include "Iismap.hxx"
  13. #include "Iiscmr.hxx"
  14. #include "WrapMaps.h"
  15. #include "ListRow.h"
  16. #include "ChkLstCt.h"
  17. #include "wrapmb.h"
  18. #include "Map11Pge.h"
  19. #include "MapWPge.h"
  20. //#include <iiscnfg.h>
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. IMPLEMENT_DYNCREATE(CCertmapCtrl, COleControl)
  27. /////////////////////////////////////////////////////////////////////////////
  28. // Message map
  29. BEGIN_MESSAGE_MAP(CCertmapCtrl, COleControl)
  30. //{{AFX_MSG_MAP(CCertmapCtrl)
  31. //}}AFX_MSG_MAP
  32. ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  33. END_MESSAGE_MAP()
  34. /////////////////////////////////////////////////////////////////////////////
  35. // Dispatch map
  36. BEGIN_DISPATCH_MAP(CCertmapCtrl, COleControl)
  37. //{{AFX_DISPATCH_MAP(CCertmapCtrl)
  38. DISP_FUNCTION(CCertmapCtrl, "SetServerInstance", SetServerInstance, VT_EMPTY, VTS_BSTR)
  39. DISP_FUNCTION(CCertmapCtrl, "SetMachineName", SetMachineName, VT_EMPTY, VTS_BSTR)
  40. DISP_STOCKFUNC_DOCLICK()
  41. DISP_STOCKPROP_FONT()
  42. DISP_STOCKPROP_ENABLED()
  43. DISP_STOCKPROP_BORDERSTYLE()
  44. DISP_STOCKPROP_CAPTION()
  45. //}}AFX_DISPATCH_MAP
  46. END_DISPATCH_MAP()
  47. /////////////////////////////////////////////////////////////////////////////
  48. // Event map
  49. BEGIN_EVENT_MAP(CCertmapCtrl, COleControl)
  50. //{{AFX_EVENT_MAP(CCertmapCtrl)
  51. EVENT_STOCK_CLICK()
  52. EVENT_STOCK_KEYUP()
  53. //}}AFX_EVENT_MAP
  54. END_EVENT_MAP()
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Property pages
  57. // TODO: Add more property pages as needed. Remember to increase the count!
  58. BEGIN_PROPPAGEIDS(CCertmapCtrl, 2)
  59. PROPPAGEID(CCertmapPropPage::guid)
  60. PROPPAGEID(CLSID_CFontPropPage)
  61. END_PROPPAGEIDS(CCertmapCtrl)
  62. /////////////////////////////////////////////////////////////////////////////
  63. // Initialize class factory and guid
  64. IMPLEMENT_OLECREATE_EX(CCertmapCtrl, "CERTMAP.CertmapCtrl.1",
  65. 0xbbd8f29b, 0x6f61, 0x11d0, 0xa2, 0x6e, 0x8, 0, 0x2b, 0x2c, 0x6f, 0x32)
  66. /////////////////////////////////////////////////////////////////////////////
  67. // Type library ID and version
  68. IMPLEMENT_OLETYPELIB(CCertmapCtrl, _tlid, _wVerMajor, _wVerMinor)
  69. /////////////////////////////////////////////////////////////////////////////
  70. // Interface IDs
  71. const IID BASED_CODE IID_DCertmap =
  72. { 0xbbd8f299, 0x6f61, 0x11d0, { 0xa2, 0x6e, 0x8, 0, 0x2b, 0x2c, 0x6f, 0x32 } };
  73. const IID BASED_CODE IID_DCertmapEvents =
  74. { 0xbbd8f29a, 0x6f61, 0x11d0, { 0xa2, 0x6e, 0x8, 0, 0x2b, 0x2c, 0x6f, 0x32 } };
  75. /////////////////////////////////////////////////////////////////////////////
  76. // Control type information
  77. static const DWORD BASED_CODE _dwCertmapOleMisc =
  78. OLEMISC_ACTIVATEWHENVISIBLE |
  79. OLEMISC_SETCLIENTSITEFIRST |
  80. OLEMISC_INSIDEOUT |
  81. OLEMISC_CANTLINKINSIDE |
  82. OLEMISC_ACTSLIKEBUTTON |
  83. OLEMISC_RECOMPOSEONRESIZE;
  84. IMPLEMENT_OLECTLTYPE(CCertmapCtrl, IDS_CERTMAP, _dwCertmapOleMisc)
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CCertmapCtrl::CCertmapCtrlFactory::UpdateRegistry -
  87. // Adds or removes system registry entries for CCertmapCtrl
  88. BOOL CCertmapCtrl::CCertmapCtrlFactory::UpdateRegistry(BOOL bRegister)
  89. {
  90. // TODO: Verify that your control follows apartment-model threading rules.
  91. // Refer to MFC TechNote 64 for more information.
  92. // If your control does not conform to the apartment-model rules, then
  93. // you must modify the code below, changing the 6th parameter from
  94. // afxRegApartmentThreading to 0.
  95. if (bRegister)
  96. return AfxOleRegisterControlClass(
  97. AfxGetInstanceHandle(),
  98. m_clsid,
  99. m_lpszProgID,
  100. IDS_CERTMAP,
  101. IDB_CERTMAP,
  102. afxRegApartmentThreading,
  103. _dwCertmapOleMisc,
  104. _tlid,
  105. _wVerMajor,
  106. _wVerMinor);
  107. else
  108. return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  109. }
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CCertmapCtrl::CCertmapCtrl - Constructor
  112. CCertmapCtrl::CCertmapCtrl():
  113. m_fUpdateFont( FALSE ),
  114. m_hAccel( NULL ),
  115. m_cAccel( 0 )
  116. {
  117. InitializeIIDs(&IID_DCertmap, &IID_DCertmapEvents);
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CCertmapCtrl::~CCertmapCtrl - Destructor
  121. CCertmapCtrl::~CCertmapCtrl()
  122. {
  123. if ( m_hAccel )
  124. DestroyAcceleratorTable( m_hAccel );
  125. m_hAccel = NULL;
  126. }
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CCertmapCtrl::OnDraw - Drawing function
  129. void CCertmapCtrl::OnDraw( CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid )
  130. {
  131. DoSuperclassPaint(pdc, rcBounds);
  132. /*
  133. CFont* pOldFont;
  134. // select the stock font, recording the old one
  135. pOldFont = SelectStockFont( pdc );
  136. // do the superclass draw
  137. DoSuperclassPaint(pdc, rcBounds);
  138. // restore the old font - sneakily getting the correct font object
  139. pOldFont = pdc->SelectObject(pOldFont);
  140. // we want the button window to continue drawing in the correct font even
  141. // when we are not using OnDraw. i.e. when it is being pushed down. This
  142. // means we need to set the CWnd::SetFont() method.
  143. if ( m_fUpdateFont )
  144. {
  145. m_fUpdateFont = FALSE;
  146. CWnd::SetFont( pOldFont );
  147. }
  148. DoSuperclassPaint(pdc, rcBounds);
  149. */
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CCertmapCtrl::DoPropExchange - Persistence support
  153. void CCertmapCtrl::DoPropExchange( CPropExchange* pPX )
  154. {
  155. ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  156. COleControl::DoPropExchange(pPX);
  157. }
  158. /////////////////////////////////////////////////////////////////////////////
  159. // CCertmapCtrl::OnResetState - Reset control to default state
  160. void CCertmapCtrl::OnResetState()
  161. {
  162. COleControl::OnResetState(); // Resets defaults found in DoPropExchange
  163. }
  164. /////////////////////////////////////////////////////////////////////////////
  165. // CCertmapCtrl message handlers
  166. //---------------------------------------------------------------------------
  167. BOOL CCertmapCtrl::PreCreateWindow(CREATESTRUCT& cs)
  168. {
  169. if ( cs.style & WS_CLIPSIBLINGS )
  170. cs.style ^= WS_CLIPSIBLINGS;
  171. cs.lpszClass = _T("BUTTON");
  172. return COleControl::PreCreateWindow(cs);
  173. }
  174. /////////////////////////////////////////////////////////////////////////////
  175. // CAppsCtrl::IsSubclassedControl - This is a subclassed control
  176. BOOL CCertmapCtrl::IsSubclassedControl()
  177. {
  178. return TRUE;
  179. }
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CAppsCtrl::OnOcmCommand - Handle command messages
  182. LRESULT CCertmapCtrl::OnOcmCommand(WPARAM wParam, LPARAM lParam)
  183. {
  184. #ifdef _WIN32
  185. WORD wNotifyCode = HIWORD(wParam);
  186. #else
  187. WORD wNotifyCode = HIWORD(lParam);
  188. #endif
  189. return 0;
  190. }
  191. //---------------------------------------------------------------------------
  192. void CCertmapCtrl::OnClick(USHORT iButton)
  193. {
  194. // in case there are any errors, prepare the error string
  195. CString sz;
  196. sz.LoadString( IDS_ERR_CERTMAP_TITLE );
  197. // free the existing name, and copy in the new one
  198. // tjp: you should compare if the old name matches the current name
  199. // and only then free and malloc the new name -- chances are that
  200. // the names are the same +++ all the free/malloc can fragment mem.
  201. free((void*)AfxGetApp()->m_pszAppName);
  202. AfxGetApp()->m_pszAppName = _tcsdup(sz);
  203. // this is the whole purpose of the control
  204. RunMappingDialog();
  205. // we are not in the business of telling the host to do
  206. // something here, so just don't fire anything off.
  207. COleControl::OnClick(iButton);
  208. }
  209. //---------------------------------------------------------------------------
  210. void CCertmapCtrl::RunMappingDialog()
  211. {
  212. //
  213. // UNICODE/ANSI conversion - RonaldM
  214. //
  215. // prepare the machine name pointer
  216. USES_CONVERSION;
  217. OLECHAR * poch = NULL;
  218. if ( !m_szMachineName.IsEmpty() )
  219. {
  220. // allocate the name buffer, no need to free
  221. poch = T2OLE((LPTSTR)(LPCTSTR)m_szMachineName);
  222. if ( !poch )
  223. {
  224. MessageBeep(0);
  225. return;
  226. }
  227. }
  228. // initialize the metabase wrappings - pass in the name of the target machine
  229. // if one has been specified
  230. //
  231. // Changed to generic metabase wrapper class - RonaldM
  232. //
  233. //IMSAdminBase * pMB = FInitMetabaseWrapper( poch );
  234. //if ( !pMB )
  235. IMSAdminBase * pMB = NULL;
  236. if (!FInitMetabaseWrapperEx( poch, &pMB ))
  237. {
  238. MessageBeep(0);
  239. return;
  240. }
  241. // the 1:1 mapping and rule-based mapping are panes in a single dialog window.
  242. // first we must build the propertysheet dialog and add the panes
  243. // pointers to the pages (construction may throw, so we need to be careful)
  244. CMap11Page page11mapping;
  245. CMapWildcardsPge pageWildMapping;
  246. // declare the property sheet
  247. CPropertySheet propsheet( IDS_MAP_SHEET_TITLE );
  248. // Things could throw here, so better protect it.
  249. try
  250. {
  251. // if there is nothing in the MB_Path, default to the first instance
  252. if ( m_szServerInstance.IsEmpty() )
  253. m_szServerInstance = _T("/LM/W3SVC/1");
  254. // I am assuming that the last character is NOT a '/' Thus, if that is what is
  255. // there, we need to remove it. Otherwise, the path gets messed up later
  256. if ( m_szServerInstance.Right(1) == _T('/') )
  257. m_szServerInstance = m_szServerInstance.Left( m_szServerInstance.GetLength()-1 );
  258. // tell the pages about the metabase path property
  259. page11mapping.m_szMBPath = m_szServerInstance + SZ_NAMESPACE_EXTENTION;
  260. pageWildMapping.m_szMBPath = m_szServerInstance + SZ_NAMESPACE_EXTENTION;
  261. // do any other initializing of the pages
  262. page11mapping.FInit(pMB);
  263. pageWildMapping.FInit(pMB);
  264. }
  265. catch ( CException e )
  266. {
  267. }
  268. // add the pages to the sheet
  269. propsheet.AddPage( &page11mapping );
  270. propsheet.AddPage( &pageWildMapping );
  271. // turn on help
  272. propsheet.m_psh.dwFlags |= PSH_HASHELP;
  273. page11mapping.m_psp.dwFlags |= PSP_HASHELP;
  274. pageWildMapping.m_psp.dwFlags |= PSP_HASHELP;
  275. // Things could (potentially maybe) throw here, so better protect it.
  276. try
  277. {
  278. // run the propdsheet dialog
  279. // let the host container know that we are putting up a modal dialog
  280. PreModalDialog();
  281. // run the dialog
  282. // tjp: should we not test the outcome of the dialog?
  283. // could the user ESCAPE out of it w/o doing anything?
  284. propsheet.DoModal();
  285. // let the host container know we are done with the modality
  286. PostModalDialog();
  287. }
  288. catch ( CException e )
  289. {
  290. }
  291. // close the metabase wrappings
  292. //
  293. // Changed to generic wrapper -- RonaldM
  294. FCloseMetabaseWrapperEx(&pMB);
  295. }
  296. //---------------------------------------------------------------------------
  297. void CCertmapCtrl::SetServerInstance(LPCTSTR szServerInstance)
  298. {
  299. m_szServerInstance = szServerInstance;
  300. }
  301. //---------------------------------------------------------------------------
  302. void CCertmapCtrl::SetMachineName(LPCTSTR szMachine)
  303. {
  304. m_szMachineName = szMachine;
  305. }
  306. //---------------------------------------------------------------------------
  307. void CCertmapCtrl::OnFontChanged()
  308. {
  309. m_fUpdateFont = TRUE;
  310. COleControl::OnFontChanged();
  311. }
  312. //---------------------------------------------------------------------------
  313. void CCertmapCtrl::OnAmbientPropertyChange(DISPID dispid)
  314. {
  315. BOOL flag;
  316. UINT style;
  317. // do the right thing depending on the dispid
  318. switch ( dispid )
  319. {
  320. case DISPID_AMBIENT_DISPLAYASDEFAULT:
  321. if ( GetAmbientProperty( DISPID_AMBIENT_DISPLAYASDEFAULT, VT_BOOL, &flag ) )
  322. {
  323. style = GetWindowLong(
  324. GetSafeHwnd(), // handle of window
  325. GWL_STYLE // offset of value to retrieve
  326. );
  327. if ( flag )
  328. style |= BS_DEFPUSHBUTTON;
  329. else
  330. style ^= BS_DEFPUSHBUTTON;
  331. SetWindowLong(
  332. GetSafeHwnd(), // handle of window
  333. GWL_STYLE, // offset of value to retrieve
  334. style
  335. );
  336. Invalidate(TRUE);
  337. }
  338. break;
  339. };
  340. COleControl::OnAmbientPropertyChange(dispid);
  341. }
  342. //---------------------------------------------------------------------------
  343. // an important method where we tell the container how to deal with us.
  344. // pControlInfo is passed in by the container, although we are responsible
  345. // for maintining the hAccel structure
  346. void CCertmapCtrl::OnGetControlInfo(LPCONTROLINFO pControlInfo)
  347. {
  348. // do a rudimentary check to see if we understand pControlInfo
  349. if ( !pControlInfo || pControlInfo->cb < sizeof(CONTROLINFO) )
  350. return;
  351. // set the accelerator handle into place
  352. pControlInfo->hAccel = m_hAccel;
  353. pControlInfo->cAccel = m_cAccel;
  354. // when we have focus, we do want the enter key
  355. pControlInfo->dwFlags = CTRLINFO_EATS_RETURN;
  356. }
  357. //---------------------------------------------------------------------------
  358. // the ole control container object specifically filters out the space
  359. // key so we do not get it as a OnMnemonic call. Thus we need to look
  360. // for it ourselves
  361. void CCertmapCtrl::OnKeyUpEvent(USHORT nChar, USHORT nShiftState)
  362. {
  363. if ( nChar == _T(' ') )
  364. {
  365. OnClick((USHORT)GetDlgCtrlID());
  366. }
  367. COleControl::OnKeyUpEvent(nChar, nShiftState);
  368. }
  369. //---------------------------------------------------------------------------
  370. void CCertmapCtrl::OnMnemonic(LPMSG pMsg)
  371. {
  372. OnClick((USHORT)GetDlgCtrlID());
  373. COleControl::OnMnemonic(pMsg);
  374. }
  375. //---------------------------------------------------------------------------
  376. void CCertmapCtrl::OnTextChanged()
  377. {
  378. DWORD i;
  379. ACCEL accel;
  380. BOOL f;
  381. BOOL flag;
  382. int iAccel;
  383. // get the new text
  384. CString sz = InternalGetText();
  385. sz.MakeLower();
  386. // if the handle has already been allocated, free it
  387. if ( m_hAccel )
  388. {
  389. DestroyAcceleratorTable( m_hAccel );
  390. m_hAccel = NULL;
  391. m_cAccel = 0;
  392. }
  393. // if there is a & character, then declare the accelerator
  394. iAccel = sz.Find(_T('&'));
  395. if ( iAccel >= 0 )
  396. {
  397. // fill in the accererator record
  398. accel.fVirt = FALT;
  399. accel.key = sz.GetAt(iAccel + 1);
  400. accel.cmd = (USHORT)GetDlgCtrlID();
  401. m_hAccel = CreateAcceleratorTable( &accel, 1 );
  402. if ( m_hAccel )
  403. m_cAccel = 1;
  404. }
  405. // finish with the default handling.
  406. COleControl::OnTextChanged();
  407. }