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.

115 lines
2.8 KiB

  1. //-----------------------------------------------------------------------------
  2. // SingleInst.h
  3. //-----------------------------------------------------------------------------
  4. #ifndef _SINGLEINST_H
  5. #define _SINGLEINST_H
  6. class CSingleInstance
  7. {
  8. public:
  9. CSingleInstance( LPTSTR strID ) :
  10. m_hFileMap(NULL),
  11. m_pdwID(NULL),
  12. m_strID(NULL)
  13. {
  14. if ( NULL != strID )
  15. {
  16. m_strID = new TCHAR[ _tcslen( strID ) + 1 ];
  17. if ( NULL != m_strID )
  18. _tcscpy( m_strID, strID );
  19. }
  20. }
  21. ~CSingleInstance()
  22. {
  23. // if we have PID we're mapped
  24. if( m_pdwID )
  25. {
  26. UnmapViewOfFile( m_pdwID );
  27. m_pdwID = NULL;
  28. }
  29. // if we have a handle close it
  30. if( m_hFileMap )
  31. {
  32. CloseHandle( m_hFileMap );
  33. m_hFileMap = NULL;
  34. }
  35. if ( NULL != m_strID )
  36. {
  37. delete [] m_strID;
  38. m_strID = NULL;
  39. }
  40. }
  41. static BOOL CALLBACK enumProc( HWND hWnd, LPARAM lParam )
  42. {
  43. DWORD dwID = 0;
  44. GetWindowThreadProcessId( hWnd, &dwID );
  45. // JeffZi - 13800: when the tooltips_class32 was being created after the welcome page of the wizards,
  46. // it was being returned as the first window for this PID. so, make sure this window
  47. // has children before setting focus
  48. if( (dwID == (DWORD)lParam) &&
  49. GetWindow(hWnd, GW_CHILD) )
  50. {
  51. SetForegroundWindow( hWnd );
  52. SetFocus( hWnd );
  53. return FALSE;
  54. }
  55. return TRUE;
  56. }
  57. BOOL IsOpen( VOID )
  58. {
  59. return !(Open());
  60. }
  61. private:
  62. BOOL Open( VOID )
  63. {
  64. BOOL bRC = FALSE;
  65. m_hFileMap = CreateFileMapping( (HANDLE)-1, NULL, PAGE_READWRITE, 0, sizeof(DWORD), m_strID );
  66. if( NULL != m_hFileMap )
  67. {
  68. if ( ERROR_ALREADY_EXISTS == GetLastError())
  69. {
  70. // get the pid and bring the other window to the front
  71. DWORD* pdwID = static_cast<DWORD *>( MapViewOfFile( m_hFileMap, FILE_MAP_READ, 0, 0, sizeof(DWORD) ) );
  72. if( pdwID )
  73. {
  74. DWORD dwID = *pdwID;
  75. UnmapViewOfFile( pdwID );
  76. EnumWindows( enumProc, (LPARAM)dwID );
  77. }
  78. CloseHandle( m_hFileMap );
  79. m_hFileMap = NULL;
  80. }
  81. else
  82. {
  83. m_pdwID = static_cast<DWORD *>( MapViewOfFile( m_hFileMap, FILE_MAP_WRITE, 0, 0, sizeof(DWORD) ) );
  84. if ( NULL != m_pdwID )
  85. {
  86. *m_pdwID = GetCurrentProcessId();
  87. bRC = TRUE;
  88. }
  89. }
  90. }
  91. return bRC;
  92. }
  93. private:
  94. LPTSTR m_strID;
  95. HANDLE m_hFileMap;
  96. DWORD* m_pdwID;
  97. }; // class CSingleInstance
  98. #endif // _SINGLEINST_H