c# - DhcpEnumFilterV4 (P/Invoke) always reports ERROR_NO_MORE_ITEMS -


i try programmatically enumerate dhcp filters on windows 2012 r2 dhcp server. using p/invoke, code looks like:

public const uint error_success = 0; public const uint error_more_data = 234; public const uint error_no_more_items = 259;  public const int max_pattern_length = 255;  [structlayout(layoutkind.sequential, charset = charset.unicode)] public struct dhcp_addr_pattern {     public bool matchhwtype;     public byte hwtype;     public bool iswildcard;     public byte length;     [marshalas(unmanagedtype.byvalarray, sizeconst = max_pattern_length)]     public byte[] pattern; }  [structlayout(layoutkind.sequential, charset = charset.unicode)] public struct dhcp_filter_enum_info {     public uint numelements;     public intptr penumrecords; }  public enum dhcp_filter_list_type : uint {      deny = 0x1,     allow = 0x2 }  [structlayout(layoutkind.sequential, charset = charset.unicode)] public struct dhcp_filter_record {     public dhcp_addr_pattern addrpatt;     public string comment; }   [dllimport("dhcpsapi.dll", setlasterror = true, charset = charset.unicode)] public static extern uint dhcpenumfilterv4(string serveripaddress,     ref dhcp_addr_pattern resumehandle, uint preferredmaximum,      dhcp_filter_list_type listtype, out intptr enumfilterinfo,     out uint elementsread, out uint elementstotal);  public static ienumerable<dhcp_filter_record> dhcpenumfilterv4(         string serveripaddress, dhcp_filter_list_type listtype,          uint preferredmaximum = 1024) {     uint cntread = 0;     uint cnttotal = 0;     uint error = error_success;     var hresume = new dhcp_addr_pattern();     var data = intptr.zero;     var size = marshal.sizeof(typeof(dhcp_filter_record));      {         error = dhcpenumfilterv4(serveripaddress, ref hresume,             preferredmaximum, listtype, out data, out cntread,              out cnttotal);         //         // problem occurs here: 'error' 259         //         if ((error == error_success) || (error == error_more_data)) {             var array = data.tostructure<dhcp_filter_enum_info>();             (uint = 0; < array.numelements; ++i) {                 var ptr = new intptr((long) array.penumrecords + * size);                 var obj = (dhcp_filter_record) marshal.ptrtostructure(ptr, typeof(dhcp_filter_record));                 yield return obj;             }              dhcprpcfreememory(array.penumrecords);             dhcprpcfreememory(data);             data = intptr.zero;          } else if (error != error_no_more_items) {             debug.assert(data == intptr.zero);             throw new win32exception((int) error);         }      } while (error == error_more_data); }  [dllimport("dhcpsapi.dll", setlasterror = true)] public static extern void dhcprpcfreememory(intptr bufferpointer); 

the documentation (http://msdn.microsoft.com/en-us/library/windows/desktop/dd897526(v=vs.85).aspx) of whole dhcp apis imho bit sketchy, not sure whether doing right thing.

the problem is: never results, dhcpenumfilterv4 returns error_no_more_items. suggestions?

i stumbled on important user comment regarding dhcp_filter_list_type in msdn (http://msdn.microsoft.com/en-us/library/windows/desktop/dd897586(v=vs.85).aspx). seems definition of enumeration in msdn wrong. following

typedef enum  {      deny   = 0x1, // wrong!     allow  = 0x2  // wrong! } dhcp_filter_list_type; 

should be

typedef enum  {      deny   = 0x0, // correct!     allow  = 0x1  // correct! } dhcp_filter_list_type; 

using updated constants, code works.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -