1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- // 必要的头文件和lib;
- #include <lm.h>
- #pragma comment(lib,"Netapi32.lib")
- bool SetNetUser( LPCTSTR lpAccount, BOOL bDisable )
- {
- if (lpAccount == NULL || lpAccount[0] == '\0' )
- return FALSE;
-
- DWORD dwLevel = 1008;
- USER_INFO_1008 ui1008;
- NET_API_STATUS nStatus;
- if ( bDisable )
- {// 禁用用户;
- ui1008.usri1008_flags = UF_SCRIPT | UF_ACCOUNTDISABLE;
- }
- else
- {// 启用用户;
- ui1008.usri1008_flags = UF_SCRIPT;
- }
-
- // 设置用户启停;
- nStatus = NetUserSetInfo(NULL, lpAccount, dwLevel, (LPBYTE)&ui1008, NULL);
-
- if ( nStatus == NERR_Success )
- return TRUE;
-
- return FALSE;
- }
- void test()
- {
- TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
- _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
-
- if ( SetNetUser(_T("Guest"), FALSE) )
- {
- _tprintf(_T("启动Guest用户成功"));
- system("pause");
- }
-
- if ( SetNetUser(_T("Guest"), TRUE) )
- {
- _tprintf(_T("禁用Guest用户成功"));
- system("pause");
- }
-
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- }
|