资源预览内容
第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
亲,该文档总共5页全部预览完了,如果喜欢就下载吧!
资源描述
头文件与库文件#include #pragma comment(lib,Wbemuuid.lib)#include #pragma comment(lib,iphlpapi.lib)链接#include #pragma comment(lib, comsuppw.lib )#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)#define FREE(x) HeapFree(GetProcessHeap(), 0, (x)void setDns()HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);hr =CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT_PRIVACY,RPC_C_ IMP_LEVEL_IMPERSONATE,NULL,EOAC_SECURE_REFS,NULL);IWbemLocator *pLocator = NULL;IWbemServices *pNamespace = NULL;IWbemClassObject *pClass = NULL;IWbemClassObject *pInputParamClass = NULL;IWbemClassObject *pInputParamInstance = NULL;IWbemClassObject *pOutInst = NULL;char sztm64 = 0;int nNetIndex = 0;/获取默认网卡序号get_default_net_index(nNetIndex);BSTR Path = SysAllocString(Lrootcimv2);BSTR ClassPath = SysAllocString(LWin32_NetworkAdapterConfiguration);BSTR MethodName1 = SysAllocString(LSetDNSServerSearchOrder);BSTR MethodName2 = SysAllocString(LEnableDHCP);LPCWSTR MethodName1ArgName = LDNSServerSearchOrder;hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,IID_IWbemLocator, (void *)&pLocator);hr = pLocator-ConnectServer(Path,NULL, NULL, NULL, 0, NULL, NULL, &pNamespace);hr = CoSetProxyBlanket( pNamespace,RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,EOAC_NONE);if(!SUCCEEDED(hr)return ;if(SUCCEEDED(hr)hr = pNamespace-GetObject(ClassPath, 0, NULL, &pClass, NULL); if(!SUCCEEDED(hr)return ;CString cstrSQL ;CString cstrtm;cstrSQL.Format(LSELECT * FROM Win32_NetworkAdapterConfiguration where InterfaceIndex=%d,nNetIndex);IEnumWbemClassObject* pEnumerator = NULL;hr = pNamespace-ExecQuery( bstr_t(WQL), cstrSQL.GetBuffer(), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);if (!SUCCEEDED(hr)return ;elseIWbemClassObject *pclsObj;ULONG uReturn = 0;while (pEnumerator) / 推出下一个对象hr = pEnumerator-Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);/没有东西了就跳出去吧if(0 = uReturn)break;VARIANT vtProp;/ WMI 里面网卡的序号hr = pclsObj-Get(LIndex, 0, &vtProp, 0, 0);int n = vtProp.iVal;/.VariantClear(&vtProp);cstrtm.Format(LWin32_NetworkAdapterConfiguration.Index=%d,n);BSTR InstancePath = SysAllocString(cstrtm.GetBuffer();if(SUCCEEDED(hr)hr = pClass-GetMethod(MethodName1, 0, &pInputParamClass, NULL); if(!SUCCEEDED(hr)return ; if(SUCCEEDED(hr)hr = pInputParamClass-SpawnInstance(0, &pInputParamInstance); if(!SUCCEEDED(hr)return ; if(SUCCEEDED(hr)hr = pInputParamInstance-Put(MethodName1ArgName, 0, NULL, 0); if(!SUCCEEDED(hr)return ; if(SUCCEEDED(hr)hr = pNamespace-ExecMethod(InstancePath, MethodName1, 0, NULL, pInputParamInstance, &pOutInst, NULL);if(!SUCCEEDED(hr)return; if(SUCCEEDED(hr)hr = pNamespace-ExecMethod(InstancePath, MethodName2, 0, NULL, NULL, &pOutInst, NULL);if(!SUCCEEDED(hr)return ;SysFreeString(InstancePath);SysFreeString(Path);SysFreeString(ClassPath);SysFreeString(MethodName1);SysFreeString(MethodName2);if(pOutInst)pOutInst-Release();if(pInputParamInstance) pInputParamInstance-Release();if(pInputParamClass) pInputParamClass-Release();if(pClass) pClass-Release();if(pNamespace) pNamespace-Release();if(pLocator) pLocator-Release();CoUninitialize();return ;PMIB_IPFORWARDTABLE MyGetIpForwardTable(BOOL bOrder) PMIB_IPFORWARDTABLE pIpRouteTab = NULL;DWORD dwActualSize = 0;/ 查询所需缓冲区的大小if(:GetIpForwardTable(pIpRouteTab, &dwActualSize, bOrder) = ERROR_INSUFFICIENT_BUFFER)/ 为 MIB_IPFORWARDTABLE 结构申请内存pIpRouteTab = (PMIB_IPFORWARDTABLE):GlobalAlloc(GPTR, dwActualSize); / 获取路由表if(:GetIpForwardTable(pIpRouteTab, &dwActualSize, bOrder) = NO_ERROR) return pIpRouteTab;:GlobalFree(pIpRouteTab);return NULL; void get_default_net_index(int& nIndex)PMIB_IPFORWARDTABLE pIpRouteTable = MyGetIpForwardTable(TRUE); if(pIpRouteTable != NULL)DWORD i, dwCurrIndex;struct in_addr inadDest;struct in_addr inadMask;struct in_addr inadGateway;PMIB_IPADDRTABLE pIpAddrTable = NULL;for (i = 0; i dwNumEntries; i+)dwCurrIndex = pIpRouteTable-tablei.dwForwardIfIndex;/ 目的地址inadDest.s_addr = pIpRouteTable-tablei.dwForwardDest;if (0 = inadDest.s_addr)/ 子网掩码 inadMask.s_addr = pIpRouteTable-tablei.dwForwardMask;/ 网关地址 inadGateway.s_addr = pIpRouteTable-tablei.dwForwardNextHop;nIndex = pIpRouteTable-tablei.dwForwardIfIndex;break;FREE(pIpRouteTable);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号