UnclePooh
UnclePooh

Reputation: 78

Shared memory between Universal Windows Platform (UWP) application and Win32 application

I try to share memory between C++/CX UWP application and C++Win32 application.
From the UWP application, I call the following code:

auto sid = ToStdWstring(Windows::Security::Authentication::Web::WebAuthenticationBroker::GetCurrentApplicationCallbackUri()->Host);
boost::to_upper(sid);
const auto memoryMapName = L"AppContainerNamedObjects\\" + sid + L"\\Mapped";

auto explicitAccess = EXPLICIT_ACCESS{};
explicitAccess.grfAccessPermissions = STANDARD_RIGHTS_ALL | FILE_MAP_ALL_ACCESS;
explicitAccess.grfAccessMode = GRANT_ACCESS;
explicitAccess.grfInheritance = NO_INHERITANCE;
explicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
explicitAccess.Trustee.TrusteeType = TRUSTEE_IS_USER;
explicitAccess.Trustee.ptstrName = (LPTSTR)memoryMapName.c_str();

auto newAccessControlList = PACL{ nullptr };
constexpr auto numOfExplicitAccessEntries = 1u;
constexpr auto oldAccessControlListIsAbsent = nullptr;

if (ERROR_SUCCESS != SetEntriesInAcl(numOfExplicitAccessEntries,
                                     &explicitAccess, 
                                     oldAccessControlListIsAbsent, 
                                     &newAccessControlList))
{
    PrintErrorMessage();
    return false;
}

auto securityDescriptor = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if (nullptr == securityDescriptor)
{
    PrintErrorMessage();
    return false;
}

if (not InitializeSecurityDescriptor(securityDescriptor, SECURITY_DESCRIPTOR_REVISION))
{
    PrintErrorMessage();
    return false;
}

if (not SetSecurityDescriptorDacl(securityDescriptor, TRUE, newAccessControlList, FALSE))
{
    PrintErrorMessage();
    return false;
}

auto securityAttributes = SECURITY_ATTRIBUTES{};
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
securityAttributes.bInheritHandle = false;
securityAttributes.lpSecurityDescriptor = securityDescriptor;

const auto message = std::wstring{ L"It's message from the UWP process" };

auto* fileMapping = CreateFileMappingFromApp(INVALID_HANDLE_VALUE,
                                                &securityAttributes,  
                                                PAGE_READWRITE,      
                                                message.size() * sizeof(wchar_t),
                                                memoryMapName.c_str());

if (fileMapping == nullptr)
{
    PrintErrorMessage();
    return false;
}

but function CreateFileMappingFromApp returns nullptr and I receive error message: The structure of the access control list (ACL) is incorrect.

Do you have any ideas what should be changed?

Thanks in advance, Michał

Upvotes: 0

Views: 440

Answers (0)

Related Questions