I have created a Direct3D9 wrapper in Go which uses CGo to interface with the COM objects in C.
I would like to get rid of the dependency on a C-compiler under Windows so the user would not have to install MinGW or Cygwin to use DirectX from Go.
The problem is that d3d9.dll does not expose C-functions but uses COM. The only function that can be called directly after loading the DLL (with syscall.LoadLibrary("d3d9.dll")
) is Direct3DCreate9
. This returns a COM object which exposes all functionality as methods.
How can I call COM object methods in a DLL from pure Go without CGo?
I know of the Go-OLE library which states it calls COM interfaces without CGo but I cannot, from the sources, see how I would go about doing the same thing for Direct3D9. A simple example with only the relevant parts would be of great help.
(Not a comprehensive answer as I have no time to actually test this, but still…)
While MSDN most of the time assumes you work with COM objects using some platform which has built-in "glue" for them (such as Visual C++™ product or something like this), in fact it is possible to work with COM objects using plain C—look here and here for starters.
Studying those resources you can learn that calling methods on a "COM interface" amounts to properly working with its "VTBL" (Virtual function TaBLe) block which is always located in a well-known place relative to the pointer disguised by that "interface" "thing" returned by functions instantiating COM objects.
The
go-ole
package implements in plain Go what you'd otherwise do in plain C, so armed with the knowledge of "to call methods on a COM objects we need to operate on its VTBL" we can find the implementation ofIDispatch
support in that package. So I'd start there.I'd also go right into the
go-ole
issue tracker asking for implementing a piece of example code which would show how to call methods on a COM object's handler acquired by means other than calling functions fromgo-ole
package.I asked the guys from go-ole, like @kostix suggested.
Here is the solution:
(c) mattn