How to Use Direct3d from Managed Code Using an Unmanaged C++ DLL


Application Security Testing: An Integral Part of DevOps

This article explains how to use Direct3d, Input and Sound from managed code like C#, using an unmanaged C++ DLL.

The DLL functions need to be declared as extern in the C++ dll extern "C" __declspec(dllexport) int InitDX(int hWnd,int w, int h) and imported in the C# application file [DllImport("DXC", SetLastError = true)] private static extern void InitDX(int hWnd,int w,int h);

The trick is to call the blocking DLL from managed code within a extra thread.

In C# like this:

    int tmph = 0; 
     void Video() 
     D = new Display();//the window for direct3d to use   
     D.Owner = this; 
     D.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 
     tmph = (int)D.Handle; 
     VideoThread = new Thread(new ThreadStart(Video)); 
     VideoThread.Name = "WOR Video Thread"; 

To close the DXUT Interface call the C++ function.

 extern "C" __declspec(dllexport) void CloseDX()  

The above code closes the DirectXWindow and shutdowns the UT Interface.

With Application specific calls, one can vary the tile data, which is displayed in the render frame callback.

The Sound Interface could be initiated with:

extern "C" __declspec(dllexport)         private int InitDirectSound()

The Sounds can then be played, controlled and stopped with:

extern "C" __declspec(dllexport) void PlayLobbySound(int nr) 

...and other exported functions.

The Direct Input Interface here only uses the keyboard, which is initiated as follows:

extern "C" __declspec(dllexport) private void InitializeInputDevices()

A Call to extern "C" __declspec(dllexport) private bool ReadImmediateData() fills the buffer and the any key can be compared with extern "C" __declspec(dllexport) private bool CheckKey(int k).

The C++ DLL project must be of Unicode type including the DXUT Comon files.

Library includes are:

  • dxerr.lib
  • dxguid.lib
  • d3dx9d.lib
  • d3d9.lib
  • winmm.lib
  • comctl32.lib
  • odbc32.lib
  • odbccp32.lib
  • Dsound.lib
  • dinput8.lib

For details see the dxc.cpp file in the attachment.

This is part of the Wizard of Wor Arcade game conversion for windows:

Wizard of Wor a c# game using c++ Directx dll
Figure 1

About the Author

Tim Freitag

looking 4 job ;)



  • why not xna?

    Posted by adamtuliper on 03/12/2012 12:05pm

    Why go through this route rather than the framework Microsoft has already provided for similar access via the xna framework? (ex http://msdn.microsoft.com/en-us/library/bb203894.aspx)

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.