LONG CRegKey::SetValue(LPCWSTR lpValueName,LPCWSTR strData,DWORD cbDataAsChars,DWORD dwType)
{
	if (IsUnicodeSystem())
	{
		if (dwType==REG_SZ || dwType==REG_EXPAND_SZ || dwType==REG_MULTI_SZ)
			cbDataAsChars*=2;
		return ::RegSetValueExW(m_hKey,lpValueName,0,dwType,(CONST BYTE*)strData,cbDataAsChars);
	}
	else
	{
		switch (dwType)
		{
		case REG_SZ:
		case REG_EXPAND_SZ:
			{
				char* lpDataA=alloccopyWtoA((LPCWSTR)strData,cbDataAsChars);
				LONG lRet=::RegSetValueExA(m_hKey,W2A(lpValueName),0,dwType,(CONST BYTE*)lpDataA,cbDataAsChars);
				delete[] lpDataA;
				return lRet;
			}
		case REG_MULTI_SZ:
			{
				SIZE_T i;
				for (i=0;(((LPCWSTR)strData)[i]!='\0' || ((LPCWSTR)strData)[i+1]!='\0') && i<cbDataAsChars;i++);
				char* pDataA=new char[i+2];
				ULONG_PTR ind=0;			
				while (((LPCWSTR)strData)[ind]!='\0' && ind<cbDataAsChars)
				{
					SIZE_T nlen=wcslen(((LPCWSTR)strData)+ind);
					MemCopyWtoA(pDataA+ind,((LPCWSTR)strData)+ind,nlen);
					ind+=nlen;
					pDataA[ind++]='\0';
				}
				pDataA[ind]='\0';
				LONG lRet=::RegSetValueExA(m_hKey,W2A(lpValueName),0,dwType,(CONST BYTE*)pDataA,cbDataAsChars);
				delete[] pDataA;
				return lRet;
			}
		default:
			return ::RegSetValueExA(m_hKey,W2A(lpValueName),0,dwType,(CONST BYTE*)strData,cbDataAsChars);
		}
	}
}
Exemple #2
0
BOOL CCheckFileNotificationsThread::CreateHandlesOld()
{
	ASSERT(m_pEventHandles==NULL);
	ASSERT(m_lState==sInitializing);

	FnDebugMessage("FN: creating handles (old method)");
	

	CLocateDlg* pLocateDlg=GetLocateDlg();
	ASSERT(pLocateDlg!=NULL);
	
	// Loads roods from databases so that we know what to listen
	CArrayFAP<LPWSTR> aRoots;
	const CArray<PDATABASE>& aAllDatabases=GetLocateApp()->GetDatabases();
	CArray<PDATABASE> aUsedDatabases;
	for (int i=0;i<aAllDatabases.GetSize();i++)
	{
		if (pLocateDlg->IsDatabaseUsedInSearch(aAllDatabases[i]->GetID()))
			aUsedDatabases.Add(aAllDatabases[i]);
	}
	if (aUsedDatabases.GetSize()==0)
		return FALSE;
	CDatabaseInfo::GetRootsFromDatabases(aRoots,aUsedDatabases);
	if (aRoots.GetSize()==0)
		return FALSE;
	

	// Create arrays for event handles and data structures
	//
	// The first handle in m_pEventHandles is stop event, the rest are change notification 
	// objects returned by FindFirstChangeNotification function.
	// The first pointer in m_pRoots is NULL, the rest are pointers
	// to root directory. The lists are terminated with NULL
    
	// Allocating arraysn, note that the size of the list is not 
	// necessary aRoots.GetSize()+2 if FindFirstChangeNotification returns error
	m_pEventHandles=new HANDLE[aRoots.GetSize()+1];
	ASSERT(m_pEventHandles!=NULL);
	
	m_pRoots=new LPWSTR[aRoots.GetSize()+1];
	ASSERT(m_pRoots!=NULL);
	
	
	// First handle in event handles array is stop handle and 
	// first pointer to root directory is NULL,
	// so that each element in m_pEventHandles (with index >0) 
	// corresponds to element in m_pRoots array with the same index
	m_pEventHandles[0]=m_hStopEvent;
	m_pRoots[0]=NULL;


	// Creating handles for directories in aRoots array using FindFirstChangeNotification
	m_nHandles=1;
	
	for (int i=0;i<aRoots.GetSize();i++)
	{
		if (m_lFlags&fwStopWhenPossible)
		{
			// Notify to Stop() that we are going to stop what 
			// we are doing
			SetEvent(m_hStopEvent);
			InterlockedExchange(&m_lState,sStopping);
			break;
		}


		CStringW sRoot=aRoots.GetAt(i);

		// If root of the type "X:", change it to "X:\"
		if (sRoot[1]==':' && sRoot[2]=='\0')
			sRoot << L'\\';
		
				
#ifdef _DEBUG_LOGGING
		// If logging is on, do not use change notifications for root containing log file
		LPCSTR pLogFile=GetDebugLoggingFile();
		if (pLogFile!=NULL)
		{
			// No debug logging for drive containing hfcdebug.log

			char* szPath=alloccopyWtoA(sRoot);
			MakeLower(szPath);
			BOOL bSame=strncmp(szPath,pLogFile,sRoot.GetLength())==0;
			delete[] szPath;
            if (bSame)
				continue;
		}
#endif
		

		// Create find change notification objects
		if (IsUnicodeSystem())
			m_pEventHandles[m_nHandles]=FindFirstChangeNotificationW(sRoot,TRUE,
				FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE);
		else
			m_pEventHandles[m_nHandles]=FindFirstChangeNotification(W2A(sRoot),TRUE,
				FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE);
		
		if (m_pEventHandles[m_nHandles]==INVALID_HANDLE_VALUE)
		{
			// FindFirstChangeNotification returned error, skipping this directory
			continue;
		}

		DebugOpenEvent(m_pEventHandles[m_nHandles]);


		sRoot.MakeLower();
		sRoot.FreeExtra();
		m_pRoots[m_nHandles]=sRoot.GiveBuffer();
		m_nHandles++;

	}


	

	FnDebugMessage("FN handles created");
	return TRUE;
}