简体   繁体   中英

Prevent decreasing COM reference counter when application closes down

I've created a UI front-end (called "B") using VC# for another application (let's call it "A"). B is a stand-alone EXE, which must be (due to some reasons) called by A. (DLL is not possible due to security policies.)

The current flow is:

  1. Start A (which is acting as COM server).
  2. Let A execute B using a system() call.
  3. B creates a COM connection to A.
  4. User closes B
  5. A is closed, too. <<< THIS is the problem!

The desired flow is:

...(5) A is NOT closed.

As far as I found out, I believe that the reason for the undesired behaviour is that the COM reference counter is automatically decreased. Because B is using the existing A instance, the counter is previously 1, therefore decreased to 0, leading to the complete shutdown of A. (This is a real problem because the user may loose data in A at that moment.)

I already tried to influence the GC to keep A alive, but with no success.

The question is:

How can I prevent the shutdown of A , eg, by avoiding the decrementing of the reference counter?

如果您可以访问A中的com对象,则可以对其执行Marshal.AddRef来增加引用计数器,即声明A也正在使用COM对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM