Handmade Hero » Forums » Code » Why don't we call GetProcAddress on Certain COM methods
HFKloren
Henry Kloren
9 posts
#16201 Why don't we call GetProcAddress on Certain COM methods
3 months, 2 weeks ago Edited by Henry Kloren on Sept. 3, 2018, 1:20 p.m.

For example, the earliest occurence of this in the series was with Audio, where we called GetProcAddress to get DirectSoundCreate but we didn't call it to get SetFormat. Is this a weird COM idiosyncrasy, where it maps member functions to the correct addresses in the DLL for you...
ratchetfreak
419 posts
#16204 Why don't we call GetProcAddress on COM methods
3 months, 2 weeks ago

COM objects are structs with a built in virtual function table which is filled in by the library. This means that the getProcAddress bits are done for you.
HFKloren
Henry Kloren
9 posts
#16205 Why don't we call GetProcAddress on COM methods
3 months, 2 weeks ago

so to instantiate all the COM methods we only need to call its create function, and it'll fill out the vtable itself? so we only ever have to GetProcAddress one function?
ratchetfreak
419 posts
#16206 Why don't we call GetProcAddress on COM methods
3 months, 2 weeks ago

exactly.

It's a common pattern with COM, a single stand along function to create the first COM object and everything else is through the vtables of the COM objects
mmozeiko
Mārtiņš Možeiko
1833 posts / 1 project
#16210 Why don't we call GetProcAddress on Certain COM methods
3 months, 2 weeks ago

Another common pattern with COM objects is CoCreateInstance function. COM objects are registered in system special registry. And they can be created if you know their class ID.

Here's example code for WASAPI: https://docs.microsoft.com/en-us/...ktop/coreaudio/rendering-a-stream
See how IMMDeviceEnumerator object (pEnumerator) is created by calling CoCreateInstance and passing its class id value - CLSID_MMDeviceEnumerator. No need for GetProcAddress or linking to dll file at all.
Draos
15 posts
#16213 Why don't we call GetProcAddress on Certain COM methods
3 months, 2 weeks ago Edited by Draos on Sept. 4, 2018, 1:04 a.m.

this is a really interesting post; never thought about this. so does it essentially search the registry for the dll with the code, and what functions it contains, then uses that information to populate the object's vtable with pointers into the right places within the dll?
mmozeiko
Mārtiņš Možeiko
1833 posts / 1 project
#16223 Why don't we call GetProcAddress on Certain COM methods
3 months, 2 weeks ago

Yes, something like that.