vista下使用内存映射实现进程通讯错误。
时间:2008-07-24 14:55:13
来源:论坛整理 作者: 编辑:chinaitzhe
代码如下:
创建代码:
CShareRestrictedSD ShareRestrictedSD;//这个是获得权限的
m_nFileSize = pFileSize;
wchar_t name[256];
//创建互斥量
m_mtx = ::CreateMutexW(ShareRestrictedSD.GetSA(), FALSE, name);
// creates as well as opens a file map.
wcscpy(name,L"SCMapFile_" );
wcscat(name,pFileName);
m_hFileHandle = CreateFileMappingW( INVALID_HANDLE_VALUE, // handle to file
ShareRestrictedSD.GetSA(),
PAGE_READWRITE, // protection
0, // high-order
m_nFileSize, // low-order DWORD of size
name // object name
);
if( !m_hFileHandle ) {
return false;
}
/获得权限函数
/假如这家伙起作用,那么它的作者是jiangsheng;
//假如这家伙一点用没有,那我不知道它的作者。
PVOID BuildRestrictedSD(PSECURITY_DESCRIPTOR pSD) {
DWORD dwAclLength;
PSID psidEveryone = NULL;
PACL pDACL = NULL;
BOOL bResult = FALSE;
PACCESS_ALLOWED_ACE pACE = NULL;
SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY ;
SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
__try {
// initialize the security descriptor
if (!InitializeSecurityDescriptor(pSD,
SECURITY_DESCRIPTOR_REVISION)) {
printf("InitializeSecurityDescriptor() failed with error %d\n",
GetLastError());
__leave;
}
// obtain a sid for the Authenticated Users Group
if (!AllocateAndInitializeSid(&siaWorld, 1,
SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0,
&psidEveryone)) {
printf("AllocateAndInitializeSid() failed with error %d\n",
GetLastError());
__leave;
}
// NOTE:
//
// The Authenticated Users group includes all user accounts that
// have been successfully authenticated by the system. If access
// must be restricted to a specific user or group other than
// Authenticated Users, the SID can be constructed using the
// LookupAccountSid() API based on a user or group name.
// calculate the DACL length
dwAclLength = sizeof(ACL)
// add space for Authenticated Users group ACE
sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)
GetLengthSid(psidEveryone);
// allocate memory for the DACL
pDACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
dwAclLength);
if (!pDACL) {
printf("HeapAlloc() failed with error %d\n", GetLastError());
__leave;
}
// initialize the DACL
if (!InitializeAcl(pDACL, dwAclLength, ACL_REVISION)) {
printf("InitializeAcl() failed with error %d\n",
GetLastError());
__leave;
}
// add the Authenticated Users group ACE to the DACL with
// GENERIC_READ, GENERIC_WRITE, and GENERIC_EXECUTE access
if (!AddAccessAllowedAce(pDACL, ACL_REVISION,
GENERIC_ALL,
psidEveryone)) {
printf("AddAccessAllowedAce() failed with error %d\n",
GetLastError());
__leave;
}
// set the DACL in the security descriptor
if (!SetSecurityDescriptorDacl(pSD, TRUE, pDACL, FALSE)) {
printf("SetSecurityDescriptorDacl() failed with error %d\n",
GetLastError());
__leave;
}
bResult = TRUE;
} __finally {
if (psidEveryone) FreeSid(psidEveryone);
}
if (bResult == FALSE) {
if (pDACL) HeapFree(GetProcessHeap(), 0, pDACL);
pDACL = NULL;
}
return (PVOID) pDACL;
}
// The following function frees memory allocated in the
// BuildRestrictedSD() function
VOID FreeRestrictedSD(PVOID ptr) {
if (ptr) HeapFree(GetProcessHeap(), 0, ptr);
return;
}
CShareRestrictedSD::CShareRestrictedSD()
{
ptr=NULL;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = &sd;
sa.bInheritHandle = FALSE;
// build a restricted security descriptor
ptr = BuildRestrictedSD(&sd);
if (!ptr) {
TRACE("BuildRestrictedSD() failed\n");
}
}
CShareRestrictedSD::~CShareRestrictedSD()
{
if(ptr){
FreeRestrictedSD(ptr);
}
}
SECURITY_ATTRIBUTES* CShareRestrictedSD::GetSA()
{
if(ptr){
return &sa;
}
else
return NULL;
}
/////////////////////////////////////
打开
m_nFileSize = pFileSize;
wchar_t name[256];
//创建互斥量
m_mtx = ::CreateMutexW(NULL, FALSE, name);
// creates as well as opens a file map.
wcscpy(name,L"SCMapFile_" );
wcscat(name,pFileName);
//m_hFileHandle = CreateFileMappingW( INVALID_HANDLE_VALUE, // handle to file
// NULL, // security
// PAGE_READWRITE, // protection
// 0, // high-order DWORD of size
// m_nFileSize, // low-order DWORD of size
// name // object name
// );
m_hFileHandle = ::OpenFileMapping( FILE_MAP_READ, TRUE, name );
if( !m_hFileHandle ) {
return false;
}
望大家指教
网友回复:顶下。
网友回复:UP
网友回复:问题解决。vista 下不同用户下secssion 不是在一个空间下的,所一名字错误。
真是晕了。
关键字:vista,使用,内存,映射,实现,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面