# DX12 How to render my DirectX C++ Engine to a C# Panel

This topic is 754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So the title pretty much sums it all up. I wanted to know how to set the HWND for my DirectX Engine to a C# Panel. I know it involves making a C++/CLI Wrapper. I'm learning DirectX from Frank Luna's DirectX 12 Book so the engine is here: https://github.com/d3dcoder/d3d12book. If someone could download his source code and make a C++/CLI Wrapper out of Chapter 4 that would be great. All you need to do is go to chapter 4 and open it in Visual Studio and you'll have all the files there. I know this is a lot of ask but I've been trying to do this for days and any help would be so appreciated.

##### Share on other sites

What exactly are you trying to do? Use a c++ renderer in a c# application? If so, passing the HWND to the renderer is just one of many many things that you need to implement, so us doing that one thing for you won't get you very far. What seems to be the problem with handing the window handle to the c++ side?

FWIW, my editor which is a C# application does this to pass the handle to my C++ renderer, which in my case is in another process so I use the network rather than a C++/CLI wrapper to do the communication, but since the value is an integer you should have no problem sending that across the language border.

public void InitRenderWindow()
{
if (EditorApp.Instance.IsConnectedToGame)
{
IntPtr handle = m_renderPanel.Handle;
EditorApp.Instance.GameRPC.initRenderWindow(handle.ToInt32(), m_renderPanel.Width, m_renderPanel.Height, m_renderPanel.Left, m_renderPanel.Top);
}
}


But again, if you are having trouble with understanding C++/CLI itself we are not going to do this one thing for you because then you will be stuck on the very next task.

##### Share on other sites

This is a forum not a hire platform so do it yourself. You need to create a function in C++ that is exported by VS Compiler first taking a void* ptr.

You do this by declaring as follows

__declspec(dllexport) void SetNetHandle(void* ptr)
{
//convert into HWND here and pass to DX
}


You then may bind this from C# using dllimport tag on an extern function (keep an eye on calling convention)

and pass an IntPtr from your C# control to that function.

##### Share on other sites

As others have indicated, this forum is not the appropriate place to ask people to do work for you. If you have specific questions or need some general guidance then we'll be happy to help, but please refrain from asking others to write code for you.

##### Share on other sites

So I asked a similar question over at stackoverflow (don't worry this time I didn't ask someone to do it for me) and here's the answer I got (http://gamedev.stackexchange.com/questions/124249/how-do-i-render-my-directx-c-engine-to-a-c-panel/124275#124275)

So I went and tried to implement this but I think I might've messed up some of my DLL Engine code because I get this error when I compile

An unhandled exception of type 'System.BadImageFormatException' occurred in GUI.exe

Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

So at this point I have no idea how to fix this so I was wondering if someone here could check out my code for the project and point out what's causing the issues. I know this is really broad but if I could point out what the error is I would and then ask you guys for help. My Code (https://drive.google.com/open?id=0BwmdbcDXQMDyMHNNV2RtVzlvWmM)

##### Share on other sites

BadImageFormatException is likely a bitness problem.

if you mix C++/CLI you're usually fixed to either 32bit or 64bit. Make sure the C# part matches the bitness (e.g. change "Any CPU" to the bitness your C++/CLI wrapper is in  x64=64 bit, x86 = 32bit)

##### Share on other sites

I've already done that, one of the first results on google. I decided to use P/Invoke to do it not C++/CLI

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002040
×