Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. COM object that has been separated from its underlying RCW cannot be used.

COM object that has been separated from its underlying RCW cannot be used.

Scheduled Pinned Locked Moved C#
questioncomdata-structureshelp
1 Posts 1 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I am using a third party reference, which is a COM object. The COM object has an Open and Close function and I need to make sure that Close is always called whenever possible. I wrote a wrapper class that implements IDisposable and in the Dispose function, I try to call the Close function. However I get a System.Runtime.InteropServices.InvalidComObjectException when I try to call the close function with additional details of "COM object that has been separated from its underlying RCW cannot be used."

    public class MyWrapper : IDisposable
    {
    private Session _mySession = new Session(); // This is the COM object

    public void Open() { \_MySession.Open(); }
    public void Close() { \_MySession.Close(); }
    
    public void Dispose() {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    
    public void Dispose(bool disposing) {
        \_MySession.Close(); // Calling close without having called open is OK
    }
    ~MyWrapper() { Dispose(false); }    
    

    }

    From my (limited) understanding, there are two reasons why I am getting the exception. 1) The Finalizer is running and calling the destructor, but because the finalizer is on a separate thread I get the error. 2) Based on this Stack Overflow[^] post, the Runtime Callable Wrapper (RCW) has it's own finalizer which is being called before mine, and getting rid of the COM object. I don't know if either of these, or both of these are true, but what is the recommended way to implement a COM object that needs to have finalizer code run?

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups