服务程序创建共享内存,外部一个程序要进行读写内存的操作,如何实现?

解决方案 »

  1.   

    内存映射文件,
    CreateFileMapping
    OpenFileMapping
      

  2.   

    哈哈哈哈,我回答了你的问题。
    重点是有两个:
    一个:
    服务程序创建的时候CreateFileMapping这个LPSECURITY_ATTRIBUTES参数不能为空,否则默认权限的话,普通程序是不能用写权限打开的。二个:文件映射的名字应当是介样的:"Global\\yourName"。
    其它都一样了。给分给分。
      

  3.   

    另外那个安全描述符创建起来是比较麻烦的一个事儿。下面的代码演示如何创建安全描述符,同时创建文件映射:DWORD dwRes;
    PSID pEveryoneSID = NULL, pAdminSID = NULL;
    PACL pACL = NULL;
    PSECURITY_DESCRIPTOR pSD = NULL;
    EXPLICIT_ACCESS ea[2];
    SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
    SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;

    SECURITY_ATTRIBUTES sa;

    // Create a well-known SID for the Everyone group.
    char ErrorInfo[1024];
    if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
    SECURITY_WORLD_RID,
    0, 0, 0, 0, 0, 0, 0,
    &pEveryoneSID))
    {
    wsprintf(ErrorInfo,"AllocateAndInitializeSid Error %u\n", GetLastError());
    OutputDebugString(ErrorInfo);
    break;
    }

    // Initialize an EXPLICIT_ACCESS structure for an ACE.
    // The ACE will allow Everyone read access to the key.
    ZeroMemory(&ea, 2 * sizeof(EXPLICIT_ACCESS));
    ea[0].grfAccessPermissions = KEY_READ;
    ea[0].grfAccessMode = SET_ACCESS;
    ea[0].grfInheritance= NO_INHERITANCE;
    ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
    ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
    ea[0].Trustee.ptstrName  = (LPTSTR) pEveryoneSID;

    // 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(ErrorInfo,"AllocateAndInitializeSid Error %u\n", GetLastError());
    OutputDebugString(ErrorInfo);
    FreeSid(pEveryoneSID);
    break;
    }

    // Initialize an EXPLICIT_ACCESS structure for an ACE.
    // The ACE will allow the Administrators group full access to the key.
    ea[1].grfAccessPermissions = KEY_ALL_ACCESS;
    ea[1].grfAccessMode = SET_ACCESS;
    ea[1].grfInheritance= NO_INHERITANCE;
    ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
    ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
    ea[1].Trustee.ptstrName  = (LPTSTR) pAdminSID;

    // Create a new ACL that contains the new ACEs.
    dwRes = SetEntriesInAcl(2, ea, NULL, &pACL);
    if (ERROR_SUCCESS != dwRes) 
    {
    wsprintf(ErrorInfo,"SetEntriesInAcl Error %u\n", GetLastError());
    OutputDebugString(ErrorInfo);
    FreeSid(pEveryoneSID);
    FreeSid(pAdminSID);
    LocalFree(pACL);
    break;
    }

    // Initialize a security descriptor.  
    pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, 
    SECURITY_DESCRIPTOR_MIN_LENGTH); 
    if (NULL == pSD) 

    wsprintf(ErrorInfo,"LocalAlloc Error %u\n", GetLastError());
    OutputDebugString(ErrorInfo);
    FreeSid(pEveryoneSID);
    FreeSid(pAdminSID);
    LocalFree(pACL);
    LocalFree(pSD);
    break;


    if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) 
    {  
    wsprintf(ErrorInfo,"InitializeSecurityDescriptor Error %u\n",
    GetLastError());
    OutputDebugString(ErrorInfo);
    FreeSid(pEveryoneSID);
    FreeSid(pAdminSID);
    LocalFree(pACL);
    LocalFree(pSD);
    break; 


    // Add the ACL to the security descriptor. 
    if (!SetSecurityDescriptorDacl(pSD, 
    TRUE,     // bDaclPresent flag   
    pACL, 
    FALSE))   // not a default DACL 
    {  
    wsprintf(ErrorInfo,"SetSecurityDescriptorDacl Error %u\n", GetLastError());
    OutputDebugString(ErrorInfo);
    FreeSid(pEveryoneSID);
    FreeSid(pAdminSID);
    LocalFree(pACL);
    LocalFree(pSD);
    break;


    // Initialize a security attributes structure.
    sa.nLength = sizeof (SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor = pSD;
    sa.bInheritHandle = FALSE;

    // Use the security attributes to set the security descriptor 
    // when you create a key.
    ///////////////////////////////////////////////////////////////////// HANDLE hFile = INVALID_HANDLE_VALUE;
    hMapFile = CreateFileMapping(hFile,&sa,
    PAGE_READWRITE,0,100,"Global\\MyFileMappingObject");
    if (hMapFile == NULL) 

    OutputDebugString("Could not create file mapping object.\r\n");
    FreeSid(pEveryoneSID);
    FreeSid(pAdminSID);
    LocalFree(pACL);
    LocalFree(pSD);
    break;
    }

    if (pEveryoneSID) 
    FreeSid(pEveryoneSID);
    if (pAdminSID) 
    FreeSid(pAdminSID);
    if (pACL) 
    LocalFree(pACL);
    if (pSD) 
    LocalFree(pSD);