How to enable a DHCP subnet through DhcpSAPI
I'm writing a module to manage a DHCP server co-located with the service in which the module exists.
I have code in place using the DHCP Server API which is able to create a subnet and add DHCP reservations. What I don't seem to be able to do is actually enable/activate the subnet scope.
I had assumed that DhcpSetSubnetInfo( )
would do the job with the SubnetState
field of the DHCP_SUBNET_INFO
structure set to DhcpSubnetEnabled
however this seems to have no effect.
Scanning through the rest of the DHCP Server API I can't see any other methods for configuring subnets/scopes.
Has anyone managed to do this?
Thanks for your help.
Nick.
Edit:
static bool enableSubnet(
const std::wstring& server,
DWORD dwSubnet
)
{
LPDHCP_SUBNET_INFO pInfo = NULL;
DWORD res = DhcpGetSubnetInfo(
server.c_str( ),
开发者_JAVA技巧 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;
}
Debugging the code, all of the DhcpXX functions pass, but the function returns false when checking:
bool retVal = ( pInfo->SubnetState == DhcpSubnetEnabled );
Have you tried calling DhcpCreateSubnet
with the DhcpSubnetEnabled
flag set as noted above? Possibly your code already does this - post the part that fails to create and enable the subnet.
Make sure you check all your Windows APIs calls for errors too. Again, some code would help identify what might be failing.
精彩评论