Wrapper_gen, a wrapper generator for COM interfaces

DSfix was based on a Direct3D9 wrapper, which was mostly taken from an existing code base and extended manually.

Recently, I’ve needed to hook Direct3D9Ex, and came to the conclusion that the manual busy work of writing the initial wrapper is better left to a computer than a human. Therefore, I wrote a Ruby script which takes a Microsoft COM dll header interface specification, and generates the C++ code for a wrapper class for it.

Here’s the script (wrapper_gen.rb), it’s rather tiny:

To use it, you specify the interface name, input header file, output file base name, and optionally whether you want logging information to be generated for each wrapped method.

For example, ruby wrapper_gen.rb IDirect3DTexture9 d3d9.h d3d9tex true would generate a wrapper for the IDirect3DTexture9 interface, get the information from d3d9.h, and store the generated wrapper on d3d9tex.h and d3d9tex.cpp. The implementations for the latter would include logging.

Here’s are the generated files for this test case.

d3d9tex.h:

d3d9tex.cpp:

You can adjust the code generated for the logging in the Ruby script. As you can see, this can save you a lot of rote work, particularly if you want to intercept multiple large interfaces.

Update:

The original script didn’t deal with unnamed function parameters correctly. Now it should.

 

8 thoughts on “Wrapper_gen, a wrapper generator for COM interfaces

  1. Hi, I think I fixed the issue. The script now assigns automatically generated parameter names in case none are supplied in the header.

    I updated it in the original post. Thanks for testing!

  2. Also crashes on IDirect3DDevice9. I fixed header file generation by adding if(m) before [m[1],m[2]], but I cant fix cpp generation.

  3. This is great, but it seems to work but I get error:

    C:/Users/V/Desktop/wrapper_gen.rb:84:in block (4 levels) in <main>': undefined
    method
    []’ for nil:NilClass (NoMethodError)

Leave a Reply to tapcio Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">