core.sys.windows.com.ComObject apparently has wrongly laid out Vtable

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Tue Mar 19 16:13:15 UTC 2024


I have gone ahead and ported the C style interface over to D and 
rewritten your test code.

Here the output it prints:

```
initial count 0
                ip vtable: 7FF76EFFA240
                ip AddRef: 7FF76EF9D210
    iunknownObject vtable: 7FF76EFFA240
    iunknownObject AddRef: 7FF76EF9D210
After D com object count: 1
After C com object count: 2
```

The code:

```d
import std.stdio;
import drtcom = core.sys.windows.com;
import windows = core.sys.windows.windows;

void main() {
     auto comObject = new drtcom.ComObject;
     auto iunknownObject = cast(drtcom.IUnknown)comObject;

     writeln("initial count ", comObject.count);

      {
          C_IUnknown* ip = cast(C_IUnknown*)iunknownObject;
          writeln("               ip vtable: ", ip.lpVtbl);

          auto ipaddref = cast(void*)ip.lpVtbl.AddRef;
          writeln("               ip AddRef: ", ipaddref);
      }

      {
          auto vtable = iunknownObject.__vptr;
          writeln("   iunknownObject vtable: ", vtable);

          auto addref = cast(void*)(&iunknownObject.AddRef).funcptr;
          writeln("   iunknownObject AddRef: ", addref);
      }

      {
          comObject.AddRef();
          writeln("After D com object count: ", comObject.count);

          C_IUnknown* ip = cast(C_IUnknown*)iunknownObject;
          ip.lpVtbl.AddRef(ip);
          writeln("After C com object count: ", comObject.count);
      }
}

struct C_IUnknown {
     C_IUnknownVtbl* lpVtbl;
}

struct C_IUnknownVtbl {
     extern(Windows) {
         windows.HRESULT function(C_IUnknown* This, windows.IID* riid, 
void** ppvObject) QueryInterface;
         windows.ULONG function(C_IUnknown* This) AddRef;
         windows.ULONG function(C_IUnknown* This) Release;
     }
}
```


More information about the Digitalmars-d mailing list