Ejemplo n.º 1
0
	void remote_host::get_clsid( const CATID& cat_id, LPCOLESTR server_name, CLSID& server_id )
	{
		DWPRINT( L"get_clsid( %s )\n", server_name );

		CATID Implist[1] = { cat_id };
		ATL::CComPtr<IEnumCLSID> iEnum;
		HRESULT result = root_->EnumClassesOfCategories( 1, Implist, 0, NULL, &iEnum );
		if (FAILED(result))
		{
			throw opc_exception( result, OLESTR("Failed to get enum for categeories") );
		}

		GUID glist;
		ULONG actual;
		while((result = iEnum->Next( 1, &glist, &actual )) == S_OK)
		{
			cotask_holder<OLECHAR> progID;
			cotask_holder<OLECHAR> userType;
			HRESULT res = root_->GetClassDetails( glist, progID.addr(), userType.addr() );

			if(FAILED(res))
			{
				throw opc_exception( res, OLESTR("Failed to get ProgId from ClassId") );
			}
			else
			{
				DWPRINT( L"progId = %s\n", progID );
				if ( lstrcmpW( progID.get(), server_name ) == 0 )
				{
					res = root_->CLSIDFromProgID( progID.get(), &server_id );

					if ( FAILED(res) )
					{
						throw opc_exception( res, OLESTR("IOPCServerList::CLSIDFromProgID - Failed to get ClassId from ProgId") );
					}

					return;
				}
			}
		}
	}
Ejemplo n.º 2
0
	//*
	void local_host::get_clsid( const CATID& cat_id, LPCOLESTR server_name, CLSID& server_id )
	{
#if defined _DEBUG && defined _CONSOLE
		wprintf( L"get_clsid( %s )\n", server_name );
#endif

		CATID Implist[1] = { cat_id };
		ATL::CComPtr<IEnumCLSID> iEnum;
		HRESULT result = root_->EnumClassesOfCategories(1, Implist,0, NULL, &iEnum);
		if (FAILED(result))
		{
			throw opc_exception( result, OLESTR("Failed to get enum for categeories") );
		}

		GUID glist;
		ULONG actual;
		while((result = iEnum->Next(1, &glist, &actual)) == S_OK)
		{
			cotask_holder<OLECHAR> progID;

			HRESULT res = ProgIDFromCLSID(glist, progID.addr());			
			if(FAILED(res))
			{
				throw opc_exception( res, OLESTR("Failed to get ProgId from ClassId") );
			}
			else 
			{
#if defined _DEBUG && defined _CONSOLE
				wprintf( L"progId = %s\n", progID );
#endif
				if ( lstrcmpW( progID.get(), server_name ) == 0 )
				{
					server_id = glist;

					return;					
				}																
			}
		}
	}
Ejemplo n.º 3
0
BOOL CEnumerateSerial::UsingWMI(CSimpleArray<UINT>& ports, CSimpleArray<CString>& friendlyNames)
#endif
{
  //Make sure we clear out any elements which may already be in the array(s)
#if defined CENUMERATESERIAL_USE_STL
  ports.clear();
  friendlyNames.clear();
#else
  ports.RemoveAll();
  friendlyNames.RemoveAll();
#endif  

  //What will be the return value
  BOOL bSuccess = FALSE;

  //Create the WBEM locator
  ATL::CComPtr<IWbemLocator> locator;
  HRESULT hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast<void**>(&locator));
  if (SUCCEEDED(hr))
  {
    ATL::CComPtr<IWbemServices> services;
    hr = locator->ConnectServer(_bstr_t("\\\\.\\root\\cimv2"), NULL, NULL, NULL, 0, NULL, NULL, &services);
    if (SUCCEEDED(hr))
    {
      //Execute the query
      ATL::CComPtr<IEnumWbemClassObject> classObject;
      hr = services->CreateInstanceEnum(_bstr_t("Win32_SerialPort"), WBEM_FLAG_RETURN_WBEM_COMPLETE, NULL, &classObject);
      if (SUCCEEDED(hr))
      {
        bSuccess = TRUE;

        //Now enumerate all the ports
        hr = WBEM_S_NO_ERROR;

        //Final Next will return WBEM_S_FALSE
        while (hr == WBEM_S_NO_ERROR)
        {
          ULONG uReturned = 0;
          ATL::CComPtr<IWbemClassObject> apObj[10];
          hr = classObject->Next(WBEM_INFINITE, 10, reinterpret_cast<IWbemClassObject**>(apObj), &uReturned);
          if (SUCCEEDED(hr))
          {
            for (ULONG n=0; n<uReturned; n++)
            {
              ATL::CComVariant varProperty1;
              HRESULT hrGet = apObj[n]->Get(L"DeviceID", 0, &varProperty1, NULL, NULL);
              if (SUCCEEDED(hrGet) && (varProperty1.vt == VT_BSTR) && (wcslen(varProperty1.bstrVal) > 3))
              {
                //If it looks like "COMX" then add it to the array which will be returned
                if ((_wcsnicmp(varProperty1.bstrVal, L"COM", 3) == 0) && IsNumeric(&(varProperty1.bstrVal[3]), TRUE))
                {
                  //Work out the port number
                  int nPort = _wtoi(&(varProperty1.bstrVal[3]));
                #if defined CENUMERATESERIAL_USE_STL
                  ports.push_back(nPort);
                #else
                  ports.Add(nPort);
                #endif

                  //Also get the friendly name of the port
                  ATL::CComVariant varProperty2;
                  if (SUCCEEDED(apObj[n]->Get(L"Name", 0, &varProperty2, NULL, NULL)) && (varProperty2.vt == VT_BSTR))
                  {  
                #if defined CENUMERATESERIAL_USE_STL
                  #if defined _UNICODE  
                    std::wstring szName(varProperty2.bstrVal);
                  #else
                    std::string szName(ATL::CW2A(varProperty2.bstrVal));
                  #endif
                    friendlyNames.push_back(szName);
                  #else
                    friendlyNames.Add(CString(varProperty2.bstrVal));    
                  #endif
                  }
                  else
                  {
                  #if defined CENUMERATESERIAL_USE_STL
                    friendlyNames.push_back(_T(""));
                  #else
                    friendlyNames.Add(_T(""));
                  #endif  
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  
  return bSuccess;
}