Ejemplo n.º 1
0
BOOL CAutoReg::WriteReg( LPCTSTR pName,  LPREGENTRY pReg, LPCTSTR pComputer )
{_STTEX();
	HKEY hRoot;
	HKEY hKey;
	BOOL bRet = TRUE;
	char szKey[ MAX_PATH ];
	BOOL bRemote = FALSE;

	// Generate key name
	szKey[ 0 ] = 0x0;
	strcpy( szKey, pReg->szKey );
	if ( pName != NULL && pName[ 0 ] != 0x0 )
	{
		if ( szKey[ 0 ] != 0x0 ) strcat( szKey, "\\" );
		strcat( szKey, pName );
	} // end if


	// Connect to remote computer if needed
	if ( pReg->szComputer[ 0 ] != 0x0 ) 
	{
		if ( RegConnectRegistry(	pReg->szComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return FALSE;		
		bRemote = TRUE;
	} else if ( pComputer != NULL && pComputer[ 0 ] != 0x0 )
	{
		if ( RegConnectRegistry(	(char*)pComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return FALSE;		
		bRemote = TRUE;
	} // end else if
	// Must want the local machine
	else hRoot = pReg->hKey;

	// Open The Key
	if( RegCreateKey( hRoot, szKey, &hKey ) != ERROR_SUCCESS )
	{
		// Close the remote key if open
		if ( bRemote ) RegCloseKey( hRoot );
		return FALSE;
	} // end if

	// Set The Key Value
	if( RegSetValueEx(	hKey, pReg->szName, 0, pReg->dwType, 
					(LPBYTE) pReg->pvoidData, pReg->dwLength ) 
					!= ERROR_SUCCESS )
		bRet = FALSE;

	RegCloseKey( hKey );

	// Close the remote key if open
	if ( bRemote ) RegCloseKey( hRoot );
	return bRet;	

}
Ejemplo n.º 2
0
HKEY GetRegistryLocalKey(const char* pszComputerName)
{
	HKEY hKey = NULL;
	LONG lRet = 0;
	if(strcmp(pszComputerName,"") != 0)//Local machine
			lRet = RegConnectRegistry(pszComputerName, HKEY_LOCAL_MACHINE, &hKey);
	else
		lRet = RegConnectRegistry(NULL, HKEY_LOCAL_MACHINE, &hKey);

	return hKey;

}
Ejemplo n.º 3
0
HKEY GetRegistryPerfKey(const char* pszComputerName)
{
	HKEY hKey = NULL;
	LONG lRet = 0;

	if(strcmp(pszComputerName,"") != 0){
			lRet = RegConnectRegistry(pszComputerName, HKEY_PERFORMANCE_DATA, &hKey);
	}
	else

		lRet = RegConnectRegistry(NULL, HKEY_PERFORMANCE_DATA, &hKey);

	return hKey;
}
Ejemplo n.º 4
0
BOOL CAutoReg::DeleteEntry ( LPCTSTR pName, LPREGENTRY pReg, LPCTSTR pComputer )
{_STTEX();
	BOOL	bRet = FALSE;
	HKEY	hKey, hRoot;
	char	szKey[ MAX_PATH ];
	BOOL	bRemote = FALSE;
	DWORD	access = 0;

	// Sanity Check
	if ( pReg == NULL ) return FALSE;
	if ( pName == NULL && pReg->szName[ 0 ] == 0x0 ) return FALSE;

	// Generate key name
	strcpy( szKey, pReg->szKey );

	// Connect to remote computer if needed
	if ( pReg->szComputer[ 0 ] != 0x0 ) 
	{
		if ( RegConnectRegistry(	pReg->szComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return 0;		
		bRemote = TRUE;
	} else if ( pComputer != NULL && pComputer[ 0 ] != 0x0 )
	{
		if ( RegConnectRegistry(	(char*)pComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return 0;		
		bRemote = TRUE;
	} // end else if

	// Must want the local machine
	else hRoot = pReg->hKey;

	// Open The Key
	if( RegOpenKeyEx(	hRoot, szKey, 0, KEY_ALL_ACCESS, &hKey ) != ERROR_SUCCESS )
	{
		// Close the remote key if open
		if ( bRemote ) RegCloseKey( hRoot );
		return FALSE;
	} // end if

	// Attempt to delete the key
	if( RegDeleteKey( hKey, ( pName != NULL ) ? pName : pReg->szName ) == ERROR_SUCCESS ) bRet = TRUE;

	// Close the keys
	RegCloseKey( hKey );
	if ( bRemote ) RegCloseKey( hRoot );

	return bRet;
}
Ejemplo n.º 5
0
/*
 * Opens a remote registry key and returns the associated HKEY to the caller if the
 * operation succeeds.
 *
 * TLVs:
 *
 * req: TLV_TYPE_ROOT_KEY      - The root key
 * req: TLV_TYPE_TARGET_HOST   - The target machine name
 */
DWORD request_registry_open_remote_key(Remote *remote, Packet *packet)
{
	Packet *response = packet_create_response(packet);
	LPCTSTR targetHost = NULL;
	HKEY rootKey = NULL, resKey;
	DWORD result;

	targetHost = packet_get_tlv_value_string(packet, TLV_TYPE_TARGET_HOST);
	rootKey    = (HKEY)packet_get_tlv_value_qword(packet, TLV_TYPE_ROOT_KEY);

	// Validate the parameters and then attempt to create the key
	if ((!rootKey) || (!targetHost))
		result = ERROR_INVALID_PARAMETER;
	else
	{
		result = RegConnectRegistry(targetHost, rootKey, &resKey);
	}

	// Add the HKEY if we succeeded, but always return a result
	if (result == ERROR_SUCCESS)
	{
		packet_add_tlv_qword(response, TLV_TYPE_HKEY, (QWORD)resKey);
	}

	packet_add_tlv_uint(response, TLV_TYPE_RESULT, result);

	packet_transmit(remote, response, NULL);

	return ERROR_SUCCESS;
}
Ejemplo n.º 6
0
// Connect to a remote computer's registry
// Note: WinNT won't allow you to connect to these hives via RegConnectRegistry
bool RegKey::ConnectRemote(HKEY hKeyToOpen, LPCTSTR pszComputerName)
{
	if (pszComputerName == NULL)
	{
		iLastErrorCode_ = ERROR_BAD_ARGUMENTS;
		return false;
	}

	LONG lRetValue = ERROR_SUCCESS;

	// Try to connect using the remote key
	lRetValue = RegConnectRegistry((TCHAR *)pszComputerName, hKeyToOpen, &hBaseKey_);
	if (lRetValue == ERROR_SUCCESS)
	{
		bRemote_ = true;
		pszComputerName_ = new TCHAR[MAX_COMPUTERNAME_LENGTH + 1];
		memset(pszComputerName_, 0, (MAX_COMPUTERNAME_LENGTH + 1) * sizeof(TCHAR));
		_tcsncpy_s(pszComputerName_, MAX_COMPUTERNAME_LENGTH + 1, pszComputerName, MAX_COMPUTERNAME_LENGTH);
	}
	else
	{
		bRemote_ = false;
		pszComputerName_ = NULL;
		iLastErrorCode_ = GetLastError();

		return false;
	}

	iLastErrorCode_ = ERROR_SUCCESS;
	return true;
}
Ejemplo n.º 7
0
HRESULT RemoteRegistryClass::SetRemoteRegLocation (LPCTSTR		pszServer,
																	LPVOID		hKey,
																	LPCTSTR		pszKeyPath,
																	const DWORD	dwOptions,
																	const BOOL	fCreateIt)
{
	if (NULL == hKey)
		return ERROR_BAD_ARGUMENTS;
	if (NULL != m_hRemKey)
		return ERROR_BAD_ENVIRONMENT;

	HRESULT hr=RegConnectRegistry((LPTSTR) pszServer, (HKEY) hKey, (PHKEY) &m_hRemKey);
	if (ERROR_SUCCESS == hr)
	{
		hr = RegistryClass::SetRegLocation(m_hRemKey, pszKeyPath, dwOptions, fCreateIt);
		if (hr != ERROR_SUCCESS)
		{
			RegCloseKey((HKEY) m_hRemKey);
			m_hRemKey = NULL;
		}
		else
			m_pszServer = pszServer;
	}
	else
		m_hRemKey = NULL;

	return hr;
}
Ejemplo n.º 8
0
BOOL CAutoReg::DeleteSubKeys ( LPCTSTR pName, LPREGENTRY pReg, LPCTSTR pComputer )
{_STTEX();
	BOOL	bRet = FALSE;
	HKEY	hRoot;
	char	szKey[ MAX_PATH ];
	BOOL	bRemote = FALSE;
	DWORD	access = 0;

	// Sanity Check
	if ( pReg == NULL ) return FALSE;

	// Generate key name
	szKey[ 0 ] = 0x0;
	strcpy( szKey, pReg->szKey );
	if ( pName != NULL && pName[ 0 ] != 0x0 )
	{
		if ( szKey[ 0 ] != 0x0 ) strcat( szKey, "\\" );
		strcat( szKey, pName );
	} // end if

	// Connect to remote computer if needed
	if ( pReg->szComputer[ 0 ] != 0x0 ) 
	{
		if ( RegConnectRegistry(	pReg->szComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return 0;		
		bRemote = TRUE;
	} else if ( pComputer != NULL && pComputer[ 0 ] != 0x0 )
	{
		if ( RegConnectRegistry(	(char*)pComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return 0;		
		bRemote = TRUE;
	} // end else if

	// Must want the local machine
	else hRoot = pReg->hKey;

	// Attempt to delete the Sub-keys
	if( DeleteSubKeys( hRoot, szKey ) == ERROR_SUCCESS ) bRet = TRUE;

	// Close the remote key if needed
	if ( bRemote ) RegCloseKey( hRoot );

	return bRet;
}
Ejemplo n.º 9
0
/* connect to remote computer registry */
HKEY
RegConnect (HKEY hKey, LPCTSTR pszRemote)
{
  HKEY hSubKey;
  if (RegConnectRegistry (pszRemote, hKey, &hSubKey) == ERROR_SUCCESS)
    {
      return hSubKey;
    }
  return NULL;
}
Ejemplo n.º 10
0
HKEY ConnectRegistry(const char* pszComputerName,int iMachineType,int iKeyType)
{
	HKEY hKey = NULL;
	LONG lRet = 0;
	if(iKeyType == 0)//HKEY_LOCAL_MACHINE
	{
		if(iMachineType == 0)	  //Remote machine
			lRet = RegConnectRegistry(pszComputerName, HKEY_LOCAL_MACHINE, &hKey);
		else    //Local machine
			lRet = RegConnectRegistry(NULL, HKEY_LOCAL_MACHINE, &hKey);
	}
	if(iKeyType == 1) //HKEY_PERFORMANC_DATA
	{
		if(iMachineType == 0)	//Remote machine
			lRet = RegConnectRegistry(pszComputerName, HKEY_PERFORMANCE_DATA, &hKey);
		else //Local machine
			lRet = RegConnectRegistry(NULL, HKEY_PERFORMANCE_DATA, &hKey);
	}
	return hKey;

}
Ejemplo n.º 11
0
// ****************************************************************************
// returns : 0 on local host
//           1 on remote host
//           -1 on error
static LONG LoadPerfObjects (Perf& perfStruct)
{
  DWORD BufferSize = TOTALBYTES;
  static char* globalQuery = "Global";
  HKEY hKey = HKEY_PERFORMANCE_DATA;
  bool ret  = 0;
  LONG status = ERROR_SUCCESS;

  // Allocate the buffer for the performance data.

  if (!perfStruct.perfQuery || !*perfStruct.perfQuery)
    perfStruct.perfQuery = globalQuery;

  if (perfStruct.PerfData != NULL)
    free (perfStruct.PerfData);

  perfStruct.PerfData = NULL;
  perfStruct.PerfData = (PPERF_DATA_BLOCK) malloc (BufferSize);

  if ((perfStruct.hostName) && (strcmp (perfStruct.hostName, LOCALHOST) != 0)) {

    status = RegConnectRegistry ((LPTSTR)perfStruct.hostName,
				 HKEY_PERFORMANCE_DATA,
				 &hKey);

    if (status != ERROR_SUCCESS)
      return status;
  }
  else
    hKey = HKEY_PERFORMANCE_DATA;
  
  while (RegQueryValueEx (hKey,
			  perfStruct.perfQuery,
			  NULL,
			  NULL,
			  (LPBYTE) perfStruct.PerfData,
			  &BufferSize ) == ERROR_MORE_DATA ) {

    // Get a buffer that is big enough.
    BufferSize += BYTEINCREMENT;
    perfStruct.PerfData = (PPERF_DATA_BLOCK) realloc (perfStruct.PerfData, BufferSize);
  }

  return ret;

} // LoadPerfObjects ()
Ejemplo n.º 12
0
/*
 * Split a registry key name into its root name (setting rkey)
 * returning a pointer to the rest.
 * If connection to a remote computer has been specified rkey is
 * set to that machine's handle.
 * In that case the returned rkey must be passed to a call to RegCloseKey
 */
static char *
split_name(char *name, HKEY *rkey, char *rname)
{
	char *s, *p;
	LONG ret;

	for (s = name, p = rname; *s && *s != '\\'; s++, p++)
		*p = *s;
	*p = '\0';
	if (*s == '\\')
		s++;
	if (nameval(handles, sizeof(handles) / sizeof(struct s_nameval), rname, (DWORD *)rkey)) {
		if (remote) {
			/* Redirect to the remote machine */
			if ((ret = RegConnectRegistry(remote, *rkey, rkey)) != ERROR_SUCCESS)
				wperror(remote, ret);
		}
		return (*s ? s : NULL);
	} else {
		fprintf(stderr, "Unknown root handle name [%s]\n", rname);
		exit(1);
	}
}
Ejemplo n.º 13
0
// Return Values
//
// 0 = No Access or invalid key data
// 1 = KEY_QUERY_VALUE
// 2 = KEY_READ
// 3 = KEY_WRITE
// 4 = KEY_QUERY_VALUE | KEY_WRITE
// 5 = KEY_READ | KEY_WRITE
//
DWORD CAutoReg::TestAccess( LPCTSTR pName, LPREGENTRY pReg, LPCTSTR pComputer )
{_STTEX();
	HKEY	hKey, hRoot;
	char	szKey[ MAX_PATH ];
	BOOL	bRemote = FALSE;
	DWORD	access = 0;

	if ( pReg == NULL ) return 0;

	// Generate key name
	szKey[ 0 ] = 0x0;
	strcpy( szKey, pReg->szKey );
	if ( pName != NULL && pName[ 0 ] != 0x0 )
	{
		if ( szKey[ 0 ] != 0x0 ) strcat( szKey, "\\" );
		strcat( szKey, pName );
	} // end if

	// Connect to remote computer if needed
	if ( pReg->szComputer[ 0 ] != 0x0 ) 
	{
		if ( RegConnectRegistry(	pReg->szComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return 0;		
		bRemote = TRUE;
	} else if ( pComputer != NULL && pComputer[ 0 ] != 0x0 )
	{
		if ( RegConnectRegistry(	(char*)pComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return 0;		
		bRemote = TRUE;
	} // end else if
	// Must want the local machine
	else hRoot = pReg->hKey;

	// Query access
	if( RegOpenKeyEx(	hRoot, szKey, 0,
						KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS )
	{
		RegCloseKey( hKey );
		// We have KEY_QUERY_VALUE access
		access = 1;
	} // end if

	// Read access
	if( RegOpenKeyEx(	hRoot, szKey, 0,
						KEY_READ, &hKey ) == ERROR_SUCCESS )
	{
		RegCloseKey( hKey );
		// We have KEY_READ access
		access = 2;
	} // end if

	// Write access
	if( RegOpenKeyEx(	hRoot, szKey, 0,
						KEY_WRITE, &hKey ) == ERROR_SUCCESS )
	{
		RegCloseKey( hKey );
		// We have KEY_WRITE access
		access += 3;
	} // end if

	if ( bRemote ) RegCloseKey( hRoot );

	// Tell the caller how it turned out
	return access;
}
Ejemplo n.º 14
0
LONG  connectlkey(   __in_opt  LPCTSTR lpMachineName,
  __in      HKEY hKey,
  __out     PHKEY phkResult)
{
	return RegConnectRegistry(lpMachineName, hKey,phkResult);
}
Ejemplo n.º 15
0
BOOL CAutoReg::ReadReg(LPCTSTR pName, LPREGENTRY pReg, LPCTSTR pComputer)
{_STTEX();
	HKEY	hKey, hRoot;
	char	szKey[ MAX_PATH ];
	BOOL	bRet = TRUE;
	DWORD	dwType;
	DWORD	dwLength;
	BOOL	bRemote = FALSE;

	if ( pReg == NULL ) return FALSE;

	// Generate key name
	szKey[ 0 ] = 0x0;
	strcpy( szKey, pReg->szKey );
	if ( pName != NULL && pName[ 0 ] != 0x0 )
	{
		if ( szKey[ 0 ] != 0x0 ) strcat( szKey, "\\" );
		strcat( szKey, pName );
	} // end if

	// Connect to remote computer if needed
	if ( pReg->szComputer[ 0 ] != 0x0 ) 
	{
		if ( RegConnectRegistry(	pReg->szComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return FALSE;		
		bRemote = TRUE;
	} else if ( pComputer != NULL && pComputer[ 0 ] != 0x0 )
	{
		if ( RegConnectRegistry(	(char*)pComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return FALSE;		
		bRemote = TRUE;
	} // end else if
	// Must want the local machine
	else hRoot = pReg->hKey;

	// Open The Key
	if( RegOpenKeyEx(	hRoot, szKey, 0,
						KEY_QUERY_VALUE, &hKey ) != ERROR_SUCCESS )
	{
		// Close the remote key if open
		if ( bRemote ) RegCloseKey( hRoot );
		Default( pReg );
		return FALSE;
	} // end if
	
	// Record the length and type
	dwLength	= pReg->dwLength;
	dwType		= pReg->dwType;

	// Read The Data
	if( RegQueryValueEx(	hKey, pReg->szName, 0, &dwType, 
							(LPBYTE) pReg->pvoidData, &dwLength ) != ERROR_SUCCESS )
		bRet = FALSE;

	// Make sure it is the correct type
	// And The Correct Length
	if( !bRet || pReg->dwType != dwType )
	{
		bRet = FALSE;
		Default( pReg );
	} // end if

	// Close The Key
	RegCloseKey( hKey );	

	// Close the remote key if open
	if ( bRemote ) RegCloseKey( hRoot );

	return bRet;
}
Ejemplo n.º 16
0
main( int argc, char* argv[] )
{
    DWORD   i;
    HKEY    RemoteUsers;
    HKEY    RemoteLocalMachine;
    LONG    Status;

    if( argc <= 1 ) {
        printf( "\n******* Examining HKEY_LOCAL_MACHINE on local machine\n\n" );
        ExamineKey( HKEY_LOCAL_MACHINE,
                    L"",
                    L"",
                    L"HKEY_LOCAL_MACHINE" );

        printf( "\n******* Examining HKEY_USERS on local machine\n\n" );
        ExamineKey( HKEY_USERS,
                    L"",
                    L"",
                    L"HKEY_USERS" );

        printf( "\n******* Examining HKEY_CLASSES_ROOT on local machine\n\n" );
        ExamineKey( HKEY_CLASSES_ROOT,
                    L"",
                    L"",
                    L"HKEY_CLASSES_ROOT" );

        printf( "\n******* Examining HKEY_CURRENT_USER on local machine\n\n" );
        ExamineKey( HKEY_CURRENT_USER,
                    L"",
                    L"",
                    L"HKEY_CURRENT_USER" );
    } else {
        for( i = 1; i < argc; i++ ) {
            //
            // printf( "Machine name = %s \n", argv[ i ] );
            //

            Status = RegConnectRegistry( argv[ i ],
                                         HKEY_LOCAL_MACHINE,
                                         &RemoteLocalMachine );

            if( Status != ERROR_SUCCESS ) {
                printf( MSG_ERROR_REG_CONNECT_REGISTRY, argv[i], Status );
                continue;
            }

            Status = RegConnectRegistry( argv[ i ],
                                         HKEY_USERS,
                                         &RemoteUsers );

            if( Status != ERROR_SUCCESS ) {
                RegCloseKey( RemoteLocalMachine );
                printf( MSG_ERROR_REG_CONNECT_REGISTRY, argv[i], Status );
                continue;
            }

            printf( "\n******* Examining HKEY_LOCAL_MACHINE on %s \n\n", argv[i] );
            ExamineKey( RemoteLocalMachine,
                        L"",
                        L"",
                        L"HKEY_LOCAL_MACHINE" );

            printf( "\n******* Examining HKEY_USERS on %s \n\n", argv[i] );
            ExamineKey( RemoteUsers,
                        L"",
                        L"",
                        L"HKEY_USERS" );

            RegCloseKey( RemoteLocalMachine );
            RegCloseKey( RemoteUsers );
        }
    }
}
Ejemplo n.º 17
0
LONG CRegistryKey::OpenSubkey(REGSAM samDesired, const TCHAR *pszSubkeyName, HKEY &rhKey)
{
	if (m_hKey == NULL)
  { // subkey of root key is hive root key.
    if ((_tcsicmp(pszSubkeyName,_T("HKCR")) == 0)||
				(_tcsicmp(pszSubkeyName,_T("HKEY_CLASSES_ROOT")) == 0))
    {
      rhKey = HKEY_CLASSES_ROOT;

      if (m_pszMachineName)
        return ERROR_FILE_NOT_FOUND;

      return ERROR_SUCCESS;
    }
    else if ((_tcsicmp(pszSubkeyName,_T("HKCU")) == 0)||
             (_tcsicmp(pszSubkeyName,_T("HKEY_CURRENT_USER")) == 0))
    {
      rhKey = HKEY_CURRENT_USER;

      if (m_pszMachineName)
        return ERROR_FILE_NOT_FOUND;

      return ERROR_SUCCESS;
    }
    else if ((_tcsicmp(pszSubkeyName,_T("HKLM")) == 0)||
             (_tcsicmp(pszSubkeyName,_T("HKEY_LOCAL_MACHINE")) == 0))
    {
      rhKey = HKEY_LOCAL_MACHINE;

      if (m_pszMachineName)
        return RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);

      return ERROR_SUCCESS;
    }
    else if ((_tcsicmp(pszSubkeyName,_T("HKU")) == 0)||
             (_tcsicmp(pszSubkeyName,_T("HKEY_USERS")) == 0))
    {
      rhKey = HKEY_USERS;

      if (m_pszMachineName)
        return RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);

      return ERROR_SUCCESS;
    }
    else if ((_tcsicmp(pszSubkeyName,_T("HKPD")) == 0)||
             (_tcsicmp(pszSubkeyName,_T("HKEY_PERFORMANCE_DATA")) == 0))
    {
      rhKey = HKEY_PERFORMANCE_DATA;

      if (m_pszMachineName)
        return RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);

      return ERROR_SUCCESS;
    }
    else if ((_tcsicmp(pszSubkeyName,_T("HKDD")) == 0)||
             (_tcsicmp(pszSubkeyName,_T("HKEY_DYN_DATA")) == 0))
    {
      rhKey = HKEY_DYN_DATA;

      if (m_pszMachineName)
        return RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);

      return ERROR_SUCCESS;
    }
    else if ((_tcsicmp(pszSubkeyName,_T("HKCC")) == 0)||
             (_tcsicmp(pszSubkeyName,_T("HKEY_CURRENT_CONFIG")) == 0))
    {
      rhKey = HKEY_CURRENT_CONFIG;

      if (m_pszMachineName)
      {
        TCHAR *pch = m_pszMachineName;
        while (*pch)
          pch++;
        pch--;

        ASSERT(*pch == _T('\\'));
        if (*pch != _T('\\'))
          return ERROR_INTERNAL_ERROR;

        *pch = 0;

        LONG nError = RegConnectRegistry(m_pszMachineName,rhKey,&rhKey);

        *pch = _T('\\');

        return nError;
      }

      return ERROR_SUCCESS;
    }
    else
    {
      return ERROR_FILE_NOT_FOUND;
    }
  }

	return RegOpenKeyEx(m_hKey,pszSubkeyName,0,samDesired,&rhKey);
}
Ejemplo n.º 18
0
BOOL OPCServerDlg::GetOPCServers(LPTSTR node)
{
   CListBox* pList = (CListBox*)GetDlgItem( IDC_SERVER_LIST );
   USES_CONVERSION;

   CWaitCursor wait;
   ASSERT_VALID(pList);
   pList->ResetContent();

   // New 2.0 method using component categories
   IOPCServerList* pServers=NULL;

   COSERVERINFO si;
   MULTI_QI  qi;

   si.dwReserved1 = 0;
   si.pwszName = NULL;
   if( node && node[0] != 0 )
      si.pwszName = T2OLE(node);
   si.pAuthInfo = NULL;
   si.dwReserved2 = 0;

   qi.pIID = &IID_IOPCServerList;
   qi.pItf = NULL;
   qi.hr = 0;

   HRESULT hr = CoCreateInstanceEx(CLSID_OPCServerList, NULL, CLSCTX_ALL, &si, 1, &qi);
   if (FAILED(hr) || FAILED(qi.hr))
   {
      CString msg(_T("Error connecting to OPC 2.0 Server Browser."));
      if( !m_Node.IsEmpty() )
         msg.Format(_T("Error connecting to OPC 2.0 Server Browser on %s."), (LPCTSTR)m_Node);

      if( hr == REGDB_E_CLASSNOTREG )
      {
          CString msg(_T("Please install the OPC 2.0 Components."));
          if( !m_Node.IsEmpty() )
             msg.Format(_T("Please install the OPC 2.0 Components on %s."), (LPCTSTR)m_Node);
      }

      if( FAILED(hr) )
        theDoc->ReportError(msg, hr);
      else
        theDoc->ReportError(msg, qi.hr);
   }
   else
   {
       pServers = (IOPCServerList*)qi.pItf;

       IEnumGUID *pEnumGUID=NULL;
       hr = pServers->EnumClassesOfCategories(1, &m_catid, 1, &m_catid, &pEnumGUID);
       if( SUCCEEDED(hr) )
       {
          unsigned long count;
          CLSID clsid[NEXT_COUNT];

          do
          {
             hr = pEnumGUID->Next(NEXT_COUNT, clsid, &count);
             for( ULONG index=0; index<count; index++ )
             {
                LPOLESTR pszProgID;
                LPOLESTR pszUserType;
                HRESULT hr2 = pServers->GetClassDetails(clsid[index], &pszProgID, &pszUserType);
                if (SUCCEEDED(hr2))
                {
                   OPCServerInfo* pServerInfo = new OPCServerInfo(pszProgID, pszUserType, clsid[index] );
                   m_Servers.AddTail( pServerInfo );
                   CString name;
                   name.Format( _T("%s  (%s)"),
                               (LPCTSTR)pServerInfo->m_ProgID,
                               (LPCTSTR)pServerInfo->m_Description );
                   int pos = pList->AddString( name );
                   pList->SetItemData( pos, (DWORD)pServerInfo );
                   if( m_Server == pServerInfo->m_ProgID )
                      pList->SetCurSel(pos);

                   CoTaskMemFree(pszProgID);
                   CoTaskMemFree(pszUserType);
                }
             }
          }
          while ( hr==S_OK );
          pEnumGUID->Release();
          pServers->Release();
          return TRUE;
       }
       else // hr failed
       {
          CString msg(_T("EnumClassesOfCategories failed:"));
          theDoc->ReportError(msg, hr);
       }
       pServers->Release();
   }

   // Old 1.0 method: search the registry for OPC entries
   if( m_catid != CATID_OPCDAServer10 )
       return FALSE;

   HKEY hk = HKEY_CLASSES_ROOT;
   if( node )
   {
      DWORD dwR = RegConnectRegistry ((LPTSTR)node, HKEY_CLASSES_ROOT, &hk);
      if( dwR != ERROR_SUCCESS )
         return FALSE;
   }

   TCHAR key[MAX_KEYLEN];
   DWORD size = MAX_KEYLEN;
   FILETIME ft;
   for( int index=0; RegEnumKeyEx(hk, index, key, &size, 0, NULL, NULL, &ft)==ERROR_SUCCESS; index++)
   {
      HKEY hProgID=0;
      if(RegOpenKeyEx(hk, key, 0, KEY_QUERY_VALUE, &hProgID )==ERROR_SUCCESS )
      {
         // Find OPC key
         HKEY hOPC=0;
         if(RegOpenKeyEx(hProgID, _T("OPC"), 0, KEY_QUERY_VALUE, &hOPC)==ERROR_SUCCESS )
         {
            // Found OPC key, save this information
            CLSID clsid;
            DWORD type=REG_SZ;
            HKEY hCLSID=0;
            if(RegOpenKeyEx(hProgID, _T("CLSID"), 0, KEY_QUERY_VALUE, &hCLSID)==ERROR_SUCCESS )
            {
                TCHAR clsidString[MAX_KEYLEN];
                size=MAX_KEYLEN;
                if(RegQueryValueEx(hCLSID, key, 0, &type, (BYTE*)clsidString, &size )==ERROR_SUCCESS )
                {
                    hr = CLSIDFromString( T2OLE(clsidString), &clsid );
                    if( FAILED(hr))
                    {
                    theDoc->ReportError( _T("CLSIDFromString: "), hr );
                    continue;
                    }
                }
            }
            
            TCHAR desc[MAX_KEYLEN];
            size=MAX_KEYLEN;
            RegQueryValueEx(hOPC, key, 0, &type, (BYTE*)desc, &size);
            OPCServerInfo* pServerInfo = new OPCServerInfo(T2OLE(key), L"", clsid );
            m_Servers.AddTail( pServerInfo );
            int pos = pList->AddString( pServerInfo->m_ProgID );
            pList->SetItemData( pos, (DWORD)pServerInfo );
            if( m_Server == key )
               pList->SetCurSel(pos);
            RegCloseKey( hOPC );
         }
         RegCloseKey( hProgID );
      }
      size = MAX_KEYLEN;
   }

   OnSelchangeServerList();

   return TRUE;
}
Ejemplo n.º 19
0
BOOL CAutoReg::GetSubKey( DWORD index, LPREGENTRY pDest, LPCTSTR pName, LPREGENTRY pReg, LPCTSTR pComputer )
{_STTEX();
	
	BOOL	bRet = FALSE;
	HKEY	hKey, hRoot;
	char	szKey[ MAX_PATH ];
	BOOL	bRemote = FALSE;
	DWORD	access = 0;
	DWORD	dwSize = sizeof( pReg->szName );

	// Sanity Check
	if ( pReg == NULL ) return FALSE;
	if ( pDest == NULL ) return FALSE;

	// Blank out the structure
	ZeroMemory( (LPVOID)pDest, sizeof( REGENTRY ) );

	// Generate key name
	szKey[ 0 ] = 0x0;
	strcpy( szKey, pReg->szKey );
	if ( pName != NULL && pName[ 0 ] != 0x0 )
	{
		if ( szKey[ 0 ] != 0x0 ) strcat( szKey, "\\" );
		strcat( szKey, pName );
	} // end if
	if ( pReg->szName[ 0 ] != 0x0 )
	{
		if ( szKey[ 0 ] != 0x0 ) strcat( szKey, "\\" );
		strcat( szKey, pReg->szName );
	} // end if

	// Connect to remote computer if needed
	if ( pReg->szComputer[ 0 ] != 0x0 ) 
	{
		strcpy( pDest->szComputer, pReg->szComputer );
		if ( RegConnectRegistry(	pReg->szComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return 0;		
		bRemote = TRUE;
	} else if ( pComputer != NULL && pComputer[ 0 ] != 0x0 )
	{
		strcpy( pDest->szComputer, pComputer );
		if ( RegConnectRegistry(	(char*)pComputer, 
									pReg->hKey, 
									&hRoot ) != ERROR_SUCCESS ) return 0;		
		bRemote = TRUE;
	} // end else if

	// Must want the local machine
	else hRoot = pReg->hKey;

	// Open The Key
	if( RegOpenKeyEx(	hRoot, szKey, 0, KEY_ALL_ACCESS, &hKey ) != ERROR_SUCCESS )
	{
		// Close the remote key if open
		if ( bRemote ) RegCloseKey( hRoot );
		return FALSE;
	} // end if

	pDest->dwUser = 0;
	pDest->hKey = pReg->hKey; 

	// Save the location of the key
	strcpy( pDest->szKey, szKey );

	FILETIME	ft;
	// Get sub-key info
	if ( RegEnumKeyEx( hKey, index, pDest->szName, &dwSize, 
							NULL, NULL, NULL, &ft ) == ERROR_SUCCESS )
	{
		bRet = TRUE;
		// Attempt to get sub-key info
		if( RegQueryValueEx(	hKey, pDest->szName, 0, &pDest->dwType, 
								(LPBYTE) pDest->pvoidData, &pDest->dwLength ) 
								== ERROR_SUCCESS ) 
		{
			SetDefault( pDest );
		} // end if
		else
		{
			pDest->dwType = REG_NONE;
			pDest->dwLength = 0;
		} // end else
		dwSize = MAX_PATH -1;
	} // end if	   

	// Close the keys
	RegCloseKey( hKey );
	if ( bRemote ) RegCloseKey( hRoot );

	return bRet;
}
Ejemplo n.º 20
0
BOOL CCertRemoveSelectDlg::RefreshLicenses()

/*++

Routine Description:

   Refresh internal license list with data from license server.

Arguments:

   None.

Return Values:

   BOOL.

--*/

{
   ResetLicenses();

   if ( ConnectServer() )
   {
      NTSTATUS    NtStatus;
      DWORD       ResumeHandle = 0L;

      int iLicense = 0;

      do
      {
         DWORD  EntriesRead;
         DWORD  TotalEntries;
         LPBYTE ReturnBuffer = NULL;
         DWORD  Level = LlsCapabilityIsSupported( m_hLls, LLS_CAPABILITY_SECURE_CERTIFICATES ) ? 1 : 0;

         BeginWaitCursor();
         NtStatus = ::LlsLicenseEnum( m_hLls,
                                      Level,
                                      &ReturnBuffer,
                                      LLS_PREFERRED_LENGTH,
                                      &EntriesRead,
                                      &TotalEntries,
                                      &ResumeHandle );
         EndWaitCursor();

         if (    ( STATUS_SUCCESS      == NtStatus )
              || ( STATUS_MORE_ENTRIES == NtStatus ) )
         {
            CLicense*            pLicense;
            PLLS_LICENSE_INFO_0  pLicenseInfo0;
            PLLS_LICENSE_INFO_1  pLicenseInfo1;

            pLicenseInfo0 = (PLLS_LICENSE_INFO_0)ReturnBuffer;
            pLicenseInfo1 = (PLLS_LICENSE_INFO_1)ReturnBuffer;

            while (EntriesRead--)
            {
               if (    ( m_strProductName.IsEmpty() || !m_strProductName.CompareNoCase( Level ? pLicenseInfo1->Product : pLicenseInfo0->Product ) )
                    && ( m_strSourceToUse.IsEmpty() || !m_strSourceToUse.CompareNoCase( Level ? pLicenseInfo1->Source  : TEXT("None")           ) ) )
               {
                  // we want to list this license

                  // have we seen this certificate yet?
                  for ( int i=0; i < m_licenseArray.GetSize(); i++ )
                  {
                     pLicense = (CLicense*) m_licenseArray[ i ];

                     VALIDATE_OBJECT( pLicense, CLicense );
   
                     if (    (    ( 1 == Level )
                               && ( pLicense->m_dwCertificateID == pLicenseInfo1->CertificateID     )
                               && ( pLicense->m_dwAllowedModes  == pLicenseInfo1->AllowedModes      )
                               && ( pLicense->m_dwMaxQuantity   == pLicenseInfo1->MaxQuantity       )
                               && ( !pLicense->m_strSource.CompareNoCase(  pLicenseInfo1->Source  ) )
                               && ( !pLicense->m_strProduct.CompareNoCase( pLicenseInfo1->Product ) )
                               && ( !memcmp( pLicense->m_adwSecrets,
                                             pLicenseInfo1->Secrets,
                                             sizeof( pLicense->m_adwSecrets ) )                     ) )
                          || (    ( 0 == Level )
                               && ( !pLicense->m_strProduct.CompareNoCase( pLicenseInfo0->Product ) ) ) )
                     {
                        // we've seen this certificate before; update the tally
                        pLicense->m_lQuantity += ( Level ? pLicenseInfo1->Quantity : pLicenseInfo0->Quantity );
                        break;
                     }     
                  }
   
                  if ( i >= m_licenseArray.GetSize() )
                  {
                     // we haven't seen this certificate yet; create a new license for it
                     if ( 1 == Level )
                     {
                        pLicense = new CLicense( pLicenseInfo1->Product,
                                                 pLicenseInfo1->Vendor,
                                                 pLicenseInfo1->Admin,
                                                 pLicenseInfo1->Date,
                                                 pLicenseInfo1->Quantity,
                                                 pLicenseInfo1->Comment,
                                                 pLicenseInfo1->AllowedModes,
                                                 pLicenseInfo1->CertificateID,
                                                 pLicenseInfo1->Source,
                                                 pLicenseInfo1->ExpirationDate,
                                                 pLicenseInfo1->MaxQuantity,
                                                 pLicenseInfo1->Secrets );

                        ::LlsFreeMemory( pLicenseInfo1->Product );
                        ::LlsFreeMemory( pLicenseInfo1->Admin   );
                        ::LlsFreeMemory( pLicenseInfo1->Comment );
                        ::LlsFreeMemory( pLicenseInfo1->Source  );
                     }
                     else
                     {
                        ASSERT( 0 == Level );

                        pLicense = new CLicense( pLicenseInfo0->Product,
                                                 TEXT( "Microsoft" ),
                                                 pLicenseInfo0->Admin,
                                                 pLicenseInfo0->Date,
                                                 pLicenseInfo0->Quantity,
                                                 pLicenseInfo0->Comment );
                     
                        ::LlsFreeMemory( pLicenseInfo0->Product );
                        ::LlsFreeMemory( pLicenseInfo0->Admin   );
                        ::LlsFreeMemory( pLicenseInfo0->Comment );
                     }

                     if ( NULL == pLicense )
                     {
                        NtStatus = ERROR_OUTOFMEMORY;
                        break;
                     }

                     m_licenseArray.Add( pLicense );
                  }
               }

               pLicenseInfo1++;
               pLicenseInfo0++;
            }

            ::LlsFreeMemory(ReturnBuffer);
         }

      } while ( STATUS_MORE_ENTRIES == NtStatus );

      theApp.SetLastLlsError( NtStatus );   // called api

      if ( STATUS_SUCCESS == NtStatus )
      {
         // add per server entries
         LPTSTR pszServerName = m_strServerName.GetBuffer(0);

         if ( NULL != pszServerName )
         {
            BeginWaitCursor();

            HKEY  hKeyLocalMachine;

            NtStatus = RegConnectRegistry( pszServerName, HKEY_LOCAL_MACHINE, &hKeyLocalMachine );

            if ( ERROR_SUCCESS != NtStatus )
            {
               theApp.SetLastError( NtStatus );
            }
            else
            {
               HKEY  hKeyLicenseInfo;

               NtStatus = RegOpenKeyEx( hKeyLocalMachine, TEXT( "SYSTEM\\CurrentControlSet\\Services\\LicenseInfo" ), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_SET_VALUE, &hKeyLicenseInfo );

               if ( ERROR_SUCCESS != NtStatus )
               {
                  theApp.SetLastError( NtStatus );
               }
               else
               {
                  NTSTATUS ntEnum;
                  BOOL     bFoundKey = FALSE;
                  DWORD    iSubKey = 0;

                  // if the service is 3.51-style per server, add it to the list
                  do
                  {
                     TCHAR    szKeyName[ 128 ];
                     DWORD    cchKeyName = sizeof( szKeyName ) / sizeof( *szKeyName );
                        
                     ntEnum = RegEnumKeyEx( hKeyLicenseInfo, iSubKey++, szKeyName, &cchKeyName, NULL, NULL, NULL, NULL );

                     if ( ERROR_SUCCESS == ntEnum )
                     {
                        HKEY  hKeyProduct;

                        NtStatus = RegOpenKeyEx( hKeyLicenseInfo, szKeyName, 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKeyProduct );

                        if ( ERROR_SUCCESS == NtStatus )
                        {
                           DWORD    dwType;
                           TCHAR    szDisplayName[ 128 ];
                           DWORD    cbDisplayName = sizeof( szDisplayName );

                           NtStatus = RegQueryValueEx( hKeyProduct, TEXT( "DisplayName" ), NULL, &dwType, (LPBYTE) szDisplayName, &cbDisplayName );

                           if ( ERROR_SUCCESS == NtStatus )
                           {
                              // is this product secure?
                              BOOL bIsSecure = FALSE;

                              if ( LlsCapabilityIsSupported( m_hLls, LLS_CAPABILITY_SECURE_CERTIFICATES ) )
                              {
                                 NtStatus = ::LlsProductSecurityGet( m_hLls, szDisplayName, &bIsSecure );
                                 theApp.SetLastLlsError( NtStatus );

                                 if ( STATUS_SUCCESS != NtStatus )
                                 {
                                    bIsSecure = FALSE;
                                 }
                              }

                              if ( !bIsSecure )
                              {
#ifdef REMOVE_CONCURRENT_ONLY_IF_PER_SERVER_MODE
                              // not secure; is it in per server mode?
                              DWORD    dwMode;
                              DWORD    cbMode = sizeof( dwMode );

                              NtStatus = RegQueryValueEx( hKeyProduct, TEXT( "Mode" ), NULL, &dwType, (LPBYTE) &dwMode, &cbMode );

                              if ( ( ERROR_SUCCESS == NtStatus ) && dwMode )
                              {
                                 // per server mode; add to list
#endif
                                 DWORD    dwConcurrentLimit;
                                 DWORD    cbConcurrentLimit = sizeof( dwConcurrentLimit );

                                 NtStatus = RegQueryValueEx( hKeyProduct, TEXT( "ConcurrentLimit" ), NULL, &dwType, (LPBYTE) &dwConcurrentLimit, &cbConcurrentLimit );

                                 if (    ( ERROR_SUCCESS == NtStatus )
                                      && ( 0 < dwConcurrentLimit )
                                      && ( m_strProductName.IsEmpty() || !m_strProductName.CompareNoCase( szDisplayName ) )
                                      && ( m_strSourceToUse.IsEmpty() || !m_strSourceToUse.CompareNoCase( TEXT("None")  ) ) )
                                 {
                                    CLicense * pLicense = new CLicense( szDisplayName,
                                                                        TEXT(""),
                                                                        TEXT(""),
                                                                        0,
                                                                        dwConcurrentLimit,
                                                                        TEXT(""),
                                                                        LLS_LICENSE_MODE_ALLOW_PER_SERVER );

                                    if ( NULL != pLicense )
                                    {
                                       m_licenseArray.Add( pLicense );
                                    }
                                 }
                              }
#ifdef REMOVE_CONCURRENT_ONLY_IF_PER_SERVER_MODE
                              }
#endif
                           }

                           RegCloseKey( hKeyProduct );
                        }
                     }
                  } while ( ERROR_SUCCESS == ntEnum );

                  RegCloseKey( hKeyLicenseInfo );
               }

               RegCloseKey( hKeyLocalMachine );
            }

            m_strServerName.ReleaseBuffer();
         }
         
         EndWaitCursor();

         m_bLicensesRefreshed = TRUE;

         // remove any entries from the list that aren't removable
         for ( int i=0; i < m_licenseArray.GetSize(); )
         {
            CLicense* pLicense = (CLicense*) m_licenseArray[ i ];

            VALIDATE_OBJECT( pLicense, CLicense );

            if ( pLicense->m_lQuantity <= 0 )
            {
               delete pLicense;
               m_licenseArray.RemoveAt( i );
            }
            else
            {
               i++;
            }
         }
      }
      else
      {
         theApp.DisplayLastError();
         ResetLicenses();
      }
   }

   return m_bLicensesRefreshed;
}