⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processdlg.cpp

📁 最适合在学校成批地建立删除学生用户或学生文件夹
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					wsprintf(szUserName,"%s%s%d0%d",g_szPrefix,g_szYear,nClass,nNumber);
				else
					wsprintf(szUserName,"%s%s%d%d",g_szPrefix,g_szYear,nClass,nNumber);
			}
			wsprintf(szShowTips,"             %s",szUserName);
			::SendMessage(g_hShowWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szShowTips);
			MultiByteToWideChar(CP_ACP, 0, szUserName, -1, wszUserName, 32);

			nStatus=NetUserGetInfo(NULL,wszUserName,0,(LPBYTE*)&pUI);
			NetApiBufferFree(pUI);
			if(nStatus==NERR_Success)
				nStatus = NetUserDel(NULL,wszUserName);
			if(nStatus!=NERR_Success&&nStatus!=NERR_UserNotFound)
			{
				wsprintf(szShowTips,"删除学生用户%s失败!",szUserName);
				AfxMessageBox(szShowTips);
			}
		}
	}
	if(g_bIsContinue)
		AfxMessageBox("学生用户删除完毕!");
	::SendMessage(g_hExitWnd,WM_COMMAND,WM_DESTROY,0);
}

void CreateDirectories(LPVOID pParam)
{
	NET_API_STATUS nStatus;	
	HANDLE hDir=NULL;
	WIN32_FIND_DATA DirInfo;
	DirInfo.dwFileAttributes=0;
	hDir=FindFirstFile(g_szMainDir,&DirInfo);
	char szClassDir[96]="";
	char szUserDir[96]="";
	char szUserName[32]="";
	WCHAR wszUserName[32];
	char szShowTips[128]="";
	char szClassGroupName[32]="";
	WCHAR wszClassGroupName[32];
	BOOL bClassDirExist=FALSE;
	BOOL bUserDirExist=FALSE;
	if(hDir==INVALID_HANDLE_VALUE||!(DirInfo.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
	{
		wsprintf(szShowTips,"找不到指定主文件夹\"%s\"!",g_szMainDir);
		AfxMessageBox(szShowTips);
		if(hDir!=INVALID_HANDLE_VALUE)
			FindClose(hDir);
		::SendMessage(g_hExitWnd,WM_COMMAND,WM_DESTROY,0);
		return;
	}
	FindClose(hDir);

	PSID pSID, pAdminSID = NULL,*ppClassGroupSID = NULL;

	PACL pACL = NULL;
	PSECURITY_DESCRIPTOR pSD = NULL;
	EXPLICIT_ACCESS ea[3];
	EXPLICIT_ACCESS *pea=NULL;
	SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
	SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
	SECURITY_ATTRIBUTES sa;
	CArray<UINT,UINT>arrClass;
	CArray<char*,char*>arrStr;

	USER_INFO_0 *pUI=NULL;
	LOCALGROUP_INFO_1 *pLGI=NULL;

	for(UINT nClass=g_nClassFrom;nClass<=g_nClassTo&&g_bIsContinue;++nClass)
	{
		char* pTemp;
		pLGI=NULL;
		wsprintf(szClassGroupName,"%s%s级%d班",g_szPrefix,g_szYear,nClass);
		MultiByteToWideChar(CP_ACP, 0, szClassGroupName, -1, wszClassGroupName, 32);
		nStatus=NetLocalGroupGetInfo(NULL,wszClassGroupName,1,(LPBYTE*)&pLGI);
		NetApiBufferFree(pLGI);
		if(nStatus==NERR_Success)
		{
			arrClass.Add(nClass);
			pTemp=new char[32];
			strcpy(pTemp,szClassGroupName);
			arrStr.Add(pTemp);
		}
	}
	// Create a SID for the BUILTIN\Administrators group.
	if(! AllocateAndInitializeSid( &SIDAuthNT, 2,
		SECURITY_BUILTIN_DOMAIN_RID,
		DOMAIN_ALIAS_RID_ADMINS,
		0, 0, 0, 0, 0, 0,
		&pAdminSID) ) 
	{
		wsprintf(szShowTips,"设置主文件夹\"%s\"失败!\n原因: AllocateAndInitializeSid 失败.",g_szMainDir);
		AfxMessageBox(szShowTips);
		goto CleanupAdmin; 
	}
	// Initialize an EXPLICIT_ACCESS structure for an ACE.
	pea=(EXPLICIT_ACCESS*)LocalAlloc(LPTR,(arrClass.GetSize()+1)*sizeof(EXPLICIT_ACCESS)); 
	if(!pea)
	{
		wsprintf(szShowTips,"设置主文件夹\"%s\"失败!\n原因: LocalAlloc 失败.",g_szMainDir);
		AfxMessageBox(szShowTips);
		goto CleanupGroups; 
	}
//	pea=new EXPLICIT_ACCESS[arrClass.GetSize()+1];
//	ZeroMemory(&pea, (arrClass.GetSize()+1) * sizeof(EXPLICIT_ACCESS));
	ppClassGroupSID=new PSID[arrClass.GetSize()];
	// The ACE will allow the Administrators group full access to the key.	
	pea[0].grfAccessPermissions = 0x001F01FF;
	pea[0].grfAccessMode = SET_ACCESS;
	pea[0].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
	pea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
	pea[0].Trustee.TrusteeType = TRUSTEE_IS_USER;
	pea[0].Trustee.ptstrName  = g_szCurrentUser;
	for(nClass=0;nClass<arrClass.GetSize()&&g_bIsContinue;++nClass)
	{
		pea[nClass+1].grfAccessPermissions = 0x001200A9;
		pea[nClass+1].grfAccessMode = SET_ACCESS;
		pea[nClass+1].grfInheritance= NO_INHERITANCE;
		pea[nClass+1].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
		pea[nClass+1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
		pea[nClass+1].Trustee.ptstrName  = arrStr.GetAt(nClass);
		if(! AllocateAndInitializeSid( &SIDAuthWorld, 1,
			SECURITY_WORLD_RID,
			0, 0, 0, 0, 0, 0, 0,
			&ppClassGroupSID[nClass]) )
		{
			wsprintf(szShowTips,"设置主文件夹\"%s\"失败!\n原因: AllocateAndInitializeSid 失败.",g_szMainDir);
			AfxMessageBox(szShowTips );
			goto CleanupGroups;
		}
	}
	// Create a new ACL that contains the new ACEs.
	nStatus=SetEntriesInAcl(arrClass.GetSize()+1, pea, NULL, &pACL);
	if (nStatus!=ERROR_SUCCESS) 
	{
		wsprintf(szShowTips,"设置主文件夹\"%s\"失败!\n原因: SetEntriesInAcl 失败.",g_szMainDir);
		AfxMessageBox(szShowTips);
		goto CleanupGroups;
	}
	nStatus=SetNamedSecurityInfo(g_szMainDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
		NULL,NULL,pACL,NULL);
	if(nStatus!=ERROR_SUCCESS)
	{
		wsprintf(szShowTips,"设置主文件夹\"%s\"的权限失败!\n原因: SetNamedSecurityInfo 失败.",g_szMainDir);
		AfxMessageBox(szShowTips);
	}
CleanupGroups:	
	for(nClass=0;nClass<arrClass.GetSize();++nClass)
	{
		if (ppClassGroupSID[nClass]) 
			FreeSid(ppClassGroupSID[nClass]);
		delete []arrStr.GetAt(nClass);
	}
	arrStr.RemoveAll();
	delete []ppClassGroupSID;
	if(pACL)
		LocalFree(pACL);
	if(pea)
		LocalFree(pea);
//	delete(pea);
	pACL=NULL;
	ZeroMemory(ea, 3 * sizeof(EXPLICIT_ACCESS));
	ea[0].grfAccessPermissions = 0x001F01FF;
	ea[0].grfAccessMode = SET_ACCESS;
	ea[0].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
	ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
	ea[0].Trustee.TrusteeType = TRUSTEE_IS_USER;
	ea[0].Trustee.ptstrName  = g_szCurrentUser;
	for(nClass=0;nClass<arrClass.GetSize()&&g_bIsContinue;++nClass)
	{
		if (pACL) 
		    LocalFree(pACL);
		if(pSID)
			FreeSid(pSID);
		if (pSD) 
			LocalFree(pSD);	
		pACL=NULL;
		pSID=NULL;
		pSD=NULL;
		::SendMessage(g_hShowWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szClassDir);
		wsprintf(szClassDir,"%s\\%d班",g_szMainDir,arrClass.GetAt(nClass));
		wsprintf(szClassGroupName,"%s%s级%d班",g_szPrefix,g_szYear,arrClass.GetAt(nClass));
		DirInfo.dwFileAttributes=0;
		hDir=FindFirstFile(szClassDir,&DirInfo);
		if(hDir==INVALID_HANDLE_VALUE)
			bClassDirExist=FALSE;
		else if(hDir!=INVALID_HANDLE_VALUE&&(DirInfo.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
		{
			bClassDirExist=TRUE;
			FindClose(hDir);
		}
		else
		{
			wsprintf(szShowTips,"创建用户组文件夹\"%s\"失败!\n原因: 存在同名文件.",szClassDir);
			AfxMessageBox(szShowTips);
			FindClose(hDir);
			continue;
		}

		ZeroMemory(ea, 3*sizeof(EXPLICIT_ACCESS));
		ea[0].grfAccessPermissions = 0x001F01FF;
		ea[0].grfAccessMode = SET_ACCESS;
		ea[0].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
		ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
		ea[0].Trustee.TrusteeType = TRUSTEE_IS_USER;
		ea[0].Trustee.ptstrName  = g_szCurrentUser;

		ea[1].grfAccessPermissions = 0x001200A9;
		ea[1].grfAccessMode = SET_ACCESS;
		ea[1].grfInheritance= NO_INHERITANCE;
		ea[1].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
		ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
		ea[1].Trustee.ptstrName  = szClassGroupName;

		if(! AllocateAndInitializeSid( &SIDAuthWorld, 1,
			SECURITY_WORLD_RID,
			0, 0, 0, 0, 0, 0, 0,
			&pSID) )
		{
			wsprintf(szShowTips,"操作用户组文件夹\"%s\"失败!\n原因: AllocateAndInitializeSid 失败.",szClassDir);
			AfxMessageBox( szShowTips);
			continue;
		}
		if (SetEntriesInAcl(2, ea, NULL, &pACL)!=ERROR_SUCCESS) 
		{
			wsprintf(szShowTips,"操作用户组文件夹\"%s\"失败!\n原因: SetEntriesInAcl 失败.",szClassDir);
			AfxMessageBox(szShowTips);
			continue;
		}
		if(bClassDirExist)
		{
			
			nStatus=SetNamedSecurityInfo(szClassDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
				NULL,NULL,pACL,NULL);
			if(nStatus!=ERROR_SUCCESS)
			{
				wsprintf(szShowTips,"设置用户组文件夹\"%s\"的权限失败!\n原因: SetNamedSecurityInfo 失败.",szClassDir);
				AfxMessageBox(szShowTips);
			}
		}
		else
		{
			pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
				SECURITY_DESCRIPTOR_MIN_LENGTH); 
			if (pSD == NULL) 
			{ 
				wsprintf(szShowTips,"创建用户组文件夹\"%s\"失败!\n原因: LocalAlloc 失败.",szClassDir);
				AfxMessageBox(szShowTips);
				continue; 
			} 
			
			if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) 
			{
				wsprintf(szShowTips,"创建用户组文件夹\"%s\"失败!\n原因: InitializeSecurityDescriptor 失败.",szClassDir);
				AfxMessageBox(szShowTips);
				continue; 
			} 
			// Add the ACL to the security descriptor. 
			
			if (!SetSecurityDescriptorDacl(pSD, 
				TRUE,     // fDaclPresent flag   
				pACL, 
				FALSE))   // not a default DACL 
			{  
				wsprintf(szShowTips,"创建用户组文件夹\"%s\"失败!\n原因: SetSecurityDescriptorDacl 失败.",szClassDir);
				AfxMessageBox(szShowTips);
				continue; 
			} 
			sa.nLength = sizeof (SECURITY_ATTRIBUTES);
			sa.lpSecurityDescriptor = pSD;
			sa.bInheritHandle = FALSE;
			if(!CreateDirectory(szClassDir,&sa))
			{  
				wsprintf(szShowTips,"创建用户组文件夹\"%s\"失败!\n原因: CreateDirectory 失败.",szClassDir);
				AfxMessageBox(szShowTips);
				continue; 
			}
		}
		for(UINT nNumber=g_nNumFrom;nNumber<=g_nNumTo&&g_bIsContinue;++nNumber)
		{
			pUI=NULL;
			bUserDirExist=FALSE;
			if (pACL) 
				LocalFree(pACL);
			if(pSID)
				FreeSid(pSID);
			if (pSD) 
				LocalFree(pSD);	
			pACL=NULL;
			pSID=NULL;
			pSD=NULL;
			if(arrClass.GetAt(nClass)<10)
			{
				if(nNumber<10)
					wsprintf(szUserName,"%s%s0%d0%d",g_szPrefix,g_szYear,arrClass.GetAt(nClass),nNumber);
				else
					wsprintf(szUserName,"%s%s0%d%d",g_szPrefix,g_szYear,arrClass.GetAt(nClass),nNumber);
			}
			else
			{
				if(nNumber<10)
					wsprintf(szUserName,"%s%s%d0%d",g_szPrefix,g_szYear,arrClass.GetAt(nClass),nNumber);
				else
					wsprintf(szUserName,"%s%s%d%d",g_szPrefix,g_szYear,arrClass.GetAt(nClass),nNumber);
			}
			if(nNumber<10)
				wsprintf(szUserDir,"%s\\0%d",szClassDir,nNumber);
			else
				wsprintf(szUserDir,"%s\\%d",szClassDir,nNumber);
			MultiByteToWideChar(CP_ACP, 0, szUserName, -1, wszUserName, 32);
			nStatus=NetUserGetInfo(NULL,wszUserName,0,(LPBYTE*)&pUI);
			NetApiBufferFree(pUI);
			if(nStatus!=NERR_Success)
				continue;
			::SendMessage(g_hShowWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szUserDir);
			if(bClassDirExist)
			{
				DirInfo.dwFileAttributes=0;
				hDir=FindFirstFile(szUserDir,&DirInfo);
				if(hDir==INVALID_HANDLE_VALUE)
					bUserDirExist=FALSE;
				else if(hDir!=INVALID_HANDLE_VALUE&&(DirInfo.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
				{
					bUserDirExist=TRUE;
					FindClose(hDir);
				}
				else
				{
					wsprintf(szShowTips,"设置用户文件夹\"%s\"失败!\n原因: 存在同名文件.",szUserDir);
					AfxMessageBox(szShowTips);
					FindClose(hDir);
					continue;
				}
			}
			ZeroMemory(ea, 2*sizeof(EXPLICIT_ACCESS));
			ea[0].grfAccessPermissions = 0x001F01FF;
			ea[0].grfAccessMode = SET_ACCESS;
			ea[0].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
			ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
			ea[0].Trustee.TrusteeType = TRUSTEE_IS_USER;
			ea[0].Trustee.ptstrName  = g_szCurrentUser;

			ea[1].grfAccessPermissions = 0x001200EF;
			ea[1].grfAccessMode = SET_ACCESS;
			ea[1].grfInheritance= NO_INHERITANCE;
			ea[1].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
			ea[1].Trustee.TrusteeType = TRUSTEE_IS_USER;
			ea[1].Trustee.ptstrName  = szUserName;

			ea[2].grfAccessPermissions = 0x001F01FF;
			ea[2].grfAccessMode = SET_ACCESS;
			ea[2].grfInheritance= 0x0000000B;
			ea[2].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
			ea[2].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
			ea[2].Trustee.ptstrName  = szUserName;

			if(! AllocateAndInitializeSid( &SIDAuthWorld, 1,
				SECURITY_WORLD_RID,
				0, 0, 0, 0, 0, 0, 0,
				&pSID) )
			{
				wsprintf(szShowTips,"操作用户文件夹\"%s\"失败!\n原因: AllocateAndInitializeSid 失败.",szUserDir);
				AfxMessageBox(szShowTips);
				continue;
			}
			if (SetEntriesInAcl(3, ea, NULL, &pACL)!=ERROR_SUCCESS) 
			{
				wsprintf(szShowTips,"操作用户文件夹\"%s\"失败!\n原因: SetEntriesInAcl 失败.",szUserDir);
				AfxMessageBox(szShowTips);
				continue;
			}
			if(bUserDirExist)
			{

				nStatus=SetNamedSecurityInfo(szUserDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,
					NULL,NULL,pACL,NULL);
				if(nStatus!=ERROR_SUCCESS)
				{
					wsprintf(szShowTips,"设置用户文件夹\"%s\"的权限失败!\n原因: SetNamedSecurityInfo 失败.",szUserDir);
					AfxMessageBox(szShowTips);
					continue;
				}
			}
			else
			{
				pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
					SECURITY_DESCRIPTOR_MIN_LENGTH); 
				if (pSD == NULL) 
				{ 
					wsprintf(szShowTips,"创建用户文件夹\"%s\"失败!\n原因: LocalAlloc 失败.",szUserDir);
					AfxMessageBox(szShowTips);
					continue; 
				} 

				if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) 
				{
					wsprintf(szShowTips,"创建用户文件夹\"%s\"失败!\n原因: InitializeSecurityDescriptor 失败.",szUserDir);
					AfxMessageBox(szShowTips);
					continue;
				} 
				
				// Add the ACL to the security descriptor. 
				
				if (!SetSecurityDescriptorDacl(pSD, 
					TRUE,     // fDaclPresent flag   
					pACL, 
					FALSE))   // not a default DACL 
				{  
					wsprintf(szShowTips,"创建用户文件夹\"%s\"失败!\n原因: SetSecurityDescriptorDacl 失败.",szUserDir);
					AfxMessageBox(szShowTips);
					continue; 
				} 
				sa.nLength = sizeof (SECURITY_ATTRIBUTES);
				sa.lpSecurityDescriptor = pSD;
				sa.bInheritHandle = FALSE;
				if(!CreateDirectory(szUserDir,&sa))
				{
					wsprintf(szShowTips,"创建用户文件夹\"%s\"失败!\n原因: CreateDirectory 失败.",szUserDir);
					AfxMessageBox(szShowTips);
				}
			}
		}
	}
CleanupAdmin:	
    if (pAdminSID) 
        FreeSid(pAdminSID);
	if(pSID)
		FreeSid(pSID);
	if(pACL)
		LocalFree(pACL);
	if(pSD)
		LocalFree(pSD);
	arrClass.RemoveAll();

	if(g_bIsContinue)
		AfxMessageBox("学生用户文件夹创建完毕!");
	::SendMessage(g_hExitWnd,WM_COMMAND,WM_DESTROY,0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -