Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
NetUserGetGroups and NetUserGetLocalGroups erratic
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Titre:
NetUserGetGroups and NetUserGetLocalGroups erratic
Divers
Thread ID:
00602643
Message ID:
00602643
Vues:
279
I have created a call to NetUserGetGroups and NetUserGetLocalGroups for use in Windows NT/2K/XP. (Thanks to Anatoliy Mogylevets' page for the utility functions.)

It works as follows:
  =Groups("\\myserver","user")  && display global groups
or
  =Groups("\\myserver","user",.T.)  && display local groups
The problem is that it works fine the first time it is called. After that it keeps returning an error even though I am calling it with the exact same parameters. Typically the error is 2221 (user not found) or 1722 (???).

Any ideas what might be going wrong? The code is listed below. Thanks.

Neil
*** GROUPS.PRG ***
PARAMETERS cServer, cUser, local
	
	DECLARE INTEGER NetApiBufferFree IN netapi32 INTEGER Buffer 
	
	DECLARE INTEGER NetUserGetLocalGroups IN netapi32 ;
		string @ servername, ;
		string @ username, ;
		integer lvl, ;
		integer flags, ;
		integer @ buffer, ;
		integer maxlen, ;
		integer @ entriesread, ;
		integer @ total
	
	DECLARE INTEGER NetUserGetGroups IN netapi32 ;
		string @ servername, ;
		string @ username, ;
		integer lvl, ;
		integer @ buffer, ;
		integer maxlen, ;
		integer @ entriesread, ;
		integer @ total

	DECLARE RtlMoveMemory IN kernel32 ;
		STRING @ Destination, INTEGER Source, INTEGER nLength 

	#DEFINE GROUPINFO_0_SIZE         4
	#DEFINE MAX_PREFERRED_LENGTH    -1 
	#DEFINE StrBufferLength   250 
	
	STORE 0 TO lnBuffer, lnEntriesRead, lnTotalEntries
	
	cServer = STRCONV(cServer, 5)
	cUser = STRCONV(cUser,5)
	
	IF local
		lnResult = NetUserGetLocalGroups(cServer , ;
				cUser, ;
				0,;
				0, ;
				@lnBuffer, ;
				MAX_PREFERRED_LENGTH, ;
				@lnEntriesRead , ;
				@lnTotalEntries)
	ELSE
		lnResult = NetUserGetGroups(cServer , ;
				cUser, ;
				0,;
				@lnBuffer, ;
				MAX_PREFERRED_LENGTH, ;
				@lnEntriesRead , ;
				@lnTotalEntries)
	ENDIF
	
	IF lnResult != 0 
		? "Error code:", lnResult 
		RETURN
	ENDIF
	
	lcBuffer = Repli (Chr(0),StrBufferLength)
	= RtlMoveMemory (@lcBuffer, lnBuffer, StrBufferLength) 
	 
	* scanning returned entries
	FOR lnEntry = 1 TO lnEntriesRead 
	    * copying the UserInfo structure to a VFP string 
	    lcGroupInfo = SUBSTR (lcBuffer,; 
	        (lnEntry-1)*GROUPINFO_0_SIZE+1, GROUPINFO_0_SIZE) 
	
	    lpwstrGroupname     = buf2dword (SUBSTR(lcGroupInfo,  1,4)) 
	     
	    * copying data from pointers to VFP strings 
	    lcGroupname     = getStrFromMem (lpwstrGroupname)
	    
	    ? "Member of group:", lcGroupName
	ENDFOR 
	
	* this buffer is allocated by the system and must be freed 
	= NetApiBufferFree (lnBuffer) 
	
RETURN

FUNCTION  getStrFromMem (lnMemBlock) 
	* converting allocated in memory Unicode string to a VFP string 
	    LOCAL lcBuffer 
	    lcBuffer = SPACE(StrBufferLength) 
	    = RtlMoveMemory (@lcBuffer, lnMemBlock, StrBufferLength) 
	    lcBuffer = SUBSTR (lcBuffer, 1, AT(Chr(0)+Chr(0),lcBuffer)-1) 
RETURN  STRTRAN(lcBuffer, Chr(0),"") 

FUNCTION buf2dword (lcBuffer) 
	RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ; 
	       Asc(SUBSTR(lcBuffer, 2,1)) * 256 +; 
	       Asc(SUBSTR(lcBuffer, 3,1)) * 65536 +; 
	       Asc(SUBSTR(lcBuffer, 4,1)) * 16777216 
 
*** end GROUPS.PRG ***
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform