victech
asked on
NetUserGetInfo and NetWkstaGetUserInfo access violations
I have the following code:
DWORD currentuserpriv;
LPWKSTA_USER_INFO_0 currentuser = NULL;
LPUSER_INFO_1 buffer = NULL;
NetWkstaUserGetInfo(NULL, DWORD(0), (LPBYTE *)¤tuser);
NetUserGetInfo(NULL, LPCWSTR(currentuser->wkui0 _username) ,DWORD(1), (LPBYTE *)&buffer);
currentuserpriv = buffer->usri1_priv;
MessageBox(currentuser->wk ui0_userna me, NULL, MB_OK);
if (currentuserpriv != USER_PRIV_ADMIN) {
MessageBox("You must have Administrative priviliges to run Setup.", "Setup", MB_OK|MB_ICONSTOP);
}
Upon running it, however, it gives me an access violation on NetUserGetInfo's currentuser->wkui0_usernam e. How do I fix this. The code is to check for administrative privileges for a setup program.
DWORD currentuserpriv;
LPWKSTA_USER_INFO_0 currentuser = NULL;
LPUSER_INFO_1 buffer = NULL;
NetWkstaUserGetInfo(NULL, DWORD(0), (LPBYTE *)¤tuser);
NetUserGetInfo(NULL, LPCWSTR(currentuser->wkui0
currentuserpriv = buffer->usri1_priv;
MessageBox(currentuser->wk
if (currentuserpriv != USER_PRIV_ADMIN) {
MessageBox("You must have Administrative priviliges to run Setup.", "Setup", MB_OK|MB_ICONSTOP);
}
Upon running it, however, it gives me an access violation on NetUserGetInfo's currentuser->wkui0_usernam
ASKER
I'm sorry, that doesn't quite answer what I was originally looking for. I just wanted to know why the access violation was being issued.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is this code and it works fine for me.
Don't forget that you should compile this as UNICODE (define both UNICODE and _UNICODE in the project settings) or take special precautions to convert to wchar_t for those functions that are UNICODE only.
The definitions for WKSTA_USER_INFO_X and USER_INFO_X are sometimes confusing because NetWkstaUserGetInfo and NetUserGetInfo are BOTH UNICODE ONLY even though the definitions for their data structures imply that they take LPTSTR they really only take LPWSTR.
If you're mixing and matching UNICODE and ASCII, be very careful with these functions and their parameters.
#include "windows.h"
#include "lm.h"
int main(int argc, char* argv[])
{
DWORD currentuserpriv;
LPWKSTA_USER_INFO_0 currentuser = NULL;
LPUSER_INFO_1 buffer = NULL;
if(NetWkstaUserGetInfo(NUL L, DWORD(0), (LPBYTE *)¤tuser) != NERR_Success){
wprintf(L"Error in NetWkstaUserGetInfo\n");
return -1;
}
else{
wprintf(L"NetWkstaUserGetI nfo OK\n");
}
if(NetUserGetInfo(NULL, LPCWSTR(currentuser->wkui0 _username) ,DWORD(1), (LPBYTE *)&buffer) != NERR_Success){
NetApiBufferFree(currentus er);
wprintf(L"Error in NetUserGetInfo\n");
return -1;
}
currentuserpriv = buffer->usri1_priv;
wprintf(L"Current user is %s\n", currentuser->wkui0_usernam e);
if (currentuserpriv != USER_PRIV_ADMIN) {
wprintf(L"You are not administrator\n");
}
else{
wprintf(L"You are administrator\n");
}
NetApiBufferFree(currentus er);
NetApiBufferFree(buffer);
return 0;
}
Don't forget that you should compile this as UNICODE (define both UNICODE and _UNICODE in the project settings) or take special precautions to convert to wchar_t for those functions that are UNICODE only.
The definitions for WKSTA_USER_INFO_X and USER_INFO_X are sometimes confusing because NetWkstaUserGetInfo and NetUserGetInfo are BOTH UNICODE ONLY even though the definitions for their data structures imply that they take LPTSTR they really only take LPWSTR.
If you're mixing and matching UNICODE and ASCII, be very careful with these functions and their parameters.
#include "windows.h"
#include "lm.h"
int main(int argc, char* argv[])
{
DWORD currentuserpriv;
LPWKSTA_USER_INFO_0 currentuser = NULL;
LPUSER_INFO_1 buffer = NULL;
if(NetWkstaUserGetInfo(NUL
wprintf(L"Error in NetWkstaUserGetInfo\n");
return -1;
}
else{
wprintf(L"NetWkstaUserGetI
}
if(NetUserGetInfo(NULL, LPCWSTR(currentuser->wkui0
NetApiBufferFree(currentus
wprintf(L"Error in NetUserGetInfo\n");
return -1;
}
currentuserpriv = buffer->usri1_priv;
wprintf(L"Current user is %s\n", currentuser->wkui0_usernam
if (currentuserpriv != USER_PRIV_ADMIN) {
wprintf(L"You are not administrator\n");
}
else{
wprintf(L"You are administrator\n");
}
NetApiBufferFree(currentus
NetApiBufferFree(buffer);
return 0;
}
ASKER
That last bit of source code you posted was perfect! Thank you very much, I'm sorry I originally rejected your answer. I'm just a beginner and didn't quite take the time to understand your answer as well as I should have. Thanks again.
Well don't feel bad. The entire set of NetXXX functions in NT are poorly documented and many don't work exactly as "advertised". The biggest issue is the UNICODE vs. non-UNICODE builds of an application that uses them. All of the header files for these use standard string definitions which are fine if you are using UNICODE in your build options.
Where are you checking the return status of NetWkstaUserGetInfo()?
How do you know that currentuser->wkui0_usernam
How do you know that currentuser is not NULL?