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.

275 lines
9.2 KiB

  1. /*++
  2. Microsoft Windows
  3. Copyright (C) Microsoft Corporation, 1981 - 1998
  4. Module Name:
  5. irftp.cpp
  6. Abstract:
  7. Author:
  8. Rahul Thombre (RahulTh) 4/30/1998
  9. Revision History:
  10. 4/30/1998 RahulTh
  11. Created this module.
  12. --*/
  13. // irftp.cpp : Defines the class behaviors for the application.
  14. //
  15. #include "precomp.hxx"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. BOOL LoadGlobalStrings();
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CIrftpApp
  24. BEGIN_MESSAGE_MAP(CIrftpApp, CWinApp)
  25. //{{AFX_MSG_MAP(CIrftpApp)
  26. // NOTE - the ClassWizard will add and remove mapping macros here.
  27. // DO NOT EDIT what you see in these blocks of generated code!
  28. //}}AFX_MSG
  29. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CIrftpApp construction
  33. CIrftpApp::CIrftpApp()
  34. {
  35. // TODO: add construction code here,
  36. // Place all significant initialization in InitInstance
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. // The one and only CIrftpApp object
  40. CIrftpApp theApp;
  41. ////////////////////////////////////////////////////////////////////////////
  42. // The instance handle for this app.
  43. HINSTANCE g_hInstance;
  44. ////////////////////////////////////////////////////////////////////////////
  45. //RPC handle
  46. RPC_BINDING_HANDLE g_hIrRpcHandle = NULL;
  47. ///////////////////////////////////////////////////////////////////////////
  48. //the main application UI. this is now global because it might be
  49. //invoked from multiple file, especially the RPC server functions
  50. CIrftpDlg AppUI;
  51. ///////////////////////////////////////////////////////////////////////////
  52. //the controller window for the application. This is necessary to
  53. //create the illusion of parentless send progress dialog boxes.
  54. //actually it is not possible to have parentless and modeless dialog
  55. //boxes. Thus, these dialog boxes actually have the controller window
  56. //as their parent
  57. //This is necessary because the AppUI may come and go and in fact
  58. //never come up at all.
  59. CController* appController = NULL;
  60. ////////////////////////////////////////////////////////////////////////////
  61. //global variable that keeps track of the number of UI components displayed
  62. //by irftp at the moment. Note: we start with -1 because we don't want to
  63. //count the first CController window which is the main app. window.
  64. //
  65. LONG g_lUIComponentCount = -1;
  66. ////////////////////////////////////////////////////////////////////////////
  67. //global variable that keeps track of the handle to the help window (if any)
  68. //The HtmlHelp window is the only one that cannot be tracked using the
  69. //g_lUIComponentCount. So we need this var. to figure out if the help window
  70. //is still up.
  71. HWND g_hwndHelp = NULL;
  72. ////////////////////////////////////////////////////////////////////////////
  73. //global variable that keeps track of whether there is a shortcut to
  74. //the app on the desktop or not
  75. //0 implies that there is a link on the desktop and -1 implies that there
  76. //is no link on the desktop.
  77. //this value is basically an indicator of not only the presence of
  78. //the shortcut on the desktop, but also of the link in the send to
  79. //folder
  80. LONG g_lLinkOnDesktop = -1;
  81. //the path of the desktop folder.
  82. TCHAR g_lpszDesktopFolder[MAX_PATH];
  83. //the path to the send to folder;
  84. TCHAR g_lpszSendToFolder[MAX_PATH];
  85. ////////////////////////////////////////////////////////////////////////////
  86. //the list of devices in range
  87. CDeviceList g_deviceList;
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CIrftpApp initialization
  90. BOOL CIrftpApp::InitInstance()
  91. {
  92. DWORD Status;
  93. error_status_t err;
  94. CError error;
  95. BOOL bSetForeground = FALSE;
  96. int i = 0;
  97. HWND hwndApp = NULL;
  98. HANDLE hMutex = NULL;
  99. BOOL fFirstInstance = FALSE;
  100. AfxEnableControlContainer();
  101. //set the global instance handle
  102. g_hInstance = AfxGetInstanceHandle();
  103. CCommandLine cLine;
  104. ParseCommandLine (cLine);
  105. if(cLine.m_fInvalidParams) //if invalid command line parameters, terminate.
  106. {
  107. error.ShowMessage (IDS_INVALID_PARAMETERS);
  108. return FALSE; //exit the app., first instance or not.
  109. }
  110. //check if another instance is already running and act accordingly.
  111. hMutex = CreateMutex (NULL, FALSE, SINGLE_INST_MUTEX);
  112. Status = GetLastError();
  113. if (hMutex)
  114. {
  115. fFirstInstance = (ERROR_ALREADY_EXISTS != Status);
  116. }
  117. else
  118. {
  119. //we could not create the mutex, so must fail.
  120. return FALSE;
  121. }
  122. g_hIrRpcHandle = GetRpcHandle();
  123. //
  124. // Load strings.
  125. //
  126. if (FALSE == LoadGlobalStrings())
  127. {
  128. return FALSE;
  129. }
  130. if (!fFirstInstance)
  131. {
  132. hwndApp = GetPrimaryAppWindow();
  133. //note: it is important to successfully set the the first instance of the app. as the foreground window.
  134. //since the first instance has already started, it is very unlikely that it will be the foreground process
  135. //therefore it will be unable to set itself as the foreground process and therefore any calls to SetActiveWindow
  136. //etc. in that instance will not cause any changes in the Z-order or input focus. Therefore, this instance needs
  137. //to make the first instance the foreground process so that any dialogs etc. put up by the windows do not show
  138. //up obscured by other apps. or without focus. The current instance is able to set the first instance as the
  139. //foreground process because this instance is either itself the foreground process or is started by the current
  140. //foreground process.
  141. if (hwndApp && (!cLine.m_fHideApp))
  142. {
  143. bSetForeground = ::SetForegroundWindow (hwndApp);
  144. }
  145. if (cLine.m_fFilesProvided)
  146. InitiateFileTransfer (g_hIrRpcHandle, cLine.m_iListLen, cLine.m_lpszFilesList);
  147. else if (cLine.m_fShowSettings)
  148. DisplaySettings(g_hIrRpcHandle);
  149. else if (!cLine.m_fHideApp)
  150. PopupUI (g_hIrRpcHandle);
  151. //do nothing otherwise.
  152. //for some reason, SetForegroundWindow does not succeed if the window which we are trying to put in the
  153. //foreground does not have any visible windows. So if a user transfers some files and then dismisses the
  154. //wireless link dialog, then at this point, the hidden parent window is not the foreground window. So, we
  155. //try for 10 seconds to get the window in the foreground. It is okay to spin here because other than getting
  156. //the window to the top of the Z-order everything else has already been done. Here, we just want to give the
  157. //shell enough time to put up the common file open dialog. Note that we stop spinning the moment we succeed
  158. //in setting the first instance as the foreground window.
  159. if (!bSetForeground && hwndApp && (!cLine.m_fHideApp))
  160. {
  161. i = 0;
  162. do
  163. {
  164. if (::SetForegroundWindow (hwndApp))
  165. break;
  166. else
  167. Sleep (100);
  168. } while ( i++ < 100 );
  169. }
  170. CloseHandle (hMutex);
  171. return FALSE; //exit the app. rather than starting the message pump
  172. }
  173. // Standard initialization
  174. // If you are not using these features and wish to reduce the size
  175. // of your final executable, you should remove from the following
  176. // the specific initialization routines you do not need.
  177. #ifdef _AFXDLL
  178. Enable3dControls(); // Call this when using MFC in a shared DLL
  179. #else
  180. Enable3dControlsStatic(); // Call this when linking to MFC statically
  181. #endif
  182. //if we reach here, it means that this is the first instance of the app.
  183. m_pMainWnd = appController = new CController (cLine.m_fHideApp);
  184. if (!appController)
  185. {
  186. return FALSE;
  187. }
  188. appController->ShowWindow(SW_HIDE);
  189. appController->SetWindowText (MAIN_WINDOW_TITLE);
  190. g_lpszDesktopFolder[0] = '\0'; //precautionary measures
  191. g_lpszSendToFolder [0] = '\0';
  192. if(!InitRPCServer())
  193. {
  194. return FALSE; //exit the app. if the RPC server cannot be started
  195. }
  196. if (cLine.m_fFilesProvided)
  197. _InitiateFileTransfer(NULL, cLine.m_iListLen, cLine.m_lpszFilesList);
  198. else if (cLine.m_fShowSettings)
  199. _DisplaySettings(NULL);
  200. else if (!cLine.m_fHideApp)
  201. _PopupUI(NULL);
  202. //else do nothing, since the app is supposed to be hidden
  203. return TRUE; //start the message pump. RPC server is already running
  204. }
  205. GLOBAL_STRINGS g_Strings;
  206. BOOL
  207. LoadGlobalStrings()
  208. {
  209. #define LOAD_STRING(id, str) \
  210. if (0 == LoadString( g_hInstance, id, g_Strings.str, sizeof(g_Strings.str)/sizeof(wchar_t))) \
  211. { \
  212. return FALSE; \
  213. }
  214. LOAD_STRING( IDS_CLOSE, Close )
  215. LOAD_STRING( IDS_NODESC_ERROR, ErrorNoDescription )
  216. LOAD_STRING( IDS_COMPLETED, CompletedSuccess )
  217. LOAD_STRING( IDS_RECV_ERROR, ReceiveError )
  218. LOAD_STRING( IDS_CONNECTING, Connecting )
  219. LOAD_STRING( IDS_RECV_CANCELLED, RecvCancelled )
  220. return TRUE;
  221. }