通用 Windows 平台 (UWP) 应用程序和 Win32 应用程序之间共享内存

发布于 2025-01-17 09:25:27 字数 2471 浏览 1 评论 0原文

我尝试在C ++/CX UWP应用程序和C ++ Win32应用程序之间共享内存。
从UWP应用程序中,我调用以下代码:

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;
}

但是函数createfilemappingfromapp返回nullptr,我接收错误消息: 访问控制列表(ACL)的结构不正确。

您有什么想法应该改变吗?

提前致谢, 米歇尔

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ł

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文