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?
NetUserGetInfo_ API call, strange return value
Re: NetUserGetInfo_ API call, strange return value
Well, it's not the easy way, but it works great, thanks
(The variant with NetUserGetInfo_() would be a 4 liner

Here's the (not working) code using NetUserGetInfo_():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
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)
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
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
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:
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