(FINISHED)(COPIED)创建一个私有命名空间[定稿]由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“项目24创建个人空间”。
以下列出创建一个私有命名空间的步骤以及函数知识点
源码:
int WINAPI WinMain(…){
//创建一个边界描述符
Char szBoundaryText[] = “Boundary”;
HANDLE hBoundary = CreateBoundaryDescriptor(szBoundaryText,0);
//创建一个SID
BYTE localAdminSID[SECURITY_MAX_SID_SIZE];
PSID plocalAdminSID = &localAdminSID;
DWORD cbSID = sizeof(localAdminSID);
CreateWellKnownSid(WinBuiltinAdministratorsSid,NULL,plocalAdminSID,&cbSID);
//将SID和边界描述符关联起来
AddSIDToBoundaryDescriptor(&hBoundary,plocalAdminSID);
//初始化安全描述符
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = FALSE;
sa.nLength = sizeof(sa);
ConvertStringSecurityDescriptorToSecurityDescriptor(TEXT(“D(A;;GA;;;BA)”), SDDL_REVISION_1,&sa.lpSecurityDescriptor,NULL);
//创建私人命名空间,第三个参数就是以后用的前缀
HANDLE hNamespace = CreatePrivateNamespace(&sa.hBoundary,szBoundaryText);
//释放安全描述符
LocalFree(sa.lpSecurityDescriptor);
//尝试在私人命名空间中创建一个Mutex Object,并检测是否为首例程
HANDLE hMutex = CreateMutex(NULL,FALSE,TEXT(“BoundaryMutex”));
If(GetLastError()==ERROR_ALREADY_EXISTS)Printf(“Another Instance is running n”);
else printf(“First Instance”);}
下面介绍函数
CreateBoundaryDescriptor HANDLE WINAPI CreateBoundaryDescriptor(_In_ LPCTSTR Name,_In_ ULONG Flags);Parameters Name [in]
边界描述符的名字 Flags [in]
保留,设为NULL Return value
如果函数成功了,返回值就是一个到边界描述符的句柄
如果函数失败了,那么返回值就是NULL。可以调用GetLastError来获得更多的错误信息.Remarks
一个新的边界描述符必须有至少一个的SID。
CreateWellKnownSid BOOL WINAPI CreateWellKnownSid(_In_
WELL_KNOWN_SID_TYPE WellKnownSidType,_In_opt_
PSID DomainSid,_Out_opt_ PSID pSid,_Inout_
DWORD *cbSid);Parameters WellKnownSidType [in]
WELL_KNOWN_SID_TYPE枚举型结构的成员,specifies what the SID will identify DomainSid [in, optional]
一个指向SID的指针that identifies the domain to use when creating the SID.若为NULL,则使用本地计算机 pSid [out, optional]
一个指向内存的指针,用来存储新的SID cbSid [in, out]
一个双字指针描述了pSid中的可用字节数.Return value
如果函数成功了,返回值为非零
如果函数失败了,那么返回值为0.要得到更多的错误信息,调用GetLastError函数
AddSIDToBoundaryDescriptor BOOL WINAPI AddSIDToBoundaryDescriptor(_Inout_ HANDLE *BoundaryDescriptor,_In_
PSID RequiredSid);Parameters BoundaryDescriptor [in, out] 一个指向边界描述符的句柄 RequiredSid [in] 一个指向SID结构的指针
Return value 如果函数成功了,返回值非零
如果函数失败了,那么返回值为0.得到更多错误信息,调用GetLastError函数
ConvertStringSecurityDescriptorToSecurityDescriptor
BOOL WINAPI ConvertStringSecurityDescriptorToSecurityDescriptor(_In_
LPCTSTR StringSecurityDescriptor,_In_
DWORD StringSDRevision,_Out_ PSECURITY_DESCRIPTOR *SecurityDescriptor,_Out_ PULONG SecurityDescriptorSize);Parameters StringSecurityDescriptor [in] 一个指向包含字符串形式的安全描述符的指针
StringSDRevision [in] 目前此值只能被设定为SDDL_REVISION_1 SecurityDescriptor [out]
一个指向变量的指针,用来接收安全描述符。
SecurityDescriptorSize [out]
一个指向变量的指针,用来接收安全描述符的大小(以字节数表示)。此值可以设为NULL,表示不需要这个值 Return value 如果函数成功,返回值为非零
如果函数失败,那么返回值为0.要得到更多信息,调用GetLastError函数,返回值可能如下:
ERROR_INVALID_PARAMETER
有一个参数是无效的ERROR_UNKNOWN_REVISION
SDDL revision level 是无效的ERROR_NONE_MAPPED
输入的安全描述符字符串中的SID不能被找到
CreatePrivateNamespace HANDLE WINAPI CreatePrivateNamespace(_In_opt_ LPSECURITY_ATTRIBUTES lpPrivateNamespaceAttributes,_In_
LPVOID lpBoundaryDescriptor,_In_
LPCTSTR lpAliasPrefix);Parameters lpPrivateNamespaceAttributes [in, optional]
一个指向SECURITY_ATTRIBUTES结构的指针,描述了”命名空间对象”的安全属性 lpBoundaryDescriptor [in]
一个边界描述符,调用者必须在这个边界内。
lpAliasPrefix [in]
命名空间的前缀,以后要在这个命名空间内创建内核对象的时候只需要如下填写对象名prefixobjectname
只要边界描述符是不同的,不同命名空间的前缀可以相同。
Return value 如果函数成功了,那么会返回一个到新的命名空间对象的句柄
如果函数失败了,那么函数会返回NULL,如果要得到更多的消息,那么调用GetLastError Remarks 其他应用程序可以访问命名空间通过调用OpenPrivateNamespace 创建此命名空间的应用程序可以调用ClosePrivateNamespace来关闭到该命名空间对象的句柄。当这个进程终止时,这个句柄也会被关闭。在到命名空间的句柄关闭后,后续对OpenPrivateNamespace的调用会全部失败,但是命名空间内的对象上的操作可以成功。