有什么方法可以发现本地网络接口(interface)的地址是通过DHCP分配的还是通过Java静态设置的?

最佳答案

因此,正如您仅要求Win NT``解决方案''一样,这是我的代码。它列出了具有当前配置值的网络接口(interface)。

注意EnableDHCP注册表项值,我认为这是重点。

正如我在您的问题下的评论中已经提到的那样,您需要最少简单的JNI包装器。

希望这可以帮助。

更多信息在这里:http://support.microsoft.com/kb/314053

#include <windows.h>

#define NETCARD_ROOT    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards"
#define TCPIP_ROOT  L"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces"

int _tmain(int argc, _TCHAR* argv[])
{

    //First enumerate all network adapters

    HKEY hNetCardsKey;
    LSTATUS lStatus = ERROR_SUCCESS;

    lStatus = RegOpenKey(HKEY_LOCAL_MACHINE,
        NETCARD_ROOT,
        &hNetCardsKey);

    if(ERROR_SUCCESS == lStatus)
    {
        DWORD dwCards = 0L;
        DWORD dwMaxSubkeyNameLen = 0L;
        lStatus = RegQueryInfoKey(hNetCardsKey, NULL, NULL, NULL, &dwCards,
            &dwMaxSubkeyNameLen, NULL, NULL, NULL, NULL, NULL, NULL);

        if(ERROR_SUCCESS == lStatus && dwCards)
        {
            for(DWORD i = 0; i < dwCards; i++)
            {
                TCHAR wszCurrentCardIdxName[MAX_PATH];
                wszCurrentCardIdxName[0] = '\0';
                lStatus = RegEnumKey(hNetCardsKey, i,
                    wszCurrentCardIdxName, MAX_PATH);

                if(ERROR_SUCCESS == lStatus)
                {
                    TCHAR wszAdapterKeyName[MAX_PATH];
                    wszAdapterKeyName[0] = '\0';

                    wsprintf(wszAdapterKeyName, L"%s\\%s", NETCARD_ROOT,
                        wszCurrentCardIdxName);

                    HKEY hCardNameKey;

                    lStatus = RegOpenKey(
                        HKEY_LOCAL_MACHINE,
                        wszAdapterKeyName,
                        &hCardNameKey);

                    if(ERROR_SUCCESS == lStatus)
                    {
                        TCHAR wszServiceNameGuid[MAX_PATH];
                        TCHAR wszAdapterName[MAX_PATH];

                        DWORD dwSize = sizeof(wszServiceNameGuid);
                        wszServiceNameGuid[0] = '\0';
                        RegQueryValueEx(
                            hCardNameKey,
                            L"ServiceName",
                            NULL,
                            NULL,
                            (LPBYTE)wszServiceNameGuid,
                            &dwSize);

                        dwSize = sizeof(wszAdapterName);
                        RegQueryValueEx(
                            hCardNameKey,
                            L"Description",
                            NULL,
                            NULL,
                            (LPBYTE)wszAdapterName,
                            &dwSize);

                        OutputDebugStringW(wszServiceNameGuid);
                        OutputDebugStringW(L"\n");

                        RegCloseKey(hCardNameKey);

                        //Get parameters
                        TCHAR wszCardParamKey[MAX_PATH];
                        wszCardParamKey[0] = '\0';
                        wsprintf(wszCardParamKey,L"%s\\%s", TCPIP_ROOT, wszServiceNameGuid);

                        HKEY hParamKey = NULL;

                        lStatus = RegOpenKey(
                            HKEY_LOCAL_MACHINE,
                            wszCardParamKey,
                            &hParamKey);

                        if(ERROR_SUCCESS == lStatus)
                        {
                            DWORD dwEnabledDHCP = 0L;
                            DWORD dwDWSize = sizeof(DWORD);
                            TCHAR wszStaticIP[32];
                            TCHAR wszDHCPIP[32];
                            DWORD dwIPSize = sizeof(wszDHCPIP);

                            ZeroMemory(wszDHCPIP, dwIPSize);
                            ZeroMemory(wszStaticIP, dwIPSize);

                            lStatus = RegQueryValueEx(
                                hParamKey,
                                L"EnableDHCP",
                                NULL, NULL,
                                (LPBYTE)&dwEnabledDHCP,
                                &dwDWSize);

                            if(SUCCEEDED(lStatus))
                            {
                                wprintf_s(L"Adapter : %s [%s] \n\tDHCP : %s\n",
                                    wszServiceNameGuid,
                                    wszAdapterName,
                                    dwEnabledDHCP
                                    ? L"Yes" : L"No");
                            }

                            lStatus = RegQueryValueEx(
                                hParamKey,
                                L"IPAddress",
                                NULL,
                                NULL,
                                (LPBYTE)&wszStaticIP,
                                &dwIPSize);

                            if(SUCCEEDED(lStatus))
                            {
                                wprintf_s(L"\tConfigured IP Address : %s\n", wszStaticIP);
                            }

                            dwIPSize = sizeof(wszDHCPIP);
                            lStatus = RegQueryValueEx(
                                hParamKey,
                                L"DhcpIPAddress",
                                NULL,
                                NULL,
                                (LPBYTE)&wszDHCPIP,
                                &dwIPSize);

                            if(SUCCEEDED(lStatus))
                            {
                                wprintf_s(L"\tDHCP IP Address : %s\n", wszDHCPIP);
                            }

                            wprintf_s(L"\n");

                            RegCloseKey(hParamKey);
                        }

                    }
                }
            }
        }


        RegCloseKey(hNetCardsKey);
    }


    return 0;
}

简单输出:
Adapter : {6EC2554F-3359-43A2-AADB-57F427DC72FC} [Marvell Yukon 88E8072 PCI-E Gigabit Ethernet Controller]
        DHCP : No
        Configured IP Address : 192.168.5.10
        DHCP IP Address : 192.168.1.102

Adapter : {2A28BDA8-ED1D-4E6E-8990-485EE1836828} [Sony Ericsson Device 0016 USB Ethernet Emulation (NDIS 5)]
        DHCP : Yes
        Configured IP Address :
        DHCP IP Address : 0.0.0.0

Adapter : {491DC095-155F-4572-B975-2E1703C17632} [Microsoft Windows Mobile Remote Adapter]
        DHCP : Yes
        Configured IP Address :
        DHCP IP Address : 169.254.2.2

Adapter : {5F987E64-E804-42DA-9453-8E479B6FC835} [Broadcom 802.11b/g Network adapter]
        DHCP : Yes
        Configured IP Address :
        DHCP IP Address : 192.168.1.14

10-05 19:09