启用网络适配器后,将设置自动配置IP地址 [英] On enabling Network adapter, Autoconfiguration IP address getting set

查看:652
本文介绍了启用网络适配器后,将设置自动配置IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Visual Studio C ++开发适用于Windows Vista和7的应用程序,其中我必须将静态IP地址分配给网卡并建立连接.为此,我要在注册表中输入Ip值,并将Enable DHCP值设置为0.然后,我需要先禁用然后再启用网卡,这些值才能生效.为此,我在以下代码中使用"INetConnectionManager":

I am developing an application for Windows Vista and 7 in Visual Studio C++, in which I have to assign static IP address to a network card and establish a connection. For this, I am entering the Ip values in registry along with setting the Enable DHCP value to 0. Then I need to disable and then enable the network card for these values to take effect. For this, I am using "INetConnectionManager" in the following code:

  CoInitialize(0);
  typedef void (__stdcall * PNcFreeNetconProperties)(NETCON_PROPERTIES* pProps);
  HMODULE hmod = LoadLibrary(L"netshell.dll");
  if (!hmod) 
    return false;

  LPNcFreeNetconProperties NcFreeNetconProperties =
    (LPNcFreeNetconProperties)GetProcAddress(hmod, "NcFreeNetconProperties");

  if (!NcFreeNetconProperties )
    return false;

  INetConnectionManager * pMan = 0;

  HRESULT hres = CoCreateInstance(CLSID_ConnectionManager,
                  0,
                  CLSCTX_ALL,
                  __uuidof(INetConnectionManager),
                  (void**)&pMan);

  if (SUCCEEDED(hres))  
  {    
      IEnumNetConnection * pEnum = 0;
      hres = pMan->EnumConnections(NCME_DEFAULT, &pEnum);
     if (SUCCEEDED(hres)) 
     {
         INetConnection * pCon = 0;
         ULONG count;
         bool done = false;
         while (pEnum->Next(1, &pCon, &count) == S_OK && !done)
         {
             NETCON_PROPERTIES * pProps = 0;
             hres = pCon->GetProperties(&pProps);
             if (SUCCEEDED(hres)) 
             {
                 if (wcscmp(pProps-pszwDeviceName, AdapterName) == 0)
                 {
                     if (bEnable)
                         result = (pCon->Connect() == S_OK);
                     else
                         result = (pCon->Disconnect() == S_OK);
                     done = true;
                 }

                 NcFreeNetconProperties(pProps);
              }
              pCon->Release();
         }
         pEnum->Release();
     }
    pMan->Release();
  }
  FreeLibrary(hmod);
  CoUninitialize();

它很好地禁用和启用了网卡,但是设置了自动配置IPv4值,而不是注册表中的静态值.奇怪的是,它对于DHCP连接正确起作用,但对于静态连接却不起作用.

It's disabling and enabling the network card very well, BUT the autoconfiguration IPv4 values are getting set instead of the static values in the registry. This strangely works properly for DHCP connection but not for static connection.

注意:我什至尝试使用SetIfEntry,但是它无法禁用或启用网卡.

请提出我做错了什么或缺少的事情.

Please suggest where I am doing wrong or anything I am missing.

推荐答案

Windows VISTA和Win7是否支持INetConnectionManager?我已经实现了与您在此处编写的代码相同的代码,但是当应用程序以非管理员登录身份运行时,我无法通过pCon-> Connect访问.因此,看起来我们需要使用COM Moniker来提升com对象.

Does INetConnectionManager supported on Windows VISTA and Win7? I have implemented the same code what you have written here but I get access denied for pCon->Connect when application runs on non admin login. Therefore, it looks like that we need to elevate the com object using COM Moniker.

问候 IP_电话

这篇关于启用网络适配器后,将设置自动配置IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆