Jump to content
  • Advertisement
Sign in to follow this  
cnagle150

Problem passing C# Form's Handle to Managed C++ as HWND.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to expose a game engine written in C++ contained within several dlls. I'm having trouble passing the Main Form's handle from C# to the managed C++ dll that I have written to wrapper my game engine. The following is the C# code snippet that attempts to pass the main form's Handle to a managed C++ function.
        public void Startup()
        {
            MGraphicDevice.Create();

            MGraphicDevice.Initialize2(this.Handle.ToInt32());
        }

The following is the Managed C++ code that attempts to convert the specified form handle to an HWND. The HWND is necessary in order to initialize the IDirect3DDevice to the main form.
bool MGraphicDevice::Initialize2(int Handle)
{
	HWND hWnd = (HWND)Handle;
	bool bResult = false;

	bResult = m_pGraphicDevice->Initialize(hWnd, 640, 480, false);

	return(bResult);
}

When I pass in NULL to m_pGraphicDevice->Initialize() instead of calculated HWND, the function does not crash, but naturally it doesn't initialize the IDirect3DDevice9 com interface. When I pass in the calculated HWND, the program instantly crashes with a 'System.AccessViolationException' dialog. What is the logic for passing a C# Form's Handle to Managed C++ to Unmanaged C++? Any tips or help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Hey, instead of this.Handle.ToInt32() try this.Handle.ToPointer() which changes it to an IntPtr. I looked over some old c++ code where I was mixing managed & unmanaged and this seems to have worked.

Failing that put a breakpoint just after Initialize2 and look at the parsed value for Handle, see if it looks sensible, it may give you a clue as to whats happening.

Share this post


Link to post
Share on other sites
Quote:
Original post by supamike
Hey, instead of this.Handle.ToInt32() try this.Handle.ToPointer() which changes it to an IntPtr. I looked over some old c++ code where I was mixing managed & unmanaged and this seems to have worked.


That won't make a difference, as IntPtr is just a container for an integer type. So he'll have to call IntPtr.ToInt32() to retrieve the value it contains anyways.

If everything is working fine, what you're doing there should work as well. Which makes me suspect there's an error elsewhere, such as inside the engine.

Switch DirectX to debug mode too, maybe it'll tell you the problem.

Share this post


Link to post
Share on other sites
Is a HWND an int, an unsigned int or a void*? Couldn't the cast potentially blow it up?

I'm too lazy to check right now, but you could always try something ugly like:

HWND hWnd = *(HWND*)&Handle;

and see if it works.

Share this post


Link to post
Share on other sites
HWND is a HANDLE, which in turn is just a void *. Now, the thing is, the address of a pointer is stored as an integer type, so it amounts to the same thing.

All I know is that I've never had a problem initialising native direct3d in a .net form this way, so I still think the problem is elsewhere.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!