?
Solved

NetUserGetInfo and NetWkstaGetUserInfo access violations

Posted on 2000-03-19
6
Medium Priority
?
476 Views
Last Modified: 2008-03-10
I have the following code:
      DWORD currentuserpriv;
      LPWKSTA_USER_INFO_0 currentuser = NULL;
      LPUSER_INFO_1 buffer = NULL;
      NetWkstaUserGetInfo(NULL, DWORD(0), (LPBYTE *)&currentuser);
      NetUserGetInfo(NULL, LPCWSTR(currentuser->wkui0_username),DWORD(1), (LPBYTE *)&buffer);
      currentuserpriv = buffer->usri1_priv;
      MessageBox(currentuser->wkui0_username, 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_username.  How do I fix this.  The code is to check for administrative privileges for a setup program.
0
Comment
Question by:victech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 2633638
I see several problem here:

Where are you checking the return status of NetWkstaUserGetInfo()?

How do you know that currentuser->wkui0_username has a valid username?

How do you know that currentuser is not NULL?

0
 

Author Comment

by:victech
ID: 2633808
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.
0
 
LVL 32

Accepted Solution

by:
jhance earned 400 total points
ID: 2633830
Then I'll post it again since you obviously don't understand what I'm talking about:

How do you know that currentuser->wkui0_username has a valid username?

How do you know that currentuser is not NULL?

You say NetUserGetInfo is crashing.  But you are NOT validating ANY of the input parameters.  Some of these are resultant from the NetWkstaUserGetInfo function.  If it failed then currentuser might still be NULL.  Also currentuser->wkui0_username might also be NULL.

How can anyone offer any further advice until you check these paramters input to the function, which ABSOLUTELY can cause it to crash and verify that they are correct?????
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 32

Expert Comment

by:jhance
ID: 2633894
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(NULL, DWORD(0), (LPBYTE *)&currentuser) != NERR_Success){
            wprintf(L"Error in NetWkstaUserGetInfo\n");
            return -1;
      }
      else{
            wprintf(L"NetWkstaUserGetInfo OK\n");
      }

      if(NetUserGetInfo(NULL, LPCWSTR(currentuser->wkui0_username),DWORD(1), (LPBYTE *)&buffer) != NERR_Success){
            NetApiBufferFree(currentuser);

            wprintf(L"Error in NetUserGetInfo\n");

            return -1;
      }

      currentuserpriv = buffer->usri1_priv;

      wprintf(L"Current user is %s\n", currentuser->wkui0_username);

      if (currentuserpriv != USER_PRIV_ADMIN) {
            wprintf(L"You are not administrator\n");
      }
      else{
            wprintf(L"You are administrator\n");
      }

      NetApiBufferFree(currentuser);
      NetApiBufferFree(buffer);

      return 0;
}
0
 

Author Comment

by:victech
ID: 2634162
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.
0
 
LVL 32

Expert Comment

by:jhance
ID: 2634224
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.  

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question