How do I pass a handle of DirectX shared resource to another (unrelated) process?

谁都会走 提交于 2020-01-06 05:15:33

问题


I'm trying to pass a HANDLE between two unrelated processes (no child process).

This HANDLE rSharedHandle comes from DXDevice9->CreateTexture(w, h, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTexture9, &rSharedHandle);

And I want / need to use it on the other side to be shared using DXDevice11->OpenSharedResource(rSharedHandle, __uuidof(ID3D11Resource), (void**)(&TmpResource11));

The complete explanation of the procedure is can be found here.


I came across this question which as answers but not clear enough that I understand what they mean. See this here.

I also came across the marshal_as which is explained here, but I don't understand how to use it in my case. Further details about the types taken can be found here.


EDIT (1): I don't want to deal with managed code if possible. So I search more about "How to pass/share handle between processes". I came across the function DuplicateHandle() (See here) but I cannot find whether it doesn't work because the type of the handle is not a valid type (See list in Remarks section), or whether it is because of something else (unknown).

Moreover, the second process handle needs to be send before DuplicateHandle() (to be used in the function itself) and the resulting duplicated handle needs to be send to the second process (to finally be used).


回答1:


Do you mix managed and unmanaged code? - If no, I assume all the documentation on marshalling might not be necessary. Assuming, you already have some means of transferring data between both processes, I would try the following direct way:

Looking into the Windows headers (e.g. winnt.h), we can find the typedef for HANDLE:

typedef void *HANDLE;

Thus, you could try to static_cast the handle to an uint64_t (on x64 systems) on the sender side, transfer it to the receiver and static_cast it back to a HANDLE.




回答2:


I found a simple (and gross) way to do it. It is more a proof of work than a final product but it is working!

The way I proceed was the following:

  1. Create the rSharedHandle from the texture in the first process (lets call it P1)
  2. I write to a binary file the 8 bytes corresponding to the handle
  3. In P2, I read the same file and set the rSharedHandle (in P2)

And as easy as it seems, it works well.

Feel free to ask questions in the comment if you want/need more details.



来源:https://stackoverflow.com/questions/54518992/how-do-i-pass-a-handle-of-directx-shared-resource-to-another-unrelated-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!