NetUserSetInfo() not working on some computers











up vote
-1
down vote

favorite












What im trying to achieve is to reset the BAD_PW_COUNT variable when a another script attempts to login using the LogonUserA API.



For some reason, compiling this script and running it works fine on my machine(the machine the script was compiled on) but not on other machines(Virtual machines). When running the executable, it either always crashes with a exception code of 0x0005(i assume its related to permissions/access denied) or System Error 87. These errors even persists when the executable is ran through a administrative Command prompt and even through PSEXEC running as SYSTEM.



#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "netapi32.lib")
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <lm.h>
#include <sddl.h> /* for ConvertSidToStringSid function */


int wmain(int argc, wchar_t * argv)
{
DWORD dwLevel = 0;

NET_API_STATUS nStatus;

if (argc != 3)
{
fwprintf(stderr, L"Usage: %s \\ServerName UserNamen", argv[0]);
exit(1);
}
dwLevel = 2;
USER_INFO_2 ui;
ui.usri2_bad_pw_count = UF_SCRIPT | UF_LOCKOUT;
nStatus = NetUserSetInfo(NULL,
argv[2],
dwLevel,
(LPBYTE)&ui,
NULL);
if (nStatus == NERR_PasswordTooShort)
fwprintf(stderr, L"User account %s has been disabledn", argv[2]);
else
fprintf(stderr, "A system error has occurred: %dn", nStatus);
return 0;
}


Any help or guidance is greatly appreciated!. Im sort of new to C++ and Windows API in general as i usually dont deal with this sort of field.










share|improve this question
























  • There's nothing in the code you show that is specific to C++, it could be all plain C. And please don't use multiple language tags, only the one you actually are programming in (which in this case could be either c or c++). C and C++ are two very different languages really. And please read about how to ask good questions, as well as this question checklist.
    – Some programmer dude
    Nov 22 at 14:21












  • Error 5 tells you that the account does not have access to that machine. Like System. Error 87 tells you that the USER_INFO_2 struct is not initialized correctly. Use ={} to zero-initialize it and provide required field values, like usri2_priv. The last argument of NetUserSetInfo can tell which field is bad.
    – Hans Passant
    Nov 22 at 14:32






  • 1




    Just as a complete aside: the example on the NetUserSetInfo does exactly what you're asking to do. Trying to do this, using a USER_INFO_2 structure requires calling NetUserGetInfo first, to populate the current values.
    – Petesh
    Nov 22 at 14:35












  • 0x0005 is not an (SEH) exception code. If it crashes, the most common reason is an access violation, with code 0xC0000005. Using an uninitialized variable (ui) that contains pointers can certainly cause an access violation.
    – IInspectable
    Nov 22 at 21:47















up vote
-1
down vote

favorite












What im trying to achieve is to reset the BAD_PW_COUNT variable when a another script attempts to login using the LogonUserA API.



For some reason, compiling this script and running it works fine on my machine(the machine the script was compiled on) but not on other machines(Virtual machines). When running the executable, it either always crashes with a exception code of 0x0005(i assume its related to permissions/access denied) or System Error 87. These errors even persists when the executable is ran through a administrative Command prompt and even through PSEXEC running as SYSTEM.



#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "netapi32.lib")
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <lm.h>
#include <sddl.h> /* for ConvertSidToStringSid function */


int wmain(int argc, wchar_t * argv)
{
DWORD dwLevel = 0;

NET_API_STATUS nStatus;

if (argc != 3)
{
fwprintf(stderr, L"Usage: %s \\ServerName UserNamen", argv[0]);
exit(1);
}
dwLevel = 2;
USER_INFO_2 ui;
ui.usri2_bad_pw_count = UF_SCRIPT | UF_LOCKOUT;
nStatus = NetUserSetInfo(NULL,
argv[2],
dwLevel,
(LPBYTE)&ui,
NULL);
if (nStatus == NERR_PasswordTooShort)
fwprintf(stderr, L"User account %s has been disabledn", argv[2]);
else
fprintf(stderr, "A system error has occurred: %dn", nStatus);
return 0;
}


Any help or guidance is greatly appreciated!. Im sort of new to C++ and Windows API in general as i usually dont deal with this sort of field.










share|improve this question
























  • There's nothing in the code you show that is specific to C++, it could be all plain C. And please don't use multiple language tags, only the one you actually are programming in (which in this case could be either c or c++). C and C++ are two very different languages really. And please read about how to ask good questions, as well as this question checklist.
    – Some programmer dude
    Nov 22 at 14:21












  • Error 5 tells you that the account does not have access to that machine. Like System. Error 87 tells you that the USER_INFO_2 struct is not initialized correctly. Use ={} to zero-initialize it and provide required field values, like usri2_priv. The last argument of NetUserSetInfo can tell which field is bad.
    – Hans Passant
    Nov 22 at 14:32






  • 1




    Just as a complete aside: the example on the NetUserSetInfo does exactly what you're asking to do. Trying to do this, using a USER_INFO_2 structure requires calling NetUserGetInfo first, to populate the current values.
    – Petesh
    Nov 22 at 14:35












  • 0x0005 is not an (SEH) exception code. If it crashes, the most common reason is an access violation, with code 0xC0000005. Using an uninitialized variable (ui) that contains pointers can certainly cause an access violation.
    – IInspectable
    Nov 22 at 21:47













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











What im trying to achieve is to reset the BAD_PW_COUNT variable when a another script attempts to login using the LogonUserA API.



For some reason, compiling this script and running it works fine on my machine(the machine the script was compiled on) but not on other machines(Virtual machines). When running the executable, it either always crashes with a exception code of 0x0005(i assume its related to permissions/access denied) or System Error 87. These errors even persists when the executable is ran through a administrative Command prompt and even through PSEXEC running as SYSTEM.



#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "netapi32.lib")
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <lm.h>
#include <sddl.h> /* for ConvertSidToStringSid function */


int wmain(int argc, wchar_t * argv)
{
DWORD dwLevel = 0;

NET_API_STATUS nStatus;

if (argc != 3)
{
fwprintf(stderr, L"Usage: %s \\ServerName UserNamen", argv[0]);
exit(1);
}
dwLevel = 2;
USER_INFO_2 ui;
ui.usri2_bad_pw_count = UF_SCRIPT | UF_LOCKOUT;
nStatus = NetUserSetInfo(NULL,
argv[2],
dwLevel,
(LPBYTE)&ui,
NULL);
if (nStatus == NERR_PasswordTooShort)
fwprintf(stderr, L"User account %s has been disabledn", argv[2]);
else
fprintf(stderr, "A system error has occurred: %dn", nStatus);
return 0;
}


Any help or guidance is greatly appreciated!. Im sort of new to C++ and Windows API in general as i usually dont deal with this sort of field.










share|improve this question















What im trying to achieve is to reset the BAD_PW_COUNT variable when a another script attempts to login using the LogonUserA API.



For some reason, compiling this script and running it works fine on my machine(the machine the script was compiled on) but not on other machines(Virtual machines). When running the executable, it either always crashes with a exception code of 0x0005(i assume its related to permissions/access denied) or System Error 87. These errors even persists when the executable is ran through a administrative Command prompt and even through PSEXEC running as SYSTEM.



#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "netapi32.lib")
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <lm.h>
#include <sddl.h> /* for ConvertSidToStringSid function */


int wmain(int argc, wchar_t * argv)
{
DWORD dwLevel = 0;

NET_API_STATUS nStatus;

if (argc != 3)
{
fwprintf(stderr, L"Usage: %s \\ServerName UserNamen", argv[0]);
exit(1);
}
dwLevel = 2;
USER_INFO_2 ui;
ui.usri2_bad_pw_count = UF_SCRIPT | UF_LOCKOUT;
nStatus = NetUserSetInfo(NULL,
argv[2],
dwLevel,
(LPBYTE)&ui,
NULL);
if (nStatus == NERR_PasswordTooShort)
fwprintf(stderr, L"User account %s has been disabledn", argv[2]);
else
fprintf(stderr, "A system error has occurred: %dn", nStatus);
return 0;
}


Any help or guidance is greatly appreciated!. Im sort of new to C++ and Windows API in general as i usually dont deal with this sort of field.







c++ c windows winapi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 14:20









Some programmer dude

293k24243403




293k24243403










asked Nov 22 at 14:18









nikki

43




43












  • There's nothing in the code you show that is specific to C++, it could be all plain C. And please don't use multiple language tags, only the one you actually are programming in (which in this case could be either c or c++). C and C++ are two very different languages really. And please read about how to ask good questions, as well as this question checklist.
    – Some programmer dude
    Nov 22 at 14:21












  • Error 5 tells you that the account does not have access to that machine. Like System. Error 87 tells you that the USER_INFO_2 struct is not initialized correctly. Use ={} to zero-initialize it and provide required field values, like usri2_priv. The last argument of NetUserSetInfo can tell which field is bad.
    – Hans Passant
    Nov 22 at 14:32






  • 1




    Just as a complete aside: the example on the NetUserSetInfo does exactly what you're asking to do. Trying to do this, using a USER_INFO_2 structure requires calling NetUserGetInfo first, to populate the current values.
    – Petesh
    Nov 22 at 14:35












  • 0x0005 is not an (SEH) exception code. If it crashes, the most common reason is an access violation, with code 0xC0000005. Using an uninitialized variable (ui) that contains pointers can certainly cause an access violation.
    – IInspectable
    Nov 22 at 21:47


















  • There's nothing in the code you show that is specific to C++, it could be all plain C. And please don't use multiple language tags, only the one you actually are programming in (which in this case could be either c or c++). C and C++ are two very different languages really. And please read about how to ask good questions, as well as this question checklist.
    – Some programmer dude
    Nov 22 at 14:21












  • Error 5 tells you that the account does not have access to that machine. Like System. Error 87 tells you that the USER_INFO_2 struct is not initialized correctly. Use ={} to zero-initialize it and provide required field values, like usri2_priv. The last argument of NetUserSetInfo can tell which field is bad.
    – Hans Passant
    Nov 22 at 14:32






  • 1




    Just as a complete aside: the example on the NetUserSetInfo does exactly what you're asking to do. Trying to do this, using a USER_INFO_2 structure requires calling NetUserGetInfo first, to populate the current values.
    – Petesh
    Nov 22 at 14:35












  • 0x0005 is not an (SEH) exception code. If it crashes, the most common reason is an access violation, with code 0xC0000005. Using an uninitialized variable (ui) that contains pointers can certainly cause an access violation.
    – IInspectable
    Nov 22 at 21:47
















There's nothing in the code you show that is specific to C++, it could be all plain C. And please don't use multiple language tags, only the one you actually are programming in (which in this case could be either c or c++). C and C++ are two very different languages really. And please read about how to ask good questions, as well as this question checklist.
– Some programmer dude
Nov 22 at 14:21






There's nothing in the code you show that is specific to C++, it could be all plain C. And please don't use multiple language tags, only the one you actually are programming in (which in this case could be either c or c++). C and C++ are two very different languages really. And please read about how to ask good questions, as well as this question checklist.
– Some programmer dude
Nov 22 at 14:21














Error 5 tells you that the account does not have access to that machine. Like System. Error 87 tells you that the USER_INFO_2 struct is not initialized correctly. Use ={} to zero-initialize it and provide required field values, like usri2_priv. The last argument of NetUserSetInfo can tell which field is bad.
– Hans Passant
Nov 22 at 14:32




Error 5 tells you that the account does not have access to that machine. Like System. Error 87 tells you that the USER_INFO_2 struct is not initialized correctly. Use ={} to zero-initialize it and provide required field values, like usri2_priv. The last argument of NetUserSetInfo can tell which field is bad.
– Hans Passant
Nov 22 at 14:32




1




1




Just as a complete aside: the example on the NetUserSetInfo does exactly what you're asking to do. Trying to do this, using a USER_INFO_2 structure requires calling NetUserGetInfo first, to populate the current values.
– Petesh
Nov 22 at 14:35






Just as a complete aside: the example on the NetUserSetInfo does exactly what you're asking to do. Trying to do this, using a USER_INFO_2 structure requires calling NetUserGetInfo first, to populate the current values.
– Petesh
Nov 22 at 14:35














0x0005 is not an (SEH) exception code. If it crashes, the most common reason is an access violation, with code 0xC0000005. Using an uninitialized variable (ui) that contains pointers can certainly cause an access violation.
– IInspectable
Nov 22 at 21:47




0x0005 is not an (SEH) exception code. If it crashes, the most common reason is an access violation, with code 0xC0000005. Using an uninitialized variable (ui) that contains pointers can certainly cause an access violation.
– IInspectable
Nov 22 at 21:47

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432933%2fnetusersetinfo-not-working-on-some-computers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432933%2fnetusersetinfo-not-working-on-some-computers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo