Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
getnetworkparams (iphlpapi)
 
.
Summary
Retrieve network parameters for the local computer

C# Signature:

[DllImport("iphlpapi.dll", CharSet=CharSet.Ansi)]
public static extern int GetNetworkParams(IntPtr pFixedInfo, ref UInt32 pBufOutLen);

VB.NET Signature:

    <DllImport("iphlpapi.dll", CharSet:=CharSet.Ansi)>
    Public Shared Function GetNetworkParams(pFixedInfo As IntPtr, ByRef pBufOutLen As Integer) As Integer
    End Function

User-Defined Types:

None.

Alternative Managed API:

Use System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().

Note that ScopeId is renamed to DhcpScopeName and EnableProxy is renamed to IsWinsProxy.

Notes:

Returns a FIXED_INFO struct.

Tips & Tricks:

Please add some!

Sample Code:

IntPtr infoPtr = IntPtr.Zero;

int infoLen = Marshal.SizeOf(typeof(FIXED_INFO));

int ret;

while (true)

{

    infoPtr = Marshal.AllocHGlobal(Convert.ToInt32(infoLen));
    ret = GetNetworkParams(infoPtr, ref infoLen);

    if (ret == ERROR_BUFFER_OVERFLOW)
    {
        //try again w/ bigger buffer:
        Marshal.FreeHGlobal(infoPtr);
        continue;
    }

    if (ret == ERROR_SUCCESS)
        break;

    throw new ApplicationException("An error occurred while fetching adapter information.", new Win32Exception(Convert.ToInt32(ret)));

}

FIXED_INFO info = (FIXED_INFO)Marshal.PtrToStructure(infoPtr, typeof(FIXED_INFO));

VB.NET Example

    Private Const ERROR_SUCCESS As Long = 0
    Private Const ERROR_BUFFER_OVERFLOW As Integer = 111

    Private Const MAX_HOSTNAME_LEN As Integer = 128
    Private Const MAX_SCOPE_ID_LEN As Integer = 256
    Private Const MAX_DOMAIN_NAME_LEN As Integer = 128

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
    Private Structure FIXED_INFO
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_HOSTNAME_LEN + 4)>
    Public HostName As String
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_DOMAIN_NAME_LEN + 4)>
    Public DomainName As String
    Public CurrentDnsServer As IntPtr
    Public DnsServerList As IP_ADDR_STRING
    Public NodeType As UInteger
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_SCOPE_ID_LEN + 4)>
    Public ScopeId As String
    Public EnableRouting As Boolean
    Public EnableProxy As Boolean
    Public EnableDns As Boolean
    End Structure

    <DllImport("iphlpapi.dll", CharSet:=CharSet.Ansi)>
    Private Shared Function GetNetworkParams(pFixedInfo As IntPtr, ByRef pBufOutLen As Integer) As Integer
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim infoPtr As IntPtr = IntPtr.Zero
    Dim infoLen As Integer
    '      Dim bufOF As Boolean = True
    Dim ret As Integer

    Try
        '       Do While bufOF
        ret = GetNetworkParams(infoPtr, infoLen)
        If ret = ERROR_BUFFER_OVERFLOW Then
        infoPtr = Marshal.AllocHGlobal(infoLen)
        ret = GetNetworkParams(infoPtr, infoLen)
        End If

        If ret = ERROR_BUFFER_OVERFLOW Then
        ElseIf ret = ERROR_SUCCESS Then
        Dim info As FIXED_INFO = Marshal.PtrToStructure(infoPtr, GetType(FIXED_INFO))
        ' We can now access the FIXED_INFO fields here.
        Console.WriteLine("Enable Routing: {0}", info.EnableRouting) ' CBool(info.EnableRouting))
        Else
        Dim errorMessage As String = New Win32Exception(ret).Message
            Console.WriteLine("Error {0}", errorMessage)
        End If
        '   Loop

    Catch ex As Exception
        Console.WriteLine(ex.ToString)
    Finally
        'Try again w/ bigger buffer
        Marshal.FreeHGlobal(infoPtr)
    End Try
    End Sub

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions