NetUserGetInfo_ API call, strange return value

Just starting out? Need help? Post your questions and find answers here.
Anden
Enthusiast
Enthusiast
Posts: 135
Joined: Mon Jul 21, 2003 7:23 am
Contact:

NetUserGetInfo_ API call, strange return value

Post by Anden »

Does NetUserGetInfo_(..) already work with PB?

Because it doesn't conform to the API:

If the function is successful, it returns NERR_SUCCESS.
If the function fails, the return value is one of the following error codes.

Value Meaning
ERROR_ACCESS_DENIED The user does not have access to the requested information.
NERR_InvalidComputer The computer name is invalid.
NERR_UserNotFound The user name could not be found.

The call ALWAYS returns 123 as result, which isn't one of the values above.

Any other easy way to see if a user has admin privileges or not?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: NetUserGetInfo_ API call, strange return value

Post by PB »

> Any other easy way to see if a user has admin privileges or not?

viewtopic.php?t=9390
Anden
Enthusiast
Enthusiast
Posts: 135
Joined: Mon Jul 21, 2003 7:23 am
Contact:

Re: NetUserGetInfo_ API call, strange return value

Post by Anden »

PB wrote:> Any other easy way to see if a user has admin privileges or not?

viewtopic.php?t=9390
Well, it's not the easy way, but it works great, thanks

(The variant with NetUserGetInfo_() would be a 4 liner :-))
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

remember that most (all) NetApi-functions nees Strings in AnsiWide Mode.
So for every NetApi-FunctionCall you have to translate Purebasic-Strings first to Ansiwidestrings. See my several examples on that topic
SPAMINATOR NR.1
Anden
Enthusiast
Enthusiast
Posts: 135
Joined: Mon Jul 21, 2003 7:23 am
Contact:

Post by Anden »

Rings wrote:remember that most (all) NetApi-functions nees Strings in AnsiWide Mode.
So for every NetApi-FunctionCall you have to translate Purebasic-Strings first to Ansiwidestrings. See my several examples on that topic
Here's the (not working) code using NetUserGetInfo_():

Enumeration
#USER_PRIV_GUEST
#USER_PRIV_USER
#USER_PRIV_ADMIN
EndEnumeration

ui3.USER_INFO_3
usr$ = Space(256)
nsize = 256
GetUserName_(@usr$, @nsize)
usrW$ = Space(512)
MultiByteToWideChar_(#CP_ACP, #MB_PRECOMPOSED, @usr$, -1, @usrW$, 512)

CallDebugger
res.l = NetUserGetInfo_(#NULL, @usrW$, 3, lpBuf.l)
Debug res
If (res = 0) ;#nerr_success
CopyMemory(lpBuf, @ui3, SizeOf(ui3))
Debug ui3\privilege
;If (ui3\privilege = #USER_PRIV_ADMIN)
NetApiBufferFree_(lpBuf)
EndIf

Constants like #nerr_success are not defined within PB, neither are USER_INFO_X (except of USER_INFO_3)
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

This example is not completed, but works in its way.
For More info about the USER_INFO_x structures, see WinApi.HLP oder SDK.
it does the job for me :)

Code: Select all

Enumeration 
#USER_PRIV_GUEST 
#USER_PRIV_USER 
#USER_PRIV_ADMIN 
EndEnumeration 

*ui3.USER_INFO_3 
usr.s = Space(256) 
nsize = Len(usr.s)
GetUserName_(@usr.s, @nsize) 
usrW.s = Space(512) 
Result=MultiByteToWideChar_(#CP_ACP, #MB_PRECOMPOSED, @usr.s, -1, @usrW.s, 512) 
lpBuf.l=0
res.l = NetUserGetInfo_(0, @usrW.s, 3, @lpBuf) 
If res = 0 And lpBuf<>0 ;#nerr_success 
 *ui3=lpBuf
 If *ui3\uName<>0 
  Buffer.s=Space(512)
  Result=WideCharToMultiByte_(#CP_ACP ,0,*ui3\uName,255,@Buffer.s,512,0,0)
  Debug Buffer.s
 EndIf
 
 Debug *ui3\privilege 
  ;If (ui3\privilege = #USER_PRIV_ADMIN) 
 Debug *ui3\LastLogon
 If *ui3\HomeDir<>0 
  Buffer.s=Space(512)
  Result=WideCharToMultiByte_(#CP_ACP ,0,*ui3\HomeDir,255,@Buffer.s,512,0,0)
  Debug Buffer.s
 EndIf
 If *ui3\HomeDirDrive<>0 
  Buffer.s=Space(512)
  Result=WideCharToMultiByte_(#CP_ACP ,0,*ui3\HomeDirDrive,255,@Buffer.s,512,0,0)
  Debug Buffer.s
 EndIf
 If *ui3\FullName<>0
  Buffer.s=Space(512)
  Result=WideCharToMultiByte_(#CP_ACP ,0,*ui3\FullName,255,@Buffer.s,512,0,0)
  Debug Buffer.s
 EndIf
 If *ui3\UserComment<>0
  Buffer.s=Space(512)
  Result=WideCharToMultiByte_(#CP_ACP ,0,*ui3\UserComment,255,@Buffer.s,512,0,0)
  Debug Buffer.s
 EndIf

 Debug *ui3\PasswordAge
 Debug *ui3\CodePage
 Debug *ui3\LogonHours
 
 NetApiBufferFree_(lpBuf) 
EndIf 
SPAMINATOR NR.1
Anden
Enthusiast
Enthusiast
Posts: 135
Joined: Mon Jul 21, 2003 7:23 am
Contact:

Post by Anden »

Thanks for directing me in the right lane, Rings.

Somehow managed to "ignore" the * in the API reference for the *bufptr :-)

NET_API_STATUS NetUserGetInfo(
LPWSTR servername,
LPWSTR username,
DWORD level,
LPBYTE *bufptr
);

Anyway, here's the "easy" IsAdmin() procedure:

Code: Select all

#NERR_SUCCESS = 0
#USER_PRIV_ADMIN = 2

Procedure IsAdmin()
  Result.l = #False
  ui3.USER_INFO_3
  usr$ = Space(256)
  nsize = Len(usr$)
  GetUserName_(@usr$, @nsize)
  usrW$ = Space(512)
  MultiByteToWideChar_(#CP_ACP, #MB_PRECOMPOSED, @usr$, -1, @usrW$, Len(usrW$))
  If (NetUserGetInfo_(#NULL, @usrW$, 3, @lpBuf.l) = #NERR_SUCCESS)
    CopyMemory(lpBuf, @ui3, SizeOf(ui3))
    If (ui3\privilege = #USER_PRIV_ADMIN) : Result = #True : EndIf
    NetApiBufferFree_(lpBuf)
  EndIf
  ProcedureReturn Result
EndProcedure

Post Reply