win32net NetServerEnum BufferSize, Resume

DaveMoore djmoore at uh.edu
Fri Jan 26 22:20:39 EST 2001


I'm working with the Win32 API NetServerEnum function under
ActivePython 2.02.

The parameter prefLen, used by most of the enumeration functions,
specifies the size of the returned data buffer, which is copied to 
a list of dictionaries. The suggested size is 4096.

The Microsoft Platform System Development Kit says that if prefLen
(which they call prefmaxlen) is set to MAX_PREFERRED_LENGTH, the API
call will automagically allocate a buffer of the correct size.
LMCons.h defines MAX_PREFERRED_LENGTH as ((DWORD) -1), which I take to
mean one less than the maximum possible value (I am not a C coder).

If I set prefLen to, say,  -2 or 2147483647, the error 'Not enough
storage is available to process this command.' occurs.

On the other hand, if prefLen is too small, say 128, I only get a
partial list.

On the gripping hand, prefLen=-1 yields the entire list.

Does preflen=-1 result in a buffer of exactly the right size, or the
largest buffer that can be allocated?

NetServerEnum also returns resumeHandle, which supposedly allows you
to recall the function if there is more information. I'd think an
undersized buffer would be exactly what resumeHandle would cover, but
all my calls return zero. When, exactly, does the resume mechanism
kick in?

The code fragment below is based on John Nielson's NetWkstaUserEnum
example in the Win32 Extensions Network Overview. I've deleted
Nielson's initial call to Net...Enum, and just set resumeHandle
nonzero so as to go through the While loop at least once. Would this
be a problem if I did indeed need to resume?

# Begin
from win32net import NetServerEnum
def getservers(domain):
  SV_TYPE_ALL=0xFFFFFFFF
  resumeHandle=1  #Dummy resume handle
  prefLen= -1     #Preferred buffer size
  level=100       #Detail level
  srv=None        #PSDK says this is reserved, must be null.
  while resumeHandle: 
    try:
      (srvrs,total,resH2)=NetServerEnum(srv,level,SV_TYPE_ALL, \  
                                        domain, resumeHandle,  \
                                        prefLen)
      resumeHandle=resH2
      print "resumeHandle = ",resumeHandle," Total = ", total, \ 
            " prefLen=", prefLen
      for entry in srvrs:
        print entry["name"]
    except win32net.error, nerr:
      return nerr
  return "*** done"

print getservers(None)
# End

-- 
Dave Moore == djmoore at uh.edu == I speak for me.



More information about the Python-list mailing list