UWP与Win32应用间共享DirectX Texture2D失败问题解决
尝试搭建UWP应用与Win32应用间的共享DirectX 2D纹理环境。UWP端基于C++/WinRT的CoreApp模板,仅保留Run方法,在wWinMain中创建共享纹理;Win32端为空白项目。UWP端创建共享纹理返回S_OK,但Win32端调用OpenSharedResourceByName时返回E_INVALIDARG(参数无效),而相同方案在两个Win32应用间可正常运行,推测问题与UWP沙箱限制有关。
UWP端核心代码
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) { // Create D3D11 Device and Context ID3D11Device1* d3d11Device; ID3D11DeviceContext1* d3d11DeviceContext; { ID3D11Device* baseDevice; ID3D11DeviceContext* baseDeviceContext; D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_1 }; UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; D3D11CreateDevice( 0, D3D_DRIVER_TYPE_HARDWARE, 0, creationFlags, featureLevels, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION, &baseDevice, 0, &baseDeviceContext); baseDevice->QueryInterface(__uuidof(ID3D11Device1), (void**)&d3d11Device); baseDevice->Release(); baseDeviceContext->QueryInterface(__uuidof(ID3D11DeviceContext1), (void**)&d3d11DeviceContext); baseDeviceContext->Release(); } HRESULT result; D3D11_TEXTURE2D_DESC texDesc; texDesc.Width = 360; texDesc.Height = 360; texDesc.MipLevels = 1; texDesc.ArraySize = 1; texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; texDesc.SampleDesc.Count = 1; texDesc.SampleDesc.Quality = 0; texDesc.Usage = D3D11_USAGE_DEFAULT; texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_RENDER_TARGET; texDesc.CPUAccessFlags = 0; texDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX; result = d3d11Device->CreateTexture2D(&texDesc, nullptr, &texture2D); IDXGIResource1* pResource; texture2D->QueryInterface(__uuidof(IDXGIResource1), (void**)&pResource); result = pResource->CreateSharedHandle( nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, L"SharedTexture2D", &textureHandle); CoreApplication::Run(make<App>()); }
Win32端核心代码
#define WIN32_LEAN_AND_MEAN #define NOMINMAX #include <string> #include <Userenv.h> #include <windows.h> #include <d3d11_1.h> #include <windows.h> #include <d3d11_1.h> #pragma comment(lib, "d3d11.lib") #pragma comment(lib, "Userenv.lib") int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nShowCmd*/) { // Create D3D11 Device and Context as for uwp // Try to open shared resource by name // from Package.appmanifest of uwp project constexpr auto packageFamilyName= L"b0660272-d6e8-493b-8da6-7e324d3652f3_6jxptbrcxhds4"; PSID psid{}; DeriveAppContainerSidFromAppContainerName(packageFamilyName, &psid); auto path = std::wstring(); path.resize(256u); ULONG returnedPathSize = 0u; auto status = GetAppContainerNamedObjectPath( nullptr, psid, path.size(), (LPWSTR)(path.data()), &returnedPathSize); path.insert(returnedPathSize - 1u, L"\\SharedTexture2D"); ID3D11Texture2D* texture; auto result = d3d11Device->OpenSharedResourceByName( path.c_str(), DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, __uuidof(ID3D11Texture2D), (void**)&texture); return result; }
解决方法
最初调用DeriveAppContainerSidFromAppContainerName获取应用容器SID后仍报错,最终修正该函数传入的包族名参数后问题解决。
内容的提问来源于stack exchange,提问作者UnclePooh
相关产品推荐
相关产品推荐

