如何获取XP控制面板网络连接的连接名
如何获取XP控制面板网络连接窗口中的连接名及其相应适配器名称和描述呢?
[解决办法]
用WMI就可以,程序如下:
//---------------------------------------#include <vcl.h>#include <comdef.h>#include <Wbemidl.h>#pragma comment(lib, "bcbatl.lib")#pragma hdrstopvoid __stdcall _com_issue_error(HRESULT) throw(_com_error){ ShowMessage("com error");}#include "wmi.h"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){HRESULT hRes;IWbemLocator * pIWbemLocator = NULL;IWbemServices * pWbemServices = NULL;IEnumWbemClassObject * pEnumObject = NULL;VARIANT v;hRes=CoInitialize(0);if (FAILED(hRes)){ ShowMessage("CoInitialize error"); return;}//Security needs to be initialized in XP first HRESULT hres1=CoInitializeSecurity( NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,0) ;if ((hres1!=RPC_E_TOO_LATE ) && (hres1 != S_OK)){ CoUninitialize(); ShowMessage("CoInitializeSecurity error"); return;}BSTR bstrNamespace = (L"\\\\localhost\\root\\CIMV2");BSTR name=NULL;BSTR pass=NULL; hRes = CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator, (LPVOID *) &pIWbemLocator);if (SUCCEEDED(hRes)){ hRes = pIWbemLocator->ConnectServer( bstrNamespace, // Namespace name, // Userid pass, // PW NULL, // Locale 0, // flags NULL, // Authority NULL, // Context &pWbemServices );}else{ ShowMessage(" CoCreateInstance error"); CoUninitialize(); return;} hRes = CoSetProxyBlanket(pWbemServices, //ProxyRPC_C_AUTHN_DEFAULT, //Authentication serviceRPC_C_AUTHZ_DEFAULT, //Authorization serviceCOLE_DEFAULT_PRINCIPAL, //Server principal name used by authentication serviceRPC_C_AUTHN_LEVEL_DEFAULT, //Authentication levelRPC_C_IMP_LEVEL_IMPERSONATE, //Impersonation levelCOLE_DEFAULT_AUTHINFO, //Client identityEOAC_DEFAULT //Capability flags);if (FAILED(hRes)){ ShowMessage(" CoSetProxyBlanket error"); CoUninitialize(); return;}WCHAR temp[200],*classname; ;classname=comb->Text.c_str();wcscpy(temp,L"select * from ");wcscat( temp,classname);BSTR strQuery=SysAllocString(temp);BSTR strQL = (L"WQL");hRes = pWbemServices->ExecQuery(strQL, strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);if (FAILED(hRes)){ ShowMessage(" ExecQuery error "); CoUninitialize(); return;}ULONG uCount = 1, uReturned;IWbemClassObject * pClassObject = NULL;hRes = pEnumObject->Reset();hRes = pEnumObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned);if (FAILED(hRes)){ ShowMessage(" get objectclass error "); CoUninitialize(); return;}SAFEARRAY* psarProp = NULL;long lLower, lUpper;BSTR strClassProp=NULL;long row,i,col;pClassObject->GetNames(NULL,WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,NULL,&psarProp);pClassObject->Release();pClassObject = NULL;SafeArrayGetLBound(psarProp, 1, &lLower);SafeArrayGetUBound(psarProp, 1, &lUpper);sg->ColCount = lUpper-lLower+1;for (i = lLower, col = 0; i <= lUpper; ++i, ++col) { hRes = SafeArrayGetElement(psarProp, &i, &strClassProp); sg->Cells[col][0]=strClassProp;// Memo1->Lines->Add(strClassProp);hRes = pEnumObject->Reset();row=0;while (SUCCEEDED(hRes = pEnumObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned))){ if (pClassObject ==NULL) break;// hRes = pClassObject->GetObjectText(0,&(v.bstrVal)); hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0); if (SUCCEEDED(hRes)) { row++; if(v.vt==8) sg->Cells[col][row]=v.bstrVal; else if (v.vt==3 || v.vt==1 ) sg->Cells[col][row]=v.iVal; else sg->Cells[col][row]=String("@")+v.vt; } else { ShowMessage("Error in getting object"); break; } pClassObject->Release(); pClassObject = NULL; VariantClear( &v );} }SysFreeString(strQuery);SafeArrayDestroy(psarProp);pIWbemLocator->Release();pWbemServices->Release();pEnumObject->Release();CoUninitialize(); }//---------------------------------------