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

我已经使用DHCP Server API编写了代码,该代码能够创建一个子网并添加DHCP保留。我似乎无法执行的操作实际上是启用/激活子网范围。

我以为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 );

最佳答案

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

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

07-24 09:44
查看更多