简体   繁体   English

启用网络适配器时,自动配置 IP 地址设置

[英]On enabling Network adapter, Autoconfiguration IP address getting set

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.我正在 Visual Studio C++ 中为 Windows Vista 和 7 开发一个应用程序,其中我必须为网卡分配静态 IP 地址并建立连接。 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.为此,我在注册表中输入 Ip 值并将启用 DHCP 值设置为 0。然后我需要禁用然后启用网卡以使这些值生效。 For this, I am using "INetConnectionManager" in the following code:为此,我在以下代码中使用了“INetConnectionManager”:

  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();

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.它很好地禁用和启用网卡,但是自动配置 IPv4 值正在设置而不是注册表中的静态值。 This strangely works properly for DHCP connection but not for static connection.这奇怪地适用于 DHCP 连接,但不适用于静态连接。

NOTE: I even tried SetIfEntry for it, but it fails to disable or enable Network Card.注意:我什至尝试过 SetIfEntry,但它无法禁用或启用网卡。

Please suggest where I am doing wrong or anything I am missing.请建议我做错了什么或我遗漏了什么。

Does INetConnectionManager supported on Windows VISTA and Win7? Windows VISTA 和 Win7 是否支持 INetConnectionManager? 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.我已经实现了与您在此处编写的代码相同的代码,但是当应用程序在非管理员登录时运行时,我的 pCon->Connect 访问被拒绝。 Therefore, it looks like that we need to elevate the com object using COM Moniker.因此,看起来我们需要使用 COM Moniker 来提升 com 对象。

Regards IP_Telephony关于 IP_Telephony

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM