connectivity/com.nokia.tcf/native/TCFNative/TCFClient/ClientManager.cpp
changeset 1481 c7f22cc57d44
parent 914 46f36a149510
child 1991 1c752f14a126
equal deleted inserted replaced
1475:fb0e02cb252b 1481:c7f22cc57d44
    21 #include "stdafx.h"
    21 #include "stdafx.h"
    22 #include "ClientManager.h"
    22 #include "ClientManager.h"
    23 #include "TCErrorConstants.h"
    23 #include "TCErrorConstants.h"
    24 #include "resource.h"
    24 #include "resource.h"
    25 #include <stdio.h>
    25 #include <stdio.h>
       
    26 #ifdef _PSAPI_PRESENT
       
    27 	#include <psapi.h>
       
    28 #endif
    26 
    29 
    27 #ifdef _DEBUG
    30 #ifdef _DEBUG
    28 extern BOOL gDoLogging;
    31 extern BOOL gDoLogging;
    29 extern char TCDebugMsg[100];
    32 extern char TCDebugMsg[100];
    30 #define TCDEBUGOPEN() if (gDoLogging) m_DebugLog->WaitForAccess();
    33 #define TCDEBUGOPEN() if (gDoLogging) m_DebugLog->WaitForAccess();
   199 	{
   202 	{
   200 		delete m_DebugLog;
   203 		delete m_DebugLog;
   201 		m_DebugLog = NULL;
   204 		m_DebugLog = NULL;
   202 	}
   205 	}
   203 }
   206 }
       
   207 
       
   208 #ifdef _PSAPI_PRESENT
       
   209 void
       
   210 CClientManager::FindOrCreateRunningServer()
       
   211 {
       
   212 	TCDEBUGOPEN();
       
   213 	TCDEBUGLOGS("CClientManager::FindOrCreateRunningServer: searching for existing TCFServer.exe\n");
       
   214 
       
   215 	// Directory where TCFServer.exe lives
       
   216 	char exeDirectory[MAX_DLLPATHNAME] = {0};
       
   217 
       
   218 	BOOL foundRunningServer = FALSE;
       
   219 
       
   220 	// Get the list of process identifiers.
       
   221 
       
   222 	DWORD aProcesses[1024], cbNeeded, cProcesses;
       
   223 	unsigned int i;
       
   224 
       
   225 	if ( EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
       
   226 	{
       
   227 		// Calculate how many process identifiers were returned.
       
   228 
       
   229 		cProcesses = cbNeeded / sizeof(DWORD);
       
   230 
       
   231 		// Find existing TCFServer
       
   232 		for ( i = 0; i < cProcesses; i++ )
       
   233 		{
       
   234 			if( aProcesses[i] != 0 )
       
   235 			{
       
   236 				TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
       
   237 
       
   238 				// Get a handle to the process.
       
   239 				HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
       
   240 											   PROCESS_VM_READ,
       
   241 											   FALSE, aProcesses[i] );
       
   242 
       
   243 				// Get the process name.
       
   244 				if (NULL != hProcess )
       
   245 				{
       
   246 					HMODULE hMod;
       
   247 					DWORD cbNeeded;
       
   248 
       
   249 					if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
       
   250 						 &cbNeeded) )
       
   251 					{
       
   252 						if (GetModuleBaseNameA( hProcess, hMod, szProcessName,
       
   253 										   sizeof(szProcessName)/sizeof(char) ))
       
   254 						{
       
   255 							if (strcmp(SERVER_PROCESS_NAME, szProcessName) == 0)
       
   256 							{
       
   257 								// get the full path
       
   258 								GetModuleFileNameExA(hProcess, hMod, exeDirectory,
       
   259 											sizeof(exeDirectory)/sizeof(char) );
       
   260 
       
   261 								TCDEBUGLOGA1("Found TCFServer at %s\n", exeDirectory);
       
   262 								CloseHandle( hProcess );
       
   263 								foundRunningServer = TRUE;
       
   264 								break;
       
   265 							}
       
   266 						}
       
   267 					}
       
   268 				}
       
   269 
       
   270 				CloseHandle( hProcess );
       
   271 			}
       
   272 		}
       
   273 	}
       
   274 
       
   275 	if (!foundRunningServer)
       
   276 	{
       
   277 		// start the one next to the DLL
       
   278 		strncpy(exeDirectory, m_DllLocation, strlen(m_DllLocation) + 1);
       
   279 		size_t len = strlen(exeDirectory);
       
   280 		// remove file
       
   281 		for (int i = len-1; i > 0; i--)
       
   282 		{
       
   283 			if (exeDirectory[i] == PATH_DELIMITER)
       
   284 				break;
       
   285 		}
       
   286 		exeDirectory[i] = NULL;
       
   287 	}
       
   288 
       
   289 	m_ServerExeFile = new char[MAX_DLLPATHNAME];
       
   290 	sprintf(m_ServerExeFile, "\"%s%c%s\"", exeDirectory, PATH_DELIMITER, SERVER_PROCESS_NAME);
       
   291 
       
   292 	m_ServerLockFile = new char[MAX_DLLPATHNAME];
       
   293 	sprintf(m_ServerLockFile, "%s%c%s", exeDirectory, PATH_DELIMITER, SERVER_LOCKFILE_NAME);
       
   294 
       
   295 	TCDEBUGCLOSE();
       
   296 
       
   297 }
       
   298 #endif
       
   299 
   204 CErrorMonitor*
   300 CErrorMonitor*
   205 CClientManager::FindErrorMonitor(long inClientId)
   301 CClientManager::FindErrorMonitor(long inClientId)
   206 {
   302 {
   207 	CErrorMonitor* errorMonitor = NULL;
   303 	CErrorMonitor* errorMonitor = NULL;
   208 	ErrorMonitorList::iterator iter;
   304 	ErrorMonitorList::iterator iter;