The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: xanatose on August 09, 2010, 07:56:09 PM

Title: Help with COM.
Post by: xanatose on August 09, 2010, 07:56:09 PM
Does anyone knows a good tutorial of using COM (Component Object Model) without using ATL or MFC (or visual basic)?

Preferably in C or assembler.

Title: Re: Help with COM.
Post by: dancho on August 09, 2010, 08:04:17 PM
yeah,in C

http://www.codeproject.com/KB/COM/com_in_c1.aspx

8 parts tutorial in total,
just use search to find others...
Title: Re: Help with COM.
Post by: ecube on August 09, 2010, 08:19:14 PM
here's a vbscript regexp example using Minors nice api http://www.masm32.com/board/index.php?topic=14507.0

his site is http://www.bostream.nu/minor28/, I highly recommend you check out his automation tool/examples.

but COM is really easily just find the interface you want say for IPersistantFile(used to make shortcuts) http://msdn.microsoft.com/en-us/library/ms687223%28v=VS.85%29.aspx

see the list of methods(functions) on that page? you have IsDirty,load,save etc...because it uses the iunknown interface(which has 3 entries) too if you combine them it could look like this as a structure

 IPersistFile            STRUCT
      IPersistFile_QueryInterface      dd       ? <-is iunknown
      IPersistFile_AddRef               dd       ? <-is iunknown
      IPersistFile_Release              dd       ?  <-is iunknown
      IPersistFile_IsDirty              dd       ?
      IPersistFile_Load                 dd       ?
      IPersistFile_Save                 dd      ?
      IPersistFile_SaveCompleted   dd       ?
      IPersistFile_GetCurFile           dd       ?
IPersistFile            ENDS

ok so we have our structure to use the methods all you do is cocreateinstance using the  this interface which according to msdn it's IIID is IID_IPersistFile is defined as 0000010b-0000-0000-C000-000000000046

after we use that it'll return the interface ptr in a dword and you just

mov eax,[theptr]
mov eax,[eax]

now we have a memory ptr to the vtable so to access a method just add it from the start if this were goasm id do

invoke [eax+IPersistFile.IPersistFile_Load], [theptr], param1,param2,param3

or if you wanted manually could do
invoke [eax+16], [theptr], param1,param2,param3 ;since IPersistFile_Load is 16 down


just remember that the first parameter is always the the current interface pointer you're currently using, in our case the one we got from cocreateinstance

I have some COM tuts/examples somewhere written by murphy they use to be in masm32 but hutch took em out, i'll try and find for you.

Oh and the methods I believe are always cdecl(c calling convention) so you have to adjust esp after the call.

also you can get other interface pointers using the QueryInterface method above http://msdn.microsoft.com/en-us/library/ms682521%28VS.85%29.aspx
Title: Re: Help with COM.
Post by: oex on August 09, 2010, 08:23:48 PM
You might find this coinvoke macro handy....

http://www.masm32.com/board/index.php?topic=14289.msg113911#msg113911
Title: Re: Help with COM.
Post by: baltoro on August 09, 2010, 11:15:04 PM
The best site I have ever found for COM assembly code is: Japheth's Site (http://www.japheth.de/COMnASM.html).
...It's amazing, Really.
Title: Re: Help with COM.
Post by: ecube on August 10, 2010, 12:39:23 AM
also all interface methods definitely aren't cdecl.
Title: Re: Help with COM.
Post by: xanatose on August 10, 2010, 03:18:00 PM
Wow, that was fast. Thanks everyone.

[EDIT]
BTW:
If you only have a dll and not a header, how do you get the interfaces, enums, etc? On Visual C++, you use #import and it will generate a header. But I wonder how this is done via the windows API. (so I can make the project Independent of Visual C++)
Title: Re: Help with COM.
Post by: Rockoon on August 10, 2010, 04:20:37 PM
Quote from: E^cube on August 10, 2010, 12:39:23 AM
also all interface methods definitely aren't cdecl.

definitely STDCALL.
Title: Re: Help with COM.
Post by: ecube on August 10, 2010, 04:24:47 PM
Quote from: Rockoon on August 10, 2010, 04:20:37 PM
Quote from: E^cube on August 10, 2010, 12:39:23 AM
also all interface methods definitely aren't cdecl.

definitely STDCALL.

some can be cdecl I messed with one that was last night, directx probably is too
Title: Re: Help with COM.
Post by: Vortex on August 10, 2010, 05:47:43 PM
Mike Panitz's Introduction to COM :

http://faculty.cascadia.edu/mpanitz/COM_Tutorial/index.html
Title: Re: Help with COM.
Post by: askm on December 05, 2010, 01:15:49 AM
Minor28,

Please inform per your StringFunctions.lib, as it is not error free. ::)