HRESULT GetGC(IDirectorySearch **ppDS) { HRESULT hr; IEnumVARIANT *pEnum = NULL; IADsContainer *pCont = NULL; VARIANT var; IDispatch *pDisp = NULL; ULONG lFetch; // Set IDirectorySearch pointer to NULL. *ppDS = NULL; // First, bind to the GC: namespace container object. hr = ADsOpenObject(TEXT("GC:"), NULL, NULL, ADS_SECURE_AUTHENTICATION, // Use Secure Authentication. IID_IADsContainer, (void**)&pCont); if (FAILED(hr)) { _tprintf(TEXT("ADsOpenObject failed: 0x%x\n"), hr); goto cleanup; } // Get an enumeration interface for the GC container to enumerate the // contents. The actual GC is the only child of the GC container. hr = ADsBuildEnumerator(pCont, &pEnum); if (FAILED(hr)) { _tprintf(TEXT("ADsBuildEnumerator failed: 0x%x\n"), hr); goto cleanup; } // Now enumerate. There is only one child of the GC: object. hr = pEnum->Next(1, &var, &lFetch); if (FAILED(hr)) { _tprintf(TEXT("ADsEnumerateNext failed: 0x%x\n"), hr); goto cleanup; } // Get the IDirectorySearch pointer. if ((hr == S_OK) && (lFetch == 1)) { pDisp = V_DISPATCH(&var); hr = pDisp->QueryInterface(IID_IDirectorySearch, (void**)ppDS); } cleanup: if (pEnum) ADsFreeEnumerator(pEnum); if (pCont) pCont->Release(); if (pDisp) (pDisp)->Release(); return hr; }
/** * @brief Updates information about port maps on router. */ void UpnpNatAction::UpdateInfo() { HRESULT hResult; // Cleanup old port map in case this is an update CleanPortMaps(); // Retrieve current port mappings hResult = nat->get_StaticPortMappingCollection(&portMapCollection); _ErrorException((hResult != S_OK), "retrieving current port mappings (!= S_OK)", hResult, __LINE__, __FILE__); _ErrorException((portMapCollection == NULL), "retrieving current port mappings (NULL)", NULL, __LINE__, __FILE__); // Scan through list and load port maps into vector // Code is based on MSDN sample IUnknown * ptrUnk = NULL; hResult = portMapCollection->get__NewEnum(&ptrUnk); if (SUCCEEDED(hResult)) { IEnumVARIANT * ptrEnumVar = NULL; hResult = ptrUnk->QueryInterface(IID_IEnumVARIANT, (void **) &ptrEnumVar); if (SUCCEEDED(hResult)) { VARIANT varCurDevice; VariantInit(&varCurDevice); ptrEnumVar->Reset(); // Loop through each port map in the collection while (S_OK == ptrEnumVar->Next(1, &varCurDevice, NULL)) { IStaticPortMapping * ptrPortMap = NULL; IDispatch * pdispDevice = V_DISPATCH(&varCurDevice); if (SUCCEEDED(pdispDevice->QueryInterface(IID_IStaticPortMapping, (void **) &ptrPortMap))) { // Add port map to vector UpnpNatPortMapAction * newPortMap = new (nothrow)UpnpNatPortMapAction(ptrPortMap); Utility::DynamicAllocCheck(newPortMap,__LINE__,__FILE__); portMaps.Add(newPortMap); } VariantClear(&varCurDevice); } ptrEnumVar->Release(); } ptrUnk->Release(); } }
static int discover(opal_list_t* nodelist, ICluster* pCluster) { int ret = ORTE_ERROR; int32_t nodeid; orte_node_t *node; opal_list_item_t* item; opal_list_t new_nodes; struct timeval start, stop; HRESULT hr = S_OK; long idle_processors = 0; IClusterEnumerable* pNodesCollection = NULL; IEnumVARIANT* pNodes = NULL; INode* pNode = NULL; BSTR node_name = NULL, node_arch = NULL; VARIANT var; NodeStatus Status; size_t len; /* check for timing request - get start time if so */ if (orte_timing) { gettimeofday(&start, NULL); } /* Get the collection of nodes. */ hr = pCluster->get_ComputeNodes(&pNodesCollection); if (FAILED(hr)) { ras_get_cluster_message(pCluster); OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:pCluster->get_ComputeNodes failed.")); return ORTE_ERROR; } /* Get the enumerator used to iterate through the collection. */ hr = pNodesCollection->GetEnumerator(&pNodes); if (FAILED(hr)) { ras_get_cluster_message(pCluster); OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:pNodesCollection->GetEnumerator failed.")); return ORTE_ERROR; } VariantInit(&var); /* Construct new node list. */ OBJ_CONSTRUCT(&new_nodes, opal_list_t); nodeid=0; /* Loop through the collection. */ while (hr = pNodes->Next(1, &var, NULL) == S_OK) { var.pdispVal->QueryInterface(IID_INode, reinterpret_cast<void **> (&pNode)); /* Check wether the node is ready. * There are four states: * NodeStatus_Ready = 0, * NodeStatus_Paused = 1, * NodeStatus_Unreachable = 2, probably not a windows cluster node. * NodeStatus_PendingApproval = 3 */ hr = pNode->get_Status(&Status); if (FAILED(hr)) { OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:pNode->get_Status failed.")); ret = ORTE_ERROR; goto cleanup; } /* Get available number of processors on each node. */ hr = pNode->get_NumberOfIdleProcessors(&idle_processors); if (FAILED(hr)) { OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:pNode->get_NumberOfIdleProcessors failed.")); ret = ORTE_ERROR; goto cleanup; } /* Do we have enough processors on the available nodes? * Question: How do we get the required number of processors? */ if ( (Status == NodeStatus_Ready) && (idle_processors > 0) ) { /* Get node name. */ hr = pNode->get_Name(&node_name); if (FAILED(hr)) { OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:pNode->get_Name failed.")); ret = ORTE_ERROR; goto cleanup; } /* Get node processor architecture. */ hr = pNode->get_ProcessorArchitecture(&node_arch); if (FAILED(hr)) { OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:pNode->get_ProcessorArchitecture failed.")); ret = ORTE_ERROR; goto cleanup; } /* Prevent duplicated nodes in the list*/ for (item = opal_list_get_first(&new_nodes); opal_list_get_end(&new_nodes) != item; item = opal_list_get_next(item)) { node = (orte_node_t*) item; if (0 == strcmp(node->name, (char *)node_name)) { ++node->slots; OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:allocate:discover: found -- bumped slots to %d", node->slots)); break; } } /* Did we find it? */ if (opal_list_get_end(&new_nodes) == item) { /* Nope -- didn't find it, so add a new item to the list */ OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:allocate:discover: not found -- added to list")); node = OBJ_NEW(orte_node_t); /* The function _dupenv_s is much safer than getenv on Windows. */ _dupenv_s(&node->username, &len, "username"); node->name = _com_util::ConvertBSTRToString(node_name); node->launch_id = nodeid; node->slots_inuse = 0; node->slots_max = 0; node->slots = 1; opal_list_append(nodelist, &node->super); } /* up the nodeid */ nodeid++; } pNode->Release(); VariantClear(&var); } pNodes->Release(); if (nodeid > 0) ret = ORTE_SUCCESS; /* All done */ cleanup: if (ORTE_SUCCESS == ret) { OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:allocate:discover: success")); } else { OPAL_OUTPUT_VERBOSE((1, orte_ras_base.ras_output, "ras:ccp:allocate:discover: failed (rc=%d)", ret)); } OBJ_DESTRUCT(&new_nodes); SysFreeString(node_name); SysFreeString(node_arch); /* check for timing request - get stop time and report elapsed time if so */ if (orte_timing) { gettimeofday(&stop, NULL); opal_output(0, "ras_ccp: time to allocate is %ld usec", (long int)((stop.tv_sec - start.tv_sec)*1000000 + (stop.tv_usec - start.tv_usec))); gettimeofday(&start, NULL); } return ret; }
BOOLEAN GetParameters( IN DWORD Count, IN WCHAR* Arguments[], OUT IDiscRecorder2** Recorder, OUT VARIANT_BOOL* FullErase ) { BOOLEAN validArgument = TRUE; if ((Count < 1) || (Count > 2)) { validArgument = FALSE; } if (validArgument && (Arguments[0][1] != ':')) { validArgument = FALSE; } if (validArgument && (Count == 2)) { if ( _wcsnicmp(Arguments[1], (L"-fullerase"), strlen("-fullerase")) == 0 ) { *FullErase = VARIANT_TRUE; } else { validArgument = FALSE; } } //Try to retrieve the recorder. if (validArgument) { HRESULT hr = S_OK; IDiscMaster2* tmpDiscMaster = NULL; IEnumVARIANT* tmpRecorderEnumerator = NULL; IDiscRecorder2* tmpRecorder = NULL; VARIANT tmpUniqueId; BOOLEAN recorderFound = FALSE; // create the disc master object if (SUCCEEDED(hr)) { hr = CoCreateInstance(CLSID_MsftDiscMaster2, NULL, CLSCTX_ALL, IID_PPV_ARGS(&tmpDiscMaster) ); if (FAILED(hr)) { printf("Failed to Create DiscMaster. Error: 0x%X.\n", hr); } } // get a new enumerator for the disc recorders if (SUCCEEDED(hr)) { hr = tmpDiscMaster->get__NewEnum(&tmpRecorderEnumerator); if (FAILED(hr)) { printf("Failed get__NewEnum. Error: 0x%X.\n", hr); } } // Reset the enumerator to the beginning if (SUCCEEDED(hr)) { hr = tmpRecorderEnumerator->Reset(); if (FAILED(hr)) { printf("Failed to reset enumerator. Error: 0x%X.\n", hr); } } // request just that one recorder's BSTR if (SUCCEEDED(hr)) { while (SUCCEEDED(hr) && !recorderFound) { hr = tmpRecorderEnumerator->Next(1, &tmpUniqueId, NULL); if (SUCCEEDED(hr)) { // Create a new IDiscRecorder2 hr = CoCreateInstance(CLSID_MsftDiscRecorder2, NULL, CLSCTX_ALL, IID_PPV_ARGS(&tmpRecorder) ); if (FAILED(hr)) { printf("Failed CoCreateInstance for DiscRecorder. Error: 0x%X.\n", hr); } // Initialize it with the provided BSTR if (SUCCEEDED(hr)) { hr = tmpRecorder->InitializeDiscRecorder(tmpUniqueId.bstrVal); if (FAILED(hr)) { printf("Failed to init disc recorder. Error: 0x%X.\n", hr); } } //Check if it's the right recorder. if (SUCCEEDED(hr)) { SAFEARRAY * mountPoints = NULL; hr = tmpRecorder->get_VolumePathNames(&mountPoints); if (FAILED(hr)) { printf("Unable to get mount points. Error: 0x%X.\n", hr); } else if (mountPoints->rgsabound[0].cElements == 0) { printf("\tMount Point: *** NO MOUNT POINTS FOR RECORDER ***\n"); } else { VARIANT* tmp = (VARIANT*)(mountPoints->pvData); for (ULONG i = 0; i < mountPoints->rgsabound[0].cElements; i++) { if ( _wcsnicmp(Arguments[0], tmp[i].bstrVal, 2) == 0 ) { recorderFound = TRUE; break; } } } SafeArrayDestroyDataAndNull(mountPoints); } } } } // copy to caller or release recorder if (SUCCEEDED(hr) && recorderFound) { *Recorder = tmpRecorder; } else { validArgument = FALSE; ReleaseAndNull(tmpRecorder); } // all other cleanup ReleaseAndNull(tmpDiscMaster); } return validArgument; }
HRESULT WindowsFirewallHelper::removeByBasename( const char *name ) { HRESULT hr = S_OK; IUnknown* pUnknown = NULL; IEnumVARIANT* pEnum = NULL; INetFwAuthorizedApplication* fwApp = NULL; bool result = true; long count; unsigned long cnt; int i; VARIANT v; if ( ! ready() ) { return S_FALSE; } hr = fwApps->get__NewEnum(&pUnknown); if ( hr != S_OK ) { dprintf(D_ERROR, "Failed to get enumerator for Authorized " "Applications (err=%x)\n", hr); return hr; } hr = fwApps->get_Count(&count); if ( hr != S_OK ) { dprintf(D_ERROR, "Failed to get count of Authorized " "Applications (err=%x)\n", hr); return hr; } hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void**)&pEnum); if ( hr != S_OK ) { if ( hr == E_NOINTERFACE ) { dprintf(D_ERROR, "Failed to QueryInterface for trusted " "applications. Interface not supported.\n"); } else { dprintf(D_ERROR, "Failed to QueryInterface for trusted " "applications. (err=%x)\n", hr); } return hr; } for (i=0; i<count; i++) { BSTR str = NULL; size_t len; char *tmp; const char *bn; hr = pEnum->Next(1, &v, &cnt); // interesting tidbit: Microsoft says Enum->Next() function // either returns S_OK or S_FALSE. Funny, on Win2k3 SP1 // it returns 0x80020008, or Bad Variable Type. Sigh. if ( hr != S_OK ) { // no more elements. stop. hr = S_FALSE; break; } fwApp = (INetFwAuthorizedApplication*)v.byref; fwApp->get_ProcessImageFileName(&str); // This should not be printing to stdout, but if you are // reading this and think it is necessary to print these // executable names then dprintf() them. // printf("Result is %lS\n", str); len = wcslen(str); tmp = (char*) malloc((len*2+1) * sizeof(char)); ASSERT(tmp); sprintf(tmp, "%S", str); bn = condor_basename(tmp); if ( 0 == strcasecmp(bn, name) ) { // basenames match, so remove it from the list. hr = removeTrusted(tmp); } free(tmp); SysFreeString(str); if (hr == E_ACCESSDENIED) { // don't have enough privilege to remove, so don't bother to keep trying. break; } } return hr; }
void CDlgIADsSecurityDescriptor::PopulateACL(IADsAccessControlList * pACL) { ASSERT( pACL ); HRESULT hr; ULONG lFetch; IEnumVARIANT *pEnum; LPUNKNOWN pUnk; VARIANT var; IDispatch *pDisp; BSTR bstr; CString s; IADsAccessControlEntry *pACE; m_cACEList.ResetContent(); ResetAcePtr(); hr = pACL->get__NewEnum( &pUnk ); if ( !SUCCEEDED(hr) ) { return; } hr = pUnk->QueryInterface( IID_IEnumVARIANT, (void**) &pEnum ); if ( !SUCCEEDED(hr) ) { return; } hr = pEnum->Next( 1, &var, &lFetch ); while( hr == S_OK ) { if ( lFetch == 1 ) { if ( VT_DISPATCH != V_VT(&var) ) { break; } pDisp = V_DISPATCH(&var); /////////////////////////// // Get the ACE ///////////////////////////// hr = pDisp->QueryInterface( IID_IADsAccessControlEntry, (void**)&pACE ); if ( SUCCEEDED(hr) ) { pACE->get_Trustee(&bstr); s = bstr; SysFreeString(bstr); m_cACEList.AddString( s ); m_acePtrList.AddTail( pACE ); //save the pointer for future use, // we don't need to Release() it. } VariantClear(&var); } hr = pEnum->Next( 1, &var, &lFetch ); }; pACL->Release(); }
int __cdecl main() { HRESULT hrComInit = S_OK; HRESULT hr = S_OK; ULONG cFetched = 0; CComVariant var; IUnknown *pEnumerator; IEnumVARIANT* pVariant = NULL; INetFwPolicy2 *pNetFwPolicy2 = NULL; INetFwRules *pFwRules = NULL; INetFwRule *pFwRule = NULL; long fwRuleCount; // Initialize COM. hrComInit = CoInitializeEx( 0, COINIT_APARTMENTTHREADED ); // Ignore RPC_E_CHANGED_MODE; this just means that COM has already been // initialized with a different mode. Since we don't care what the mode is, // we'll just use the existing mode. if (hrComInit != RPC_E_CHANGED_MODE) { if (FAILED(hrComInit)) { wprintf(L"CoInitializeEx failed: 0x%08lx\n", hrComInit); goto Cleanup; } } // Retrieve INetFwPolicy2 hr = WFCOMInitialize(&pNetFwPolicy2); if (FAILED(hr)) { goto Cleanup; } // Retrieve INetFwRules hr = pNetFwPolicy2->get_Rules(&pFwRules); if (FAILED(hr)) { wprintf(L"get_Rules failed: 0x%08lx\n", hr); goto Cleanup; } // Obtain the number of Firewall rules hr = pFwRules->get_Count(&fwRuleCount); if (FAILED(hr)) { wprintf(L"get_Count failed: 0x%08lx\n", hr); goto Cleanup; } wprintf(L"The number of rules in the Windows Firewall are %d\n", fwRuleCount); // Iterate through all of the rules in pFwRules pFwRules->get__NewEnum(&pEnumerator); if(pEnumerator) { hr = pEnumerator->QueryInterface(__uuidof(IEnumVARIANT), (void **) &pVariant); } while(SUCCEEDED(hr) && hr != S_FALSE) { var.Clear(); hr = pVariant->Next(1, &var, &cFetched); if (S_FALSE != hr) { if (SUCCEEDED(hr)) { hr = var.ChangeType(VT_DISPATCH); } if (SUCCEEDED(hr)) { hr = (V_DISPATCH(&var))->QueryInterface(__uuidof(INetFwRule), reinterpret_cast<void**>(&pFwRule)); } if (SUCCEEDED(hr)) { // Output the properties of this rule DumpFWRulesInCollection(pFwRule); } } } Cleanup: // Release pFwRule if (pFwRule != NULL) { pFwRule->Release(); } // Release INetFwPolicy2 if (pNetFwPolicy2 != NULL) { pNetFwPolicy2->Release(); } // Uninitialize COM. if (SUCCEEDED(hrComInit)) { CoUninitialize(); } return 0; }
HRESULT DoTheWork (INetSharingManager * pNSM) { // add a port mapping to every firewalled or shared connection INetSharingEveryConnectionCollection * pNSECC = NULL; HRESULT hr = pNSM->get_EnumEveryConnection (&pNSECC); int LastErrorCode = 0; if (!pNSECC) return ICS_Error_FailGetEvery; else { // enumerate connections IEnumVARIANT * pEV = NULL; IUnknown * pUnk = NULL; hr = pNSECC->get__NewEnum (&pUnk); if (pUnk) { hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT), (void**)&pEV); pUnk->Release(); }else{ return ICS_Error_FailGetNewEnum; } if (pEV) { VARIANT v; VariantInit (&v); while (S_OK == pEV->Next (1, &v, NULL)) { if (V_VT (&v) == VT_UNKNOWN) { INetConnection * pNC = NULL; V_UNKNOWN (&v)->QueryInterface (__uuidof(INetConnection), (void**)&pNC); if (pNC) { INetConnectionProps * pNCP = NULL; pNSM->get_NetConnectionProps (pNC, &pNCP); if (!pNCP) wprintf (L"failed to get NetConnectionProps!\r\n"); else { // check properties for firewalled or shared connection NETCON_MEDIATYPE MediaType; pNCP->get_MediaType(&MediaType); NETCON_STATUS Status; pNCP->get_Status(&Status); BSTR DevName; pNCP->get_DeviceName(&DevName); if (MediaType & (NCM_LAN | NCM_SHAREDACCESSHOST_LAN | NCM_PHONE) && Status == NCS_CONNECTED && QString(_com_util::ConvertBSTRToString(DevName)).indexOf("hosted network", 0, Qt::CaseInsensitive)==-1 && QString(_com_util::ConvertBSTRToString(DevName)).indexOf("virtual", 0, Qt::CaseInsensitive)==-1 && QString(_com_util::ConvertBSTRToString(DevName)).indexOf("teamviewer", 0, Qt::CaseInsensitive)==-1) { // got a shared/firewalled connection INetSharingConfiguration * pNSC = NULL; hr = pNSM->get_INetSharingConfigurationForINetConnection (pNC, &pNSC); if (!pNSC) wprintf (L"can't make INetSharingConfiguration object!\r\n"); else { hr = pNSC->EnableSharing(ICSSHARINGTYPE_PRIVATE); if(hr!=S_OK){ LastErrorCode = ICS_Error_EnableSharing; }else{ BSTR Name; pNCP->get_Name(&Name); QMessageBox msg; msg.setText(QString("Network: %1 %2 %3").arg(_com_util::ConvertBSTRToString(Name)).arg(_com_util::ConvertBSTRToString(DevName)).arg(Status)); msg.exec(); return 0; } pNSC->Release(); } } pNCP->Release(); } pNC->Release(); } } VariantClear (&v); } pEV->Release(); }else{ return ICS_Error_FailGetEnumVariant; } pNSECC->Release(); } if(LastErrorCode!=0) return LastErrorCode; return hr; }