如何通过DhcpSAPI启用DHCP子网

时间:2010-11-22 13:18:51

标签: c++ visual-c++ winapi dhcp

我正在编写一个模块来管理与模块所在服务位于同一位置的DHCP服务器。

我使用能够创建子网并添加DHCP预留的DHCP Server API代码。我似乎无法做的实际上是启用/激活子网范围。

我原以为DhcpSetSubnetInfo( )会将SubnetState结构的DHCP_SUBNET_INFO字段设置为DhcpSubnetEnabled,但这似乎无效。

扫描其余的DHCP服务器API我看不到任何其他配置子网/范围的方法。

有人设法做到了吗?

感谢您的帮助。

尼克。

编辑:

static bool enableSubnet( 
                    const std::wstring& server,
                    DWORD               dwSubnet
                    )
{
    LPDHCP_SUBNET_INFO pInfo = NULL;

    DWORD res = DhcpGetSubnetInfo(
                        server.c_str( ),
                        dwSubnet,
                        &pInfo
                        );

    if ( res != ERROR_SUCCESS )
    {
        DhcpRpcFreeMemory( pInfo );

        return false;
    }

    if ( pInfo->SubnetState == DhcpSubnetEnabled )
    {
        DhcpRpcFreeMemory( pInfo );

        return true;
    }

    DHCP_SUBNET_INFO info( *pInfo );

    info.SubnetState = DhcpSubnetDisabled;

    res = DhcpCreateSubnet( server.c_str( ), dwSubnet, &info );

    DhcpRpcFreeMemory( pInfo );

    if ( res != ERROR_SUCCESS )
    {
        return false;
    }

    res = DhcpGetSubnetInfo(
                        server.c_str( ),
                        dwSubnet,
                        &pInfo
                        );

    if ( res != ERROR_SUCCESS )
    {
        DhcpRpcFreeMemory( pInfo );

        return false;
    }

    bool retVal = ( pInfo->SubnetState == DhcpSubnetEnabled );

    if ( !retVal )
    {
        std::wcerr << L"Failed to enable subnet";
    }

    DhcpRpcFreeMemory( pInfo );

    return retVal;

}

调试代码,所有DhcpXX函数都通过,但是在检查时函数返回false:

    bool retVal = ( pInfo->SubnetState == DhcpSubnetEnabled );

1 个答案:

答案 0 :(得分:0)

您是否尝试过如上所述设置DhcpCreateSubnet标记来调用DhcpSubnetEnabled?您的代码可能已经执行此操作 - 发布无法创建和启用子网的部分。

确保检查所有Windows API调用是否也有错误。同样,一些代码可以帮助确定可能失败的内容。