代码启停Guest用户.cpp 983 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // 必要的头文件和lib;
  2. #include <lm.h>
  3. #pragma comment(lib,"Netapi32.lib")
  4. bool SetNetUser( LPCTSTR lpAccount, BOOL bDisable )
  5. {
  6. if (lpAccount == NULL || lpAccount[0] == '\0' )
  7. return FALSE;
  8. DWORD dwLevel = 1008;
  9. USER_INFO_1008 ui1008;
  10. NET_API_STATUS nStatus;
  11. if ( bDisable )
  12. {// 禁用用户;
  13. ui1008.usri1008_flags = UF_SCRIPT | UF_ACCOUNTDISABLE;
  14. }
  15. else
  16. {// 启用用户;
  17. ui1008.usri1008_flags = UF_SCRIPT;
  18. }
  19. // 设置用户启停;
  20. nStatus = NetUserSetInfo(NULL, lpAccount, dwLevel, (LPBYTE)&ui1008, NULL);
  21. if ( nStatus == NERR_Success )
  22. return TRUE;
  23. return FALSE;
  24. }
  25. void test()
  26. {
  27. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  28. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  29. if ( SetNetUser(_T("Guest"), FALSE) )
  30. {
  31. _tprintf(_T("启动Guest用户成功"));
  32. system("pause");
  33. }
  34. if ( SetNetUser(_T("Guest"), TRUE) )
  35. {
  36. _tprintf(_T("禁用Guest用户成功"));
  37. system("pause");
  38. }
  39. _tsetlocale(LC_CTYPE, old_locale);
  40. free(old_locale);//还原区域设定;
  41. }