Thread: When to use COM

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    When to use COM

    When should I use COM and when should not?

    COM == Component Object Model
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Wow!!! this was EXACTLY my question in the thread SHBrowseForFolder(). . . I Just didn't know that .

    From what I have just found. . . you'd use COM if you are using extremely large arrays inside a Windows GUI. Apparently, the COM actually handles memory allocation for you and keeps the pages throughout your memory use. Or, in other words, if you CoInitialize() at the start of your code and CoUnitialize() at the end of your code, and in the middle you preform several CoTaskMemAlloc() and CoTaskMemFree() then you'd not be asking the OS for more memory each time you request it. Quzah had a good idea about memory management along these same lines * sorry, don't remember the thread * so, I guess what I'm saying is that you'd use the COM if you *HAVE* to, but it may be better to use your own, if you don't trust MS to handle memory for you.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    COM is not just about memory handling, is it?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Looked to me to be the largest part of it. . . though, there is more about it on MSDN. There was a large debate about when to use it on the web, however, I don't recall where that is at the moment.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You use COM when you have to, and avoid it whenever doing so isn't more effort than using it.

    That's my philosophy, anyway. It has served me well.

    (The effort of using COM is considerable.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Its better for me to ask: When you have to use COM?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    When the functionality isn't available in any other form. For example, using DirectX (before the .Net wrappers were created). Using OLE. Using ActiveX. All these technologies are built on top of COM, and the only way not to use COM when using them is to look for some wrapper. (MFC wraps a lot of OLE and ActiveX functionality quite nicely.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Why they are built on top of COM? MS should have a reason to do so? Andwhen should we implement our objects on top of COM?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Writing COM interfaces sucks. Using COM sucks less, but still w/o wrapper classes..sucks.

    COM is great and powerful but good documentation is essential. When a company writes a COM object or ActiveX object they will provide docs on how to use it and what happens when you call this or that.

    Some functions in DX increment the ref count so you must specifically decrement it for the COM object to properly release. COM is brilliant but it is not perfect. I believe with .NET we will see the soon demise of COM and COM+.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by siavoshkc
    Why they are built on top of COM? MS should have a reason to do so?
    Because COM has some features that make it suitable for that kind of thing. Binary interoperability, for example. The fact that the interface-implementation design can hide marshaling code, which allows the implementation of an interface to sit in a different process than the one using the interface. (This is, for example, what happens when a web browser embeds Adobe Acrobat.)


    Btw, writing COM interfaces is easy. Writing COM implementations is the really bad thing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    It is good place to ask another question: .Net is a wrapper for COM or does the job itself?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    This is a question that I had a few years ago, and I didn't really "get it" until I started messing around with VBScript and VB. What you don't understand, is that Microsoft wasn't thinking about C++ when they wrote COM. Yes, COM is about binary-comatibility, which means you can write a component in one language, and then use it from another.

    In practice, this means you can write components in C++, using COM, packaging them into a DLL, and then make them callable from VBScript. This makes a lot of sense, because VBScript is a relatively limited language. So COM via C++ is basically a way to extend the (VBScript) language. Also, lots of times people write components in C++, and the sole purpose of those components is to be reused in VB code. For example, writing the GUI might be easy in VB, but the actual processing logic might already be written in c++, or it might be more efficint in c++, or it might just be easier to code in c++. Whatever the reason, you could write the front-end GUI code in VB, and the processing/business logic in a c++ com component.

    So what you'll find, if you research VB or VBScript, is that using COM components in those langauge is very easy. Using COM in C++ is not so easy, and this is my opinion, but I think it's because Microsoft never really intended for it to be that way. If you want to get a feel for this, just try writing two programs that use ADO. One in C++, and the other in VBScript.

    So back to your question, when do you use COM? For me, the answer is if you are writing some kind of reusable component that is going to be called from another language, and that language is probably VB.

    For your .NET question, that's another Framework, and no it doesn't necessarily use COM underneath. There is a way to make use of COM from within .NET, which is called "pinvoke", and there is a way to make a .NET object behave as if it was a COM object (called COM Callable wrappers). But in general, they're two different technologies. In fact, I think MS encourages newer development to use .NET...they might even be calling COM "legacy" at this point...
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by siavoshkc
    It is good place to ask another question: .Net is a wrapper for COM or does the job itself?
    If you're still wondering about DirectX, the answer is yes: Managed DirectX is just a wrapper around the native COM DirectX.

    In general, no, COM and .Net have nothing to do with each other. Except that the COM-.Net interop services can make COM objects look like .Net classes to .Net and .Net classes like COM objects to native code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed