通用 Windows 平台 (UWP) 应用程序和 Win32 应用程序之间共享内存
我尝试在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论