Register
Handmade Hero»Forums»Code»function pointer
Roman
15 posts

Roman Shvayko.I love low-level programming and games.

function pointer
4 months ago Edited by Roman on April 12, 2020, 9:15 a.m.
Hi everyone. I don't understand what does co_create_instance_stub mean and why we do return 0. Thanks for help

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#define CO_CREATE_INSTANCE(name) HRESULT __stdcall name(REFCLSID rclsid,LPUNKNOWN pUnkOuter,DWORD dwClsContext, REFIID riid,LPVOID*ppv)
typedef CO_CREATE_INSTANCE(co_create_instance);


CO_CREATE_INSTANCE(co_create_instance_stub)
{
  return 0;
}

global co_create_instance *coCreateInstanceProc_ = co_create_instance_stub;
#define coCreateInstanceProc coCreateInstanceProc_
Mārtiņš Možeiko
2109 posts / 1 project
function pointer
4 months ago Edited by Mārtiņš Možeiko on April 12, 2020, 10:14 a.m.
Where is this code from? I don't think HandmadeHero uses CoCreateInstance function.

Casey used similar style code for XInput functions. When he writes code that loads functions dynamically from DLL, he does not want usage code to be littered with "if" statements (if funciton pointer is NULL do not call it, otherwise call it).

Instead he creates stub functions that does not do anything - simply returns dummy value. And if loading dll file failed, there is no actual function pointer to assign, so the usage code does not need to care about this - as stub function will be called.

This is explained here: https://hero.handmade.network/episode/code/day006/#1725

This makes sense for XInput functions, because dll potentially can be missing if you are running on older Windows.

For CoCreateInstance function this kind of code does not make much sense, as it this function will always be present in ole32.dll file. Not sure why somebody wrote it like that...