【问题标题】:How to enable a DHCP subnet through DhcpSAPI如何通过 DhcpSAPI 启用 DHCP 子网
【发布时间】:2010-11-22 13:18:50
【问题描述】:

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

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

我曾假设DhcpSetSubnetInfo( ) 会在将DHCP_SUBNET_INFO 结构的SubnetState 字段设置为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 );

【问题讨论】:

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


    【解决方案1】:

    您是否尝试过如上所述设置DhcpSubnetEnabled 标志来调用DhcpCreateSubnet?可能您的代码已经这样做了 - 发布无法创建和启用子网的部分。

    确保您也检查所有 Windows API 调用是否存在错误。同样,一些代码将有助于确定可能失败的原因。

    【讨论】:

    • 嘿史蒂夫,我已经编辑了原始帖子以包含代码。如果子网最初不存在,则启用它创建。我还尝试在现有子网上使用 DhcpSetSubnetInfo()。仍然无法启用它......我想知道它是否不可能!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多