1212
1313#define WIN32_NO_STATUS
1414#include <stdarg.h>
15+ #include <stdlib.h>
1516#include <windef.h>
1617#include <winbase.h>
1718#include <winnls.h>
2122#include <stdio.h>
2223#include <tchar.h>
2324#include <time.h>
25+ //#include <winsock2.h>
26+ //#include <iptypes.h>
2427#include <iphlpapi.h>
2528#include <ndk/rtlfuncs.h>
2629#include <inaddr.h>
2730#include <windns.h>
2831#include <windns_undoc.h>
32+ #include <dhcpcsdk.h>
33+ #include <dhcpcapi.h>
2934#include <strsafe.h>
3035#include <conutils.h>
3136
3237#include "resource.h"
3338
39+ #define NDEBUG
40+ #include <debug.h>
41+
3442typedef struct _RECORDTYPE
3543{
3644 WORD wRecordType ;
@@ -220,6 +228,25 @@ VOID DoFormatMessage(LONG ErrorCode)
220228 }
221229}
222230
231+ LPWSTR
232+ GetUnicodeAdapterName (
233+ _In_ LPSTR pszAnsiName )
234+ {
235+ LPWSTR pszUnicodeName ;
236+ int i , len ;
237+
238+ len = strlen (pszAnsiName );
239+ pszUnicodeName = HeapAlloc (GetProcessHeap (), 0 , (len + 1 ) * sizeof (WCHAR ));
240+ if (pszUnicodeName == NULL )
241+ return NULL ;
242+
243+ for (i = 0 ; i < len ; i ++ )
244+ pszUnicodeName [i ] = (WCHAR )pszAnsiName [i ];
245+ pszUnicodeName [i ] = UNICODE_NULL ;
246+
247+ return pszUnicodeName ;
248+ }
249+
223250VOID
224251GetAdapterFriendlyName (
225252 _In_ LPSTR lpClass ,
@@ -809,24 +836,6 @@ MatchWildcard(
809836 return TRUE;
810837}
811838
812- static
813- VOID
814- BuildAdapterMap (
815- PIP_ADAPTER_INDEX_MAP pAdapterMap ,
816- PIP_ADAPTER_INFO pAdapterInfo )
817- {
818- int i , l1 , l2 ;
819-
820- pAdapterMap -> Index = pAdapterInfo -> Index ;
821-
822- wcscpy (pAdapterMap -> Name , L"\\DEVICE\\TCPIP_" );
823- l1 = wcslen (pAdapterMap -> Name );
824- l2 = strlen (pAdapterInfo -> AdapterName );
825- for (i = 0 ; i < l2 ; i ++ )
826- pAdapterMap -> Name [i + l1 ] = (WCHAR )pAdapterInfo -> AdapterName [i ];
827- pAdapterMap -> Name [i + l1 ] = UNICODE_NULL ;
828- }
829-
830839VOID
831840Release (
832841 LPWSTR pszAdapterName )
@@ -836,9 +845,10 @@ Release(
836845 ULONG adaptOutBufLen = 0 ;
837846 ULONG ret = 0 ;
838847 WCHAR szFriendlyName [MAX_PATH ];
848+ WCHAR szUnicodeAdapterName [MAX_ADAPTER_NAME_LENGTH + 4 ];
839849 MIB_IFROW mibEntry ;
840- IP_ADAPTER_INDEX_MAP AdapterMap ;
841850 BOOL bFoundAdapter = FALSE;
851+ DWORD dwVersion ;
842852
843853 ConResPrintf (StdOut , IDS_HEADER );
844854
@@ -864,6 +874,8 @@ Release(
864874 goto done ;
865875 }
866876
877+ DhcpCApiInitialize (& dwVersion );
878+
867879 pAdapter = pAdapterInfo ;
868880
869881 while (pAdapter )
@@ -884,10 +896,11 @@ Release(
884896 {
885897 if (strcmp (pAdapter -> IpAddressList .IpAddress .String , "0.0.0.0" ))
886898 {
887- BuildAdapterMap (& AdapterMap , pAdapter );
899+ mbstowcs (szUnicodeAdapterName , pAdapter -> AdapterName , strlen (pAdapter -> AdapterName ) + 1 );
900+ DPRINT1 ("AdapterName: %S\n" , szUnicodeAdapterName );
888901
889- /* Call IpReleaseAddress to release the IP address on the specified adapter. */
890- ret = IpReleaseAddress ( & AdapterMap );
902+ /* Call DhcpReleaseParameters to release the IP address on the specified adapter. */
903+ ret = DhcpReleaseParameters ( szUnicodeAdapterName );
891904 if (ret != NO_ERROR )
892905 {
893906 ConResPrintf (StdOut , IDS_DHCPRELEASEERROR , szFriendlyName );
@@ -913,6 +926,8 @@ Release(
913926 pAdapter = pAdapter -> Next ;
914927 }
915928
929+ DhcpCApiCleanup ();
930+
916931 if (bFoundAdapter == FALSE)
917932 {
918933 ConResPrintf (StdOut , IDS_DHCPNOADAPTER );
@@ -936,9 +951,10 @@ Renew(
936951 ULONG adaptOutBufLen = 0 ;
937952 ULONG ret = 0 ;
938953 WCHAR szFriendlyName [MAX_PATH ];
954+ WCHAR szUnicodeAdapterName [MAX_ADAPTER_NAME_LENGTH + 4 ];
939955 MIB_IFROW mibEntry ;
940- IP_ADAPTER_INDEX_MAP AdapterMap ;
941956 BOOL bFoundAdapter = FALSE;
957+ DWORD dwVersion ;
942958
943959 ConResPrintf (StdOut , IDS_HEADER );
944960
@@ -964,6 +980,8 @@ Renew(
964980 goto done ;
965981 }
966982
983+ DhcpCApiInitialize (& dwVersion );
984+
967985 pAdapter = pAdapterInfo ;
968986
969987 while (pAdapter )
@@ -982,10 +1000,11 @@ Renew(
9821000 {
9831001 if (pAdapter -> DhcpEnabled )
9841002 {
985- BuildAdapterMap (& AdapterMap , pAdapter );
1003+ mbstowcs (szUnicodeAdapterName , pAdapter -> AdapterName , strlen (pAdapter -> AdapterName ) + 1 );
1004+ DPRINT1 ("AdapterName: %S\n" , szUnicodeAdapterName );
9861005
987- /* Call IpRenewAddress to renew the IP address on the specified adapter. */
988- ret = IpRenewAddress ( & AdapterMap );
1006+ /* Call DhcpAcquireParameters to renew the IP address on the specified adapter. */
1007+ ret = DhcpAcquireParameters ( szUnicodeAdapterName );
9891008 if (ret != NO_ERROR )
9901009 {
9911010 ConResPrintf (StdOut , IDS_DHCPRENEWERROR , szFriendlyName );
@@ -1006,6 +1025,8 @@ Renew(
10061025 pAdapter = pAdapter -> Next ;
10071026 }
10081027
1028+ DhcpCApiCleanup ();
1029+
10091030 if (bFoundAdapter == FALSE)
10101031 {
10111032 ConResPrintf (StdOut , IDS_DHCPNOADAPTER );
@@ -1239,7 +1260,7 @@ int wmain(int argc, wchar_t *argv[])
12391260 }
12401261 else if (!_tcsnicmp (& argv [1 ][1 ], _T ("ALL" ), _tcslen (& argv [1 ][1 ])))
12411262 {
1242- DoAll = TRUE;
1263+ DoAll = TRUE;
12431264 }
12441265 else if (!_tcsnicmp (& argv [1 ][1 ], _T ("RELEASE" ), _tcslen (& argv [1 ][1 ])))
12451266 {
0 commit comments