DirectX for Visual Basic 5


Getting started

To get started you'll need the following :
DirectX support for Visual Basic

Thanks to COM, the DIRECTX.TLB file is also usable with DirectX 5 (but don't use new DirectX 5 capabilities). Once downloaded, copy this file in your Windows system directory. Under the "Project" menu, choose the "References" option and select this TLB file (or check the "DirectX" library if it's already in the list). DirectX objects are now available to Visual Basic. Check the "COM" section for more info about the underlaying technology.

Direct3D Retained Mode Sample - Light Direct3D Retained Mode Sample - Fog Direct3D Retained Mode Sample - Fire

You can also go to the samples and contributions page for screen shots of DirectX stuff written with Visual Basic...

DirectX 5 for Visual Basic

According to the last information about DirectX 5 support for VB users, DirectX 5 should finally provide support for DirectX Media but not for DirectX Foundation (last point according to directx@microsoft.com). Sorry but I just installed the DirectX 5 docs and SDK on my home computer... I expect to provide support for DirectX 5 in the middle of November

Using the DirectX.tlb file

Thanks to COM, the DIRECTX.TLB file is also usable with DirectX 5 (but don't use new DirectX 5 capabilities). Once downloaded, copy this file in your Windows system directory. Under the "Project" menu, choose the "References" option and select this TLB file (or check the "DirectX" library if it's already in the list). DirectX objects are now available to Visual Basic. Check the "COM" section for more info about the underlaying technology.

A very important point is to make sure that you set to nothing every DirectX objects before your app ends. Or your apps will likely crash...

Trapping errors

Currently, and as pointed by some of you, most of DirectX functions are seen from VB as subs. This is because these functions returns a HRESULT value, that is a sort of "OLE automation return code". This allow VB to apply standard trapping error code to these "functions" (there are *only* functions in C).
If you take no special actions, errors returned by DirectX "functions" are seen by VB as OLE automation errors and VB displays then automatically a message box that mention the error number. Let's take the following code - an attempt to use an invalid video mode - as as basis :

Sub Main() Dim dd As DirectDraw DirectDrawCreate byval 0&, dd, Nothing dd.SetCooperativeLevel hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN dd.SetDisplayMode 350, 210, 8, 0, 0 dd.RestoreDiplayMode Set dd = Nothing End Sub

When no special action is taken, a message box that mention a 88860078 automation error will appear. This number is the DDERR_INVALIDMODE.
You can still trap errors yourself by using :

On Error Resume Next

and

If Err<>0 Then ' Error code End If

If DirectX functions were declared as VB functions, the VB error trapping mechanism wouldn't apply and you would have to test every return value. For this reason, I do not plan to change this and stay with subroutines...

QueryInterface

Objects are sometimes created through a special method called QueryInterface. Objects are existing only though a mean to communicate with external world, their interface (the set of properties or methods you can use to handle this object). Sometimes it can be usefull to be able to deal with an object by using several interfaces. Let's take a quick sample. A sound is basically a piece of memory filled with sound data. No matter if you want 3D effects or something else. In this case the data are probably modified before earing but this is always just a piece of memory filled with sound data... So if you want just a simple sound you needs only to load, play and stop the sound. If you want to position this sound in a 3D space, you needs also a method that do so. So this objects supports two interfaces, one for simple sounds and the other for more elaborate effects. That's exactly the existing relationship between the DirectSoundBuffer and the DirectSound3DBuffer. The same underlaying object can be accessed using any (or both) of these interfaces just depending on what you needs to perform. QueryInterface is a COM instruction that ask to an existing object for another interface.
And the good news is that VB just do so for you.

Dim obj1 As New ObjectType1 Dim obj2 As ObjectType2 Set obj2 = obj1

I'm nearly sure that this code finally leads to the following pseudo-code :

obj1.QueryInterface IID_ObjectType2, obj2

That is to "ask" the obj1 object if it supports the interface known as ObjectType2 (uses in fact a numerical value associated with each interface). If so, the obj2 object is valid and allow to access the same object using this other interface...
The following sample allow to obtain a Direct3D object from a DirectDraw object.

Dim DDraw As DirectDraw Dim D3D As Direct3D Sub Main() DirectDrawCreate ByVal 0&, DDraw, Nothing Set D3D = DDraw End Main

The 3D sound contribution is also a good example of this. A DirectSound3DBuffer or a DirectSoundListener is created by "querying" the interface from a DirectSoundBuffer object.