Sign in to follow this  
OrenGL

[.net] Unmanaged C++ DirectX in WinForms

Recommended Posts

OrenGL    228
I know C++ but I'm new to .NET. I'm writing a game editor for an existing engine I have in C++; I'd like to use WinForms since they are much simpler for me than MFC. What I'd like to do is have my existing C++ code run in a WinForm window. I'm having trouble with creating the D3D device. I know how to create a device both in manage and unmanaged code. The problem is how to I get a window of my existing C++ to show in the WinForms? Basically I would need to pass a HWND from the WinForms to my C++ code, or pass the Device from the WinForms to the C++ code and change it to a LPDIRECT3DDEVICE9. Can anyone show me an example? Is this possible? Some Ideas I like not to resort to if possible: 1. Rewrite the C++ code in C#. 1. Port the C++ code to Managed C++. 3. Have a WinForm window and a DirectX window (2 different windows).

Share this post


Link to post
Share on other sites
ernow    732
The handle from a Control (e.g. a Form) is available through the Handle property of the control. That's your HWND.

Passing the device might not be a very good option as you need to change it to a pointer while it is a garbage collected reference...

Cheers

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
ave you figured out how to do this? I have written a wrapper around a C++ SDK and have been able to pass a managed DirectX Device to the managed wrapper, and extract the unmanaged IDirect3DDevice object.

void setDevice(Direct3D::Device* d)
{
//This is some magical hidden method that returns a pointer to the
//IDirect3DDevice9 interface, the value 0xd2b543af is required.
if(d != 0)
{
IntPtr p = d->GetObjectByValue(0xd2b543af);
if(p.ToPointer() != NULL)
{
device = (IDirect3DDevice9*)p.ToPointer();
}
}
}

And as of 9.0c I believe you can call Device::get_UnmanagedComPointer to retrieve the device. If you choose to use the above method and 9.0c (to compile against 9.0a,b and c) then you need to make sure the the d3d9.h file is included within the Microsoft.DirectX.PrivateImplementationDetails namespace:
namespace Microsoft
{
namespace DirectX
{
namespace PrivateImplementationDetails
{
#include
#include
}
}
}
using namespace Microsoft::DirectX::PrivateImplementationDetails;

The above was taken from Tom Miller's blog and is only when compiling with VC++.

Share this post


Link to post
Share on other sites
Toolmaker    967
If your current D3D code is in a C++ DLL, you can always load the DLL and pass the Form.Handle to the DLL. The Form.Handle contains the HWND, but is stored as Int_Ptr or System.Int32.

You could also wrap up your D3D code in a custom control(Using COM and loading it using Interop) and drop this on the form after loading in the control from the OCX/DLL.

Toolmaker

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this