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.

242 lines
7.6 KiB

  1. /*----------------------------------------------------------------------------
  2. rnawnd.cpp
  3. Functions to zap the RNA windows
  4. Copyright (C) 1995 Microsoft Corporation
  5. All rights reserved.
  6. Authors:
  7. ArulM
  8. ChrisK Updated for ICW usage
  9. --------------------------------------------------------------------------*/
  10. #include "pch.hpp"
  11. #include "resource.h"
  12. //#define SMALLBUFLEN 48
  13. /*******************************************************************
  14. NAME: MinimizeRNAWindow
  15. SYNOPSIS: Finds and minimizes the annoying RNA window
  16. ENTRY: pszConnectoidName - name of connectoid launched
  17. NOTES: Does a FindWindow on window class "#32770" (hard-coded
  18. dialog box class which will never change), with
  19. the title "connected to <connectoid name>" or its
  20. localized equivalent.
  21. ********************************************************************/
  22. static const TCHAR szDialogBoxClass[] = TEXT("#32770"); // hard coded dialog class name
  23. typedef struct tagWaitAndMinimizeRNAWindowArgs
  24. {
  25. LPTSTR pTitle;
  26. HINSTANCE hinst;
  27. } WnWRNAWind, FAR * LPRNAWindArgs;
  28. WnWRNAWind RNAWindArgs;
  29. HWND hwndFound = NULL;
  30. DWORD dwRASWndTitleMinLen = 0;
  31. BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam)
  32. {
  33. TCHAR szTemp[SMALLBUFLEN+2];
  34. PTSTR pszTitle;
  35. UINT uLen1, uLen2;
  36. if(!IsWindowVisible(hwnd))
  37. return TRUE;
  38. if(GetClassName(hwnd, szTemp, SMALLBUFLEN)==0)
  39. return TRUE; // continue enumerating
  40. if(lstrcmp(szTemp, szDialogBoxClass)!=0)
  41. return TRUE;
  42. if(GetWindowText(hwnd, szTemp, SMALLBUFLEN)==0)
  43. return TRUE;
  44. szTemp[SMALLBUFLEN] = 0;
  45. uLen1 = lstrlen(szTemp);
  46. Assert(dwRASWndTitleMinLen);
  47. if(uLen1 < dwRASWndTitleMinLen)
  48. return TRUE;
  49. // skip last 5 chars of title, but keep length to at least the min len
  50. uLen1 = min(dwRASWndTitleMinLen, (uLen1-5));
  51. pszTitle = (PTSTR)lparam;
  52. Assert(pszTitle);
  53. uLen2 = lstrlen(pszTitle);
  54. TraceMsg(TF_GENERAL, "Title=(%s), len=%d, Window=(%s), len=%d\r\n", pszTitle, uLen2, szTemp, uLen1);
  55. if(uLen2 < uLen1)
  56. return TRUE;
  57. if(_memicmp(pszTitle, szTemp, uLen1)!=0)
  58. return TRUE;
  59. TraceMsg(TF_GENERAL, "FOUND RNA WINDOW!!!\r\n");
  60. hwndFound = hwnd;
  61. return FALSE;
  62. }
  63. HWND MyFindRNAWindow(PTSTR pszTitle)
  64. {
  65. DWORD dwRet;
  66. hwndFound = NULL;
  67. dwRet = EnumWindows((WNDENUMPROC)(&MyEnumWindowsProc), (LPARAM)pszTitle);
  68. TraceMsg(TF_GENERAL, "EnumWindows returned %d\r\n", dwRet);
  69. return hwndFound;
  70. }
  71. DWORD WINAPI WaitAndMinimizeRNAWindow(PVOID pArgs)
  72. {
  73. // starts as a seperate thread
  74. int i;
  75. HWND hwndRNAApp;
  76. LPRNAWindArgs lpRNAArgs;
  77. lpRNAArgs = (LPRNAWindArgs)pArgs;
  78. Assert(lpRNAArgs->pTitle);
  79. for(i=0; !(hwndRNAApp=MyFindRNAWindow((PTSTR)lpRNAArgs->pTitle)) && i<100; i++)
  80. {
  81. TraceMsg(TF_GENERAL, "Waiting for RNA Window\r\n");
  82. Sleep(50);
  83. }
  84. TraceMsg(TF_GENERAL, "FindWindow (%s)(%s) returned %d\r\n", szDialogBoxClass, lpRNAArgs->pTitle, hwndRNAApp);
  85. if(hwndRNAApp)
  86. {
  87. // Hide the window
  88. // ShowWindow(hwndRNAApp,SW_HIDE);
  89. // Used to just minimize, but that wasnt enough
  90. // ChrisK reinstated minimize for ICW
  91. ShowWindow(hwndRNAApp,SW_MINIMIZE);
  92. }
  93. GlobalFree(lpRNAArgs->pTitle);
  94. // exit function and thread
  95. FreeLibraryAndExitThread(lpRNAArgs->hinst,HandleToUlong(hwndRNAApp));
  96. return (DWORD)0;
  97. }
  98. void MinimizeRNAWindow(TCHAR * pszConnectoidName, HINSTANCE hInst)
  99. {
  100. HANDLE hThread;
  101. DWORD dwThreadId;
  102. Assert(pszConnectoidName);
  103. // alloc strings for title and format
  104. TCHAR * pFmt = (TCHAR*)GlobalAlloc(GPTR, sizeof(TCHAR)*(SMALLBUFLEN+1));
  105. TCHAR * pTitle = (TCHAR*)GlobalAlloc(GPTR, sizeof(TCHAR)*((RAS_MaxEntryName + SMALLBUFLEN + 1)));
  106. if (!pFmt || !pTitle)
  107. goto error;
  108. // load the title format ("connected to <connectoid name>" from resource
  109. Assert(hInst);
  110. LoadString(hInst, IDS_CONNECTED_TO, pFmt, SMALLBUFLEN);
  111. // get length of localized title (including the %s). Assume the unmunged
  112. // part of the window title is at least "Connected to XX" long.
  113. dwRASWndTitleMinLen = lstrlen(pFmt);
  114. // build the title
  115. wsprintf(pTitle, pFmt, pszConnectoidName);
  116. RNAWindArgs.pTitle = pTitle;
  117. RNAWindArgs.hinst = LoadLibrary(TEXT("ICWDIAL.DLL"));
  118. hThread = CreateThread(0, 0, &WaitAndMinimizeRNAWindow, &RNAWindArgs, 0, &dwThreadId);
  119. Assert(hThread && dwThreadId);
  120. // dont free pTitle. The child thread needs it!
  121. GlobalFree(pFmt);
  122. // free the thread handle or the threads stack is leaked!
  123. CloseHandle(hThread);
  124. return;
  125. error:
  126. if(pFmt) GlobalFree(pFmt);
  127. if(pTitle) GlobalFree(pTitle);
  128. }
  129. // 3/28/97 ChrisK Olympus 296
  130. #if !defined (WIN16)
  131. /*******************************************************************
  132. NAME: RNAReestablishZapper
  133. SYNOPSIS: Finds and closes the annoying RNA reestablish window
  134. should it ever appear
  135. NOTES: Does a FindWindow on window class "#32770" (hard-coded
  136. dialog box class which will never change), with
  137. the title "Reestablish Connection" or it's
  138. localized equivalent.
  139. ********************************************************************/
  140. BOOL fKeepZapping = 0;
  141. void StopRNAReestablishZapper(HANDLE hthread)
  142. {
  143. if (INVALID_HANDLE_VALUE != hthread && NULL != hthread)
  144. {
  145. TraceMsg(TF_GENERAL, "ICWDIAL: Started StopRNAZapper=%d\r\n", fKeepZapping);
  146. // reset the "stop" flag
  147. fKeepZapping = 0;
  148. // wait for thread to complete & free handle
  149. WaitForSingleObject(hthread, INFINITE);
  150. CloseHandle(hthread);
  151. TraceMsg(TF_GENERAL, "ICWDIAL: Stopped StopRNAZapper=%d\r\n", fKeepZapping);
  152. }
  153. else
  154. {
  155. TraceMsg(TF_GENERAL, "ICWCONN1: StopRNAReestablishZapper called with invalid handle.\r\n");
  156. }
  157. }
  158. DWORD WINAPI RNAReestablishZapper(PVOID pTitle)
  159. {
  160. int i;
  161. HWND hwnd;
  162. TraceMsg(TF_GENERAL, "ICWDIAL: Enter RNAREstablishZapper(%s) f=%d\r\n", pTitle, fKeepZapping);
  163. // This thread continues until the fKeepZapping flag is reset
  164. while(fKeepZapping)
  165. {
  166. if(hwnd=FindWindow(szDialogBoxClass, (PTSTR)pTitle))
  167. {
  168. TraceMsg(TF_GENERAL, "ICWDIAL: Reestablish: Found Window (%s)(%s) hwnd=%x\r\n", szDialogBoxClass, pTitle, hwnd);
  169. // Post it the Cancel message
  170. PostMessage(hwnd, WM_COMMAND, IDCANCEL, 0);
  171. }
  172. Sleep(1000);
  173. }
  174. TraceMsg(TF_GENERAL, "ICWDIAL: Exit RNAREstablishZapper(%s) f=%d\r\n", pTitle, fKeepZapping);
  175. GlobalFree(pTitle);
  176. return 0;
  177. }
  178. HANDLE LaunchRNAReestablishZapper(HINSTANCE hInst)
  179. {
  180. HANDLE hThread;
  181. DWORD dwThreadId;
  182. // alloc strings for title and format
  183. TCHAR* pTitle = (TCHAR*)GlobalAlloc(LPTR, (SMALLBUFLEN+1) * sizeof(TCHAR));
  184. if (!pTitle) goto error;
  185. // load the title format "Reestablish Connection" from resource
  186. Assert(hInst);
  187. LoadString(hInst, IDS_REESTABLISH, pTitle, SMALLBUFLEN);
  188. // enable zapping
  189. fKeepZapping = TRUE;
  190. hThread = CreateThread(0, 0, &RNAReestablishZapper, pTitle, 0, &dwThreadId);
  191. Assert(hThread && dwThreadId);
  192. // dont free pTitle. The child thread needs it!
  193. return hThread;
  194. error:
  195. if(pTitle) GlobalFree(pTitle);
  196. return INVALID_HANDLE_VALUE;
  197. }
  198. #endif // !WIN16