News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

What the %*&!^? is COM?

Started by NoCforMe, June 28, 2024, 01:08:27 PM

Previous topic - Next topic

NoCforMe

I know it stands for Component Object Model.
I just don't know really what the fuck it is.
I have checked the Micro$oft docs, like this page.
I find that to be a bunch of mealy-mouthed gobbledygook.

Can someone point me to an explanation of what COM is, how it's used, etc.? In plain English? Understandable and to the point?
Assembly language programming should be fun. That's why I do it.

sinsi

It's just a complicated way of interacting with an object.
You get an interface (e.g. IShellLink), which is just a structure full of pointers to "methods".
For example, calling IShellLink.SetShowCmd will set Run in the shortcut (Maximised etc.).

That's my limited understanding when I've had to use it.

TimoVJL

May the source be with you

NoCforMe

Hey, thanks, Timo! I'm reading that article now, and it is very well written. We'll see if it explains it all.

(I like that the author starts by explaining COM at a low level, not from the point of view of C++ "wrappers", which is nice for us assembly-language programmers.)
Assembly language programming should be fun. That's why I do it.

jj2007

With COM you get access to many non-API functions, such as "open a browser":

include \masm32\MasmBasic\MasmBasic.inc ; ## COM demo: open Internet Explorer ##
.code
CLSID_IExplorer GuidFromString("0002DF01-0000-0000-C000-000000000046") ; either use quoted text syntax or...
IID_IWebBrowser2  GuidFromString({D30C1661-CDAF-11D0-8A3E-00C04FC9E26E}) ; ... paste copied registry key name
MyBrowser proc uses edi url
LOCAL vEmpty:VARIANT, hWin, WebInterface
  ClearLocals
  lea edi, WebInterface
  pInterface equ dword ptr [edi]
  .if rv(CoCreateInstance, addr CLSID_IExplorer, NULL, CLSCTX_LOCAL_SERVER, addr IID_IWebBrowser2, edi)==S_OK
CoInvoke pInterface, IWebBrowserVtbl.put_StatusBar, VARIANT_FALSE ; OK, now configure the browser
CoInvoke pInterface, IWebBrowserVtbl.put_MenuBar, VARIANT_FALSE ; false = no menu
CoInvoke pInterface, IWebBrowserVtbl.put_Visible, VARIANT_TRUE
CoInvoke pInterface, IWebBrowserVtbl.get_HWND, addr hWin
lea ecx, vEmpty ; Navigate needs pointers to four empty VARIANTS
CoInvoke pInterface, IWebBrowserVtbl.Navigate, Ole$(url), ecx, ecx, ecx, ecx
  .endif
  ret
MyBrowser endp

  Init
  .if rv(OleInitialize, NULL)==S_OK
invoke MyBrowser, Chr$("http://masm32.com/board/index.php?action=unread")
invoke OleUninitialize
  .endif
EndOfCode

NoCforMe

Further reading of the article Timo posted a link to starts to reveal what it's all about:

QuoteA C++ class is really nothing more than a struct whose first member is always a pointer to an array -- an array that contains pointers to all the functions inside of that class. And the first argument passed to each function is always a pointer to the class (i.e., struct) itself. (This is referred to as the hidden "this" pointer.)

I was pretty sure that's what the "vtable" was for. It's starting to come together now. That guy (the author of that article) is good!

Now, what about "CoInvoke" and "CoInitialize"? Are they just ways of getting at all those vtable functions? And what about "apartments"? is that part of COM too?
Assembly language programming should be fun. That's why I do it.

C3

Quote from: TimoVJL on June 28, 2024, 03:26:09 PMSome basic info:
COM in plain C by Jeff Glatt

Articles by Jeff Glatt

Thank you Timo for this article, I have been in pain with COM and how to use it with MASM32/64.

NoCforMe

More questions about COM:

1. Since one would need a valid GUID if one were to write one's own COM utility, is that tool from Micro$oft (GUIDGEN.EXE) available? do you have to have VC or some other package to get that?

2. So what are some of the top examples of COM things we might want to use in an assembly-language program? Jochen gave us one; what are some others?

3. Who here has successfully wrestled with COM and gotten it to work with assembly language? (32-bit preference here)
Assembly language programming should be fun. That's why I do it.

jj2007

Quote from: NoCforMe on June 29, 2024, 08:07:12 AMWho here has successfully wrestled with COM and gotten it to work with assembly language?

OpenAdo is pure COM under the hood.

sinsi

Quote from: NoCforMe on June 29, 2024, 08:07:12 AM3. Who here has successfully wrestled with COM and gotten it to work with assembly language? (32-bit preference here)
Saving/restoring desktop icon positions using Shell interfaces.
One module:
.const

EXTERNDEF CLSID_ShellWindows:GUID
EXTERNDEF IID_IShellWindows:GUID
EXTERNDEF IID_IDispatch:GUID
EXTERNDEF IID_IServiceProvider:GUID
EXTERNDEF IID_IShellBrowser:GUID
EXTERNDEF IID_IFolderView:GUID
EXTERNDEF SID_STopLevelBrowser:GUID

.code
GetDesktopFolderView proc ppv:PTR IFolderView
    local psw:PTR IShellWindows
    local pds:PTR IDispatch
    local psp:PTR IServiceProvider
    local psb:PTR IShellBrowser
    local psv:PTR IShellView

    local lhwnd:HANDLE
    local vtLoc:VARIANT,vtEmpty:VARIANT

        sub eax,eax
        mov psw,eax
        mov pds,eax
        mov psp,eax
        mov psb,eax
        mov psv,eax
        mov vtEmpty.dw2,eax
        mov vtEmpty.dw1,eax
        mov dword ptr vtEmpty.wReserved2,eax
        mov dword ptr vtEmpty.vt,eax
        mov vtLoc.dw2,eax
        mov vtLoc.dw1,eax
        mov dword ptr vtLoc.wReserved2,eax
        mov dword ptr vtLoc.vt,3
       
        invoke CoCreateInstance,offset CLSID_ShellWindows,0,CLSCTX_ALL,offset IID_IShellWindows,addr psw
        test  eax,eax
        jnz    done
        covoke psw,IShellWindows,FindWindowSW,addr vtLoc,addr vtEmpty,SWC_DESKTOP,addr lhwnd,SWFO_NEEDDISPATCH,addr pds
        test  eax,eax
        jnz    cleanup
        covoke pds,IDispatch,QueryInterface,offset IID_IServiceProvider,addr psp
        test  eax,eax
        jnz    cleanup
        covoke psp,IServiceProvider,QueryService,offset SID_STopLevelBrowser,offset IID_IShellBrowser,addr psb
        test  eax,eax
        jnz    cleanup
        covoke psb,IShellBrowser,QueryActiveShellView,addr psv
        test  eax,eax
        jnz    cleanup
        covoke psv,IShellView,QueryInterface,OFFSET IID_IFolderView,ppv
cleanup:
        push eax
        invoke SafeRelease,psv
        invoke SafeRelease,psb
        invoke SafeRelease,psp
        invoke SafeRelease,pds
        invoke SafeRelease,psw
        pop eax
done:  ret       
GetDesktopFolderView endp
See? Easy.

daydreamer

I got game programming book many years ago,using directx
The explanation was DX used com so it could support many different video card drivers ,and com kept pointers to your brand video card and might be updated with new version driver that fix bugs can have different address of a  proc because other proc's above it became longer or shorter
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

_japheth

Quote from: NoCforMe on June 29, 2024, 08:07:12 AMMore questions about COM:
3. Who here has successfully wrestled with COM and gotten it to work with assembly language? (32-bit preference here)

See
https://masm32.com/board/index.php?topic=10807.0

https://masm32.com/board/index.php?topic=8773.0

The latter one may be especially interesting, since it allows to generate include libs from type libs.
[and btw. it also shows how extraordinary good friends Hutch and I were  :bgrin: ]

Dummheit, gepaart mit Dreistigkeit - eine furchtbare Macht.

C3

Quote from: NoCforMe on June 29, 2024, 08:07:12 AMMore questions about COM:

1. Since one would need a valid GUID if one were to write one's own COM utility, is that tool from Micro$oft (GUIDGEN.EXE) available? do you have to have VC or some other package to get that?


I'm using Visual Studio and you can get GUID from IDE as the article show screenshot, and there is guidgen.exe in Developers CMD Path.

Vortex

#13
The FreeBASIC Editor for Windows ( WinFBE ) is providing GUID generators :

https://github.com/PaulSquires/WinFBE/releases

WinFBE_Suite\Tools\GUIDgen\GUIDgen.exe
WinFBE_Suite\Tools\GUIDgen\GUIDgen32.exe
WinFBE_Suite\Tools\GUIDgen\GUIDgen64.exe

sinsi