SetNetDlg.cpp
资源名称:SetIP.rar [点击查看]
上传用户:bjdfmy_886
上传日期:2007-09-01
资源大小:45k
文件大小:19k
源码类别:
网格计算
开发平台:
Visual C++
- // SetNetDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "SetNet.h"
- #include "SetNetDlg.h"
- #include ".setnetdlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- END_MESSAGE_MAP()
- // CSetNetDlg 对话框
- CSetNetDlg::CSetNetDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CSetNetDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CSetNetDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CSetNetDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(IDOK, OnBnClickedOk)
- END_MESSAGE_MAP()
- // CSetNetDlg 消息处理程序
- BOOL CSetNetDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 将“关于...”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- /* CoInitialize(NULL);
- CLSID clsid;
- CLSIDFromProgID(OLESTR("NetCfg.NetConfig"),&clsid);
- CComPtr<INetConfig> pGetRes;//智能指针
- pGetRes.CoCreateInstance(clsid);*/
- //pGetRes->get
- //pGetRes->Hello();
- //pGetRes.Release();//小心哦!!请看最后的“注意”
- //CoUninitialize();
- return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
- }
- void CSetNetDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CSetNetDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作矩形中居中
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标显示。
- HCURSOR CSetNetDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- IWbemLocator* g_pLoc = NULL;
- IWbemServices* g_pSvc = NULL;
- IEnumWbemClassObject* g_pEnum = NULL;
- IWbemClassObject* g_pAdapterObject = NULL;
- IWbemClassObject* g_pAdapterConfigClass = NULL;
- VARIANT g_AdapterConfigPathVal;
- #define AE_SET (-1)
- #define AE_SUCCESS (0)
- USHORT ANWMIInit(PCWSTR pszAdapterName)
- {
- HRESULT hres;
- CComBSTR TheQuery = NULL;
- ULONG NumObjects = 0;
- // Step 1: Initialize COM.
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres))
- {
- OutputDebugString("CoInitializeEx failed!!n");
- return AE_SET;
- }
- // Step 2: Set general COM security levels
- // Note: If you are using Windows 2000, you need to specify -
- // the default authentication credentials for a user by using
- // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
- // parameter of CoInitializeSecurity ------------------------
- hres = CoInitializeSecurity(
- NULL,
- -1, // COM negotiates service
- NULL, // Authentication services
- NULL, // Reserved
- RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
- RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
- NULL, // Authentication info
- EOAC_NONE, // Additional capabilities
- NULL // Reserved
- );
- if (FAILED(hres))
- {
- OutputDebugString("CoInitializeSecurity failed!!n");
- return AE_SET;
- }
- // Step 3: Obtain the initial locator to WMI
- hres = CoCreateInstance(
- CLSID_WbemLocator,
- 0,
- CLSCTX_INPROC_SERVER,
- IID_IWbemLocator,
- (LPVOID*)&g_pLoc);
- if (FAILED(hres))
- {
- OutputDebugString("CoCreateInstance failed!!n");
- return AE_SET;
- }
- // Step 4: Connect to the local rootcimv2 namespace and obtain pointer pSvc to make IWbemServices calls.
- hres = g_pLoc->ConnectServer(
- _bstr_t(L"ROOT\CIMV2"),
- NULL,
- NULL,
- 0,
- NULL,
- 0,
- 0,
- &g_pSvc);
- if (FAILED(hres))
- {
- OutputDebugString("ConnectServer failed!!n");
- return AE_SET;
- }
- // Step 5: Set security levels for the proxy
- hres = CoSetProxyBlanket(
- g_pSvc, // Indicates the proxy to set
- RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
- RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
- NULL, // Server principal name
- RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
- RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
- NULL, // client identity
- EOAC_NONE // proxy capabilities
- );
- if (FAILED(hres))
- {
- OutputDebugString("CoSetProxyBlanket failed!!n");
- return AE_SET;
- }
- // 通过适配器名称来找到指定的适配器对象.
- TheQuery = L"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE SettingID = "";
- TheQuery += pszAdapterName;
- TheQuery += L""";
- hres = g_pSvc->ExecQuery(
- //SysAllocString(L"WQL"),
- L"WQL",
- TheQuery,
- WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
- NULL,
- &g_pEnum);
- if (FAILED(hres))
- {
- OutputDebugString("ExecQuery failed!!n");
- return AE_SET;
- }
- // Get the adapter object.
- hres = g_pEnum->Next(WBEM_INFINITE, 1, &g_pAdapterObject, &NumObjects);
- if (SUCCEEDED(hres))
- {
- if (NumObjects < 0)
- {
- OutputDebugString("Can not get the specified adapter!!n");
- return AE_SET;
- }
- }
- hres = g_pSvc->GetObject(
- L"Win32_NetworkAdapterConfiguration",
- 0,
- NULL,
- &g_pAdapterConfigClass,
- NULL);
- if (FAILED(hres))
- {
- OutputDebugString("GetObject failed!!n");
- return AE_SET;
- }
- hres = g_pAdapterObject->Get(
- L"__RELPATH",
- 0,
- &g_AdapterConfigPathVal,
- NULL,
- NULL);
- if (FAILED(hres))
- {
- OutputDebugString("Get failed!!n");
- return AE_SET;
- }
- return AE_SUCCESS;
- }
- void ANWMIRelease()
- {
- VariantClear(&g_AdapterConfigPathVal);
- if (g_pAdapterConfigClass)
- {
- g_pAdapterConfigClass->Release();
- g_pAdapterConfigClass = NULL;
- }
- if (g_pAdapterObject)
- {
- g_pAdapterObject->Release();
- g_pAdapterObject = NULL;
- }
- if (g_pEnum)
- {
- g_pEnum->Release();
- g_pEnum = NULL;
- }
- if (g_pSvc)
- {
- g_pSvc->Release();
- g_pSvc = NULL;
- }
- if (g_pLoc)
- {
- g_pLoc->Release();
- g_pLoc = NULL;
- }
- CoUninitialize();
- }
- typedef struct tagMACDATA
- {
- DWORD ulStateMask;
- DWORD ulIP;
- DWORD ulMask;
- DWORD ulGateway;
- }MACDATA, *PMACDATA;
- USHORT ANEnableStaticInVista(PIP_ADAPTER_INDEX_MAP pimAdapter, PMACDATA pData)
- {
- HRESULT hres;
- USHORT usStatus = AE_SET;
- //VARIANT AdapterConfigPathVal;
- //IWbemClassObject* pAdapterConfigClass = NULL;
- IWbemClassObject* pInParamsDefinition = NULL;
- IWbemClassObject* pInParams = NULL;
- struct in_addr in;
- SAFEARRAY* pIpAddress = NULL;
- SAFEARRAY* pSubnetMask = NULL;
- SAFEARRAY* pGatewayAddress = NULL;
- SAFEARRAYBOUND bound[1];
- long i = 0;
- VARIANT ArrayVal;
- IWbemClassObject* pOutParams = NULL;
- VARIANT varReturnValue;
- //WCHAR address[260];
- CComBSTR IPAddress;
- CComBSTR MASKAddress;
- CComBSTR Gateway;
- if (AE_SUCCESS != ANWMIInit(wcschr(pimAdapter->Name, L'{')))
- {
- goto END;
- }
- // if ((pData->ulStateMask & AS_IP_CHANGED) || (pData->ulStateMask & AS_MASK_CHANGED))
- {
- DWORD dwIP = pData->ulIP;//htonl();
- DWORD dwMask = pData->ulMask;//htonl(pData->ulMask);
- //Get the EnableStatic method and the in-params class.
- hres = g_pAdapterConfigClass->GetMethod(
- L"EnableStatic",
- 0,
- &pInParamsDefinition,
- NULL);
- if (FAILED(hres))
- {
- OutputDebugString("GetMethod failed!!n");
- goto END;
- }
- //Get the in-params object.
- hres = pInParamsDefinition->SpawnInstance(0, &pInParams);
- if (FAILED(hres))
- {
- OutputDebugString("SpawnInstance failed!!n");
- goto END;
- }
- //------------------Set the in-params values for ip address.创建一个设置IP的参数
- bound[0].lLbound = 0;
- bound[0].cElements = 1;
- pIpAddress = SafeArrayCreate(VT_BSTR, 1, bound);
- if(pIpAddress == NULL)
- {
- OutputDebugString("SafeArrayCreate failed!!n");
- goto END;
- }
- in.s_addr = dwIP;
- //AnsiToUnicode(inet_ntoa(in),address);
- IPAddress = inet_ntoa(in);
- OutputDebugString("the ip address is:");
- OutputDebugStringW(IPAddress);
- hres = SafeArrayPutElement(pIpAddress, &i, (BSTR)IPAddress);
- if (FAILED(hres))
- {
- OutputDebugString("SafeArrayPutElement failed!!n");
- goto END;
- }
- //---------------------------------------------------------------------------------
- //Set the in-params values for subnet mask address. 创建一个设置子网掩码的参数
- pSubnetMask = SafeArrayCreate(VT_BSTR, 1, bound);
- if(pSubnetMask == NULL)
- {
- OutputDebugString("SafeArrayCreate failed!!n");
- goto END;
- }
- in.s_addr = dwMask;
- //AnsiToUnicode(inet_ntoa(in),address);
- MASKAddress = "255.255.255.0";//inet_ntoa(in);
- OutputDebugString("the submask is:n");
- OutputDebugStringW(MASKAddress);
- hres = SafeArrayPutElement(pSubnetMask, &i, (BSTR)MASKAddress);
- if (FAILED(hres))
- {
- OutputDebugString("SafeArrayPutElement failed!!n");
- goto END;
- }
- //---------------------------------------------------------------------------------
- // bind the in-params.
- ArrayVal.parray = pIpAddress;
- ArrayVal.vt = VT_ARRAY|VT_BSTR;
- hres = pInParams->Put(L"IPAddress", 0,&ArrayVal, 0);
- ArrayVal.parray = pSubnetMask;
- hres = pInParams->Put(L"SubnetMask", 0, &ArrayVal, 0);
- // Now actually execute the "EnableStatic" method ----执行更改
- hres = g_pSvc->ExecMethod(
- g_AdapterConfigPathVal.bstrVal,
- L"EnableStatic",
- 0,
- NULL,
- pInParams,
- &pOutParams,
- NULL);
- if (FAILED(hres))
- {
- OutputDebugString("ExecMethod failed!!n");
- goto END;
- }
- // Get the return value.
- VariantInit(&varReturnValue);
- hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0, &varReturnValue, NULL, 0);
- if (FAILED(hres))
- {
- OutputDebugString("Get return value failed!!n");
- goto END;
- }
- // Check the return value.
- if (0 != varReturnValue.lVal)
- {
- char a[100];
- sprintf(a,"%lx", varReturnValue.lVal);
- OutputDebugString("set ip and mask address failed!!n");
- OutputDebugString(a);
- goto END;
- }
- /* VariantClear(&varReturnValue);
- if (pOutParams)
- {
- pOutParams->Release();
- pOutParams = NULL;
- }
- if(pIpAddress)
- {
- SafeArrayDestroy(pIpAddress);
- pIpAddress = NULL;
- }
- if(pSubnetMask)
- {
- SafeArrayDestroy(pSubnetMask);
- pSubnetMask = NULL;
- }
- if (pInParams)
- {
- pInParams->Release();
- pInParams = NULL;
- }
- if (pInParamsDefinition)
- {
- pInParamsDefinition->Release();
- pInParamsDefinition = NULL;
- }*/
- }//ip and subnet mask address
- // if (pData->ulStateMask & AS_GATEWAY_CHANGED)
- {
- // if ((pData->ulStateMask & AS_IP_CHANGED) || (pData->ulStateMask & AS_MASK_CHANGED))
- {
- // Sleep(5000);
- }
- DWORD dwGateway = htonl(pData->ulGateway);
- //Get the EnableStatic method and the in-params class.
- hres = g_pAdapterConfigClass->GetMethod(
- L"SetGateways",
- 0,
- &pInParamsDefinition,
- NULL);
- if (FAILED(hres))
- {
- OutputDebugString("GetMethod failed!!n");
- goto END;
- }
- //Get the in-params object.
- hres = pInParamsDefinition->SpawnInstance(0, &pInParams);
- if (FAILED(hres))
- {
- OutputDebugString("SpawnInstance failed!!n");
- goto END;
- }
- //Set the in-params values for Getewat.
- bound[0].lLbound = 0;
- bound[0].cElements = 1;
- pGatewayAddress = SafeArrayCreate(VT_BSTR, 1, bound);
- if(pGatewayAddress == NULL)
- {
- OutputDebugString("SafeArrayCreate failed!!n");
- goto END;
- }
- in.s_addr = htonl(dwGateway);
- //AnsiToUnicode(inet_ntoa(in),address);
- Gateway = inet_ntoa(in);
- OutputDebugString("the gateway address is:");
- OutputDebugStringW(Gateway);
- hres = SafeArrayPutElement(pGatewayAddress, &i, (BSTR)Gateway);
- if (FAILED(hres))
- {
- OutputDebugString("SafeArrayPutElement failed!!n");
- goto END;
- }
- // bind the in-params.
- ArrayVal.parray = pGatewayAddress;
- ArrayVal.vt = VT_ARRAY|VT_BSTR;
- hres = pInParams->Put(L"DefaultIPGateway", 0,&ArrayVal, 0);
- // Now actually execute the "EnableStatic" method
- hres = g_pSvc->ExecMethod(
- g_AdapterConfigPathVal.bstrVal,
- L"SetGateways",
- 0,
- NULL,
- pInParams,
- &pOutParams,
- NULL);
- if (FAILED(hres))
- {
- OutputDebugString("ExecMethod failed!!n");
- goto END;
- }
- // Get the return value.
- VariantInit(&varReturnValue);
- hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0, &varReturnValue, NULL, 0);
- if (FAILED(hres))
- {
- OutputDebugString("Get return value failed!!n");
- goto END;
- }
- // Check the return value.
- if (0 != varReturnValue.lVal)
- {
- char a[100];
- sprintf(a,"%lx", varReturnValue.lVal);
- OutputDebugString(a);
- goto END;
- }
- VariantClear(&varReturnValue);
- if (pOutParams)
- {
- pOutParams->Release();
- pOutParams = NULL;
- }
- if (pGatewayAddress)
- {
- SafeArrayDestroy(pGatewayAddress);
- pGatewayAddress = NULL;
- }
- if (pInParams)
- {
- pInParams->Release();
- pInParams = NULL;
- }
- if (pInParamsDefinition)
- {
- pInParamsDefinition->Release();
- pInParamsDefinition = NULL;
- }
- }//Gateway
- usStatus = AE_SUCCESS;
- END:
- VariantClear(&varReturnValue);
- if (pOutParams)
- {
- pOutParams->Release();
- pOutParams = NULL;
- }
- if(pIpAddress)
- {
- SafeArrayDestroy(pIpAddress);
- pIpAddress = NULL;
- }
- if(pSubnetMask)
- {
- SafeArrayDestroy(pSubnetMask);
- pSubnetMask = NULL;
- }
- if (pGatewayAddress)
- {
- SafeArrayDestroy(pGatewayAddress);
- pGatewayAddress = NULL;
- }
- if (pInParams)
- {
- pInParams->Release();
- pInParams = NULL;
- }
- if (pInParamsDefinition)
- {
- pInParamsDefinition->Release();
- pInParamsDefinition = NULL;
- }
- ANWMIRelease();
- return usStatus;
- }
- void CSetNetDlg::OnBnClickedOk()
- {
- // TODO: 在此添加控件通知处理程序代码
- //OnOK();
- PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
- ULONG ulLen = sizeof(IP_ADAPTER_INFO);
- memset(pAdapterInfo, 0, sizeof(IP_ADAPTER_INFO));
- DWORD dwError = GetAdaptersInfo( pAdapterInfo, &ulLen );
- if (dwError != 0)
- {
- free(pAdapterInfo);
- return;
- }
- PIP_ADAPTER_INFO pSrc = pAdapterInfo;
- //while(pAdapterInfo != NULL)
- {
- if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
- {
- // if (pAdapterInfo->AddressLength < 6)
- // continue;
- ADAPTER_INFO *pAI = new ADAPTER_INFO;
- pAI->bWireless = FALSE;
- pAI->strDriverDesc = pAdapterInfo->Description;
- pAI->strName = pAdapterInfo->AdapterName;
- pAI->strNetMask = pAdapterInfo->IpAddressList.IpMask.String ;
- pAI->strNetGate = pAdapterInfo->GatewayList.IpAddress.String;
- pAI->strIP = pAdapterInfo->IpAddressList.IpAddress.String;
- pAI->bDHCP = (BOOL)pAdapterInfo->DhcpEnabled;
- if (pAI->bDHCP)
- pAI->strDhcpServer = pAdapterInfo->DhcpServer.IpAddress.String;
- //RegGetIP(pAI, pAI->strName.c_str(), 0);
- //GetLocalAdapterName(pAI, pAI->strName.c_str());
- //m_vecAdapters.push_back(pAI);
- }
- //pAdapterInfo = pAdapterInfo->Next;
- }
- IP_ADAPTER_INDEX_MAP strAdapter;
- MultiByteToWideChar (CP_ACP, 0, pAdapterInfo->AdapterName, -1, strAdapter.Name, 128);
- // wcscpy(strAdapter.Name, pAdapterInfo->AdapterName);
- strAdapter.Index = 0;
- MACDATA strData;
- strData.ulIP = inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
- strData.ulMask = atoi(pAdapterInfo->IpAddressList.IpMask.String);
- strData.ulGateway = inet_addr(pAdapterInfo->GatewayList.IpAddress.String);
- strData.ulStateMask = 0;
- ANEnableStaticInVista(&strAdapter, &strData);
- free(pSrc);
- }