Created
February 14, 2025 00:56
-
-
Save GeeLaw/178b06c7fa1ca46427677a9b91e2303c to your computer and use it in GitHub Desktop.
Write COM interfaces in ABI format in fully standard C++.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2025 Ji Luo | |
Comment to https://zhuanlan.zhihu.com/p/23530890452 | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#include<cstdint> | |
typedef std::uintptr_t const * const com_vtbl_t; | |
typedef std::int32_t hresult_t; | |
typedef std::uint32_t refcount_t; | |
struct Guid | |
{ | |
std::uint32_t data1; | |
std::uint16_t data2; | |
std::uint16_t data3; | |
std::uint8_t data4[8]; | |
}; | |
#define COM_CALL_CONV __stdcall | |
struct IUnknown | |
{ | |
/* | |
The forwarding members should use the same calling convention | |
to ensure optimal code generation (no argument shuffling needed). | |
*/ | |
hresult_t COM_CALL_CONV QueryInterface(Guid const &riid, void **ppv) | |
{ | |
return (*reinterpret_cast | |
<hresult_t (COM_CALL_CONV *)(IUnknown *, Guid const &, void **)> | |
(vtbl[0]))(this, riid, ppv); | |
} | |
refcount_t COM_CALL_CONV AddRef() | |
{ | |
return (*reinterpret_cast | |
<refcount_t (COM_CALL_CONV *)(IUnknown *)> | |
(vtbl[1]))(this); | |
} | |
refcount_t COM_CALL_CONV Release() | |
{ | |
return (*reinterpret_cast | |
<refcount_t (COM_CALL_CONV *)(IUnknown *)> | |
(vtbl[2]))(this); | |
} | |
protected: | |
com_vtbl_t vtbl; | |
}; | |
struct IObjectWithSite : IUnknown | |
{ | |
hresult_t COM_CALL_CONV SetSite(IUnknown *punk) | |
{ | |
return (*reinterpret_cast | |
<hresult_t (COM_CALL_CONV *)(IObjectWithSite *, IUnknown *)> | |
(vtbl[3]))(this, punk); | |
} | |
hresult_t COM_CALL_CONV GetSite(Guid const &riid, void **ppv) | |
{ | |
return (*reinterpret_cast | |
<hresult_t (COM_CALL_CONV *)(IObjectWithSite *, Guid const &, void **)> | |
(vtbl[4]))(this, riid, ppv); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment