windows7 下进程间(一个是windows应该用程序,一个是windows服务)通过 CreateFileMapping(INVALID_HANDLE_VALUE,IntPtr.Zero, (uint)PAGE_READWRITE, 0, (uint)lngSize, string.Format("Global\\{0}", strName));不能实现.两个windows应该程序 是没有问题的。
调试欢乐多
如果由应用程序创建映射,应该不会有这样的问题。
你说的办法应该可以,请问如何添加everyone用户组的访问权限?我用WinObj工具给共享内存对象加了everyone用户组访问权限就可以了,但不知道用C#怎么实现。另外我是用应该程序创建的共享区,同样是需要everyone用户组访问权限
[StructLayout(LayoutKind.Sequential)]
struct SECURITY_DESCRIPTOR
{
byte Revision;
byte Sbz1;
ushort Control;
IntPtr Owner;
IntPtr Group;
IntPtr Sacl;
IntPtr Dacl;
}[StructLayout(LayoutKind.Sequential)]
struct SECURITY_ATTRIBUTES
{
public uint nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}[DllImport("Advapi32.dll")]
static extern bool LookupAccountName(string lpSystemName, string lpAccountName, [Out] byte[] Sid, ref uint cbSid, [Out] byte[] ReferencedDomainName, ref uint cchReferencedDomainName, ref int peUse);[DllImport("Advapi32.dll")]
static extern bool InitializeAcl([Out] byte[] pAcl, uint nAclLength, uint dwAclRevision);[DllImport("Advapi32.dll")]
static extern bool AddAccessAllowedAce([In, Out] byte[] pAcl, uint dwAceRevision, uint AccessMask, byte[] pSid);[DllImport("Advapi32.dll")]
static extern bool InitializeSecurityDescriptor(IntPtr pSecurityDescriptor, uint dwRevision);[DllImport("Advapi32.dll")]
static extern bool SetSecurityDescriptorDacl(IntPtr pSecurityDescriptor, bool bDaclPresent, byte[] pDacl, bool bDaclDefaulted);[DllImport("Kernel32.dll")]
static extern IntPtr CreateFileMapping(IntPtr hFile, ref SECURITY_ATTRIBUTES lpAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);static void Test()
{
uint cbSid = 0;
uint cchReferencedDomainName = 0;
int peUse = 0;
LookupAccountName(null, "EveryOne", null, ref cbSid, null, ref cchReferencedDomainName, ref peUse);
byte[] sid = new byte[cbSid];
byte[] referencedDomainName = new byte[cchReferencedDomainName];
LookupAccountName(null, "EveryOne", sid, ref cbSid, referencedDomainName, ref cchReferencedDomainName, ref peUse);
byte[] acl = new byte[100];
InitializeAcl(acl, (uint)acl.Length, ACL_REVISION);
AddAccessAllowedAce(acl, ACL_REVISION, 0x10000000, sid);
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.nLength = (uint)Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SECURITY_DESCRIPTOR)));
sa.bInheritHandle = false;
InitializeSecurityDescriptor(sa.lpSecurityDescriptor, GENERIC_ALL);
SetSecurityDescriptorDacl(sa.lpSecurityDescriptor, true, acl, false);
IntPtr handle = CreateFileMapping(IntPtr.Zero, ref sa, ……
}
const uint ACL_REVISION = 2;
const uint GENERIC_ALL = 0x10000000;
const uint SECURITY_DESCRIPTOR_REVISION = 1;static void Test()
{
uint cbSid = 0;
uint cchReferencedDomainName = 0;
int peUse = 0;
LookupAccountName(null, "EveryOne", null, ref cbSid, null, ref cchReferencedDomainName, ref peUse);
byte[] sid = new byte[cbSid];
byte[] referencedDomainName = new byte[cchReferencedDomainName];
LookupAccountName(null, "EveryOne", sid, ref cbSid, referencedDomainName, ref cchReferencedDomainName, ref peUse);
byte[] acl = new byte[100];
InitializeAcl(acl, (uint)acl.Length, ACL_REVISION);
AddAccessAllowedAce(acl, ACL_REVISION, GENERIC_ALL, sid);
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.nLength = (uint)Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SECURITY_DESCRIPTOR)));
sa.bInheritHandle = false;
InitializeSecurityDescriptor(sa.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(sa.lpSecurityDescriptor, true, acl, false);
IntPtr handle = CreateFileMapping(IntPtr.Zero, ref sa, ……
}