Search code examples
c++windowsauthenticationwinapiuser-accounts

NetUserDel fails if called too soon after NetUserAdd?


My Windows application uses

  • NetUserAdd to create a local user account
  • LookupAccountName to get user sid
  • NetUserGetInfo to get level-2 user info
  • NetUserDel to delete the local user account

In real environment, the application works fine, because nobody would add the user and delete it right away. However, my test does that, so it sporadically fails at NetUserGetInfo or NetUserDel in an unexpected way. There is no error code returned. The application just terminates silently in Windows 7 and Windows 10.

I could imagine that when NetUserAdd returns, the user account might not have been fully constructed yet in the background. Is there a recommended way to check whether a user account is in a stable state before calling NetUserDel?

LookupAccountName always succeeds, so it is not a good guard. NetUserGetInfo itself sporadically fails unexpectedly. I failed to find other API in MSDN documentation (very few examples there).


Solution

  • Instead of using those unstable win32 API, calling DOS command "net user" via system() can circumvent the issue.