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

查看:288
本文介绍了启用网络适配器后,自动配置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值被设置而不是注册表中的静态值。

Its 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.

感谢和回答,

Vinayak

推荐答案

支持INetConnectionManager Windows VISTA和Win7?我实现了相同的代码,你写在这里,但我得到访问被拒绝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_Telephony

Regards IP_Telephony

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

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