1

I'm doing a Microsft PowerPoint add-in solution whit VSTO that reads info from a hardware, hardware developers gave me their SDK to control this hardware but I'm having problems trying to control it.

I have this library ARS

There is a class ARS.BaseConnection

I have this variable ARS.BaseConnection BaseConn;

The problem is when a I create a new Object of type BaseConnection

BaseConn = new ARS.BaseConnection();

Debugger doesn't show any exception, POWERPNT.exe just crashes and stops.

I tried to debug POWERPNT and it says Access violation writing location 0x00d20f78. But I'm not programming PowerPoint.

I fount that the demo program in SDK(that actually works) has a [STAThread] before main, so I think it must be run as STA so I create a new thread:

    ARS.BaseConnection BaseConn;`

    public form1()
    {
        InitializeComponent();

        System.Threading.Thread thread = new System.Threading.Thread(createBase);
        thread.SetApartmentState(System.Threading.ApartmentState.STA);
        thread.Start();
        thread.Join();

        BaseConn.Open(); // There is the problem, when I'm trying to use open() BaseConn debuger says: COM object that has been separated from its underlying RCW cannot be used.
    }

    private void createBase()
    {
        BaseConn = new ARS.BaseConnection(); //If it runs in a STA tread doesn't crash.
    }

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

How can I make this works?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
oscargilfc
  • 339
  • 1
  • 12
  • The code you posted is just not legal, the object is dead after the thread ends. You will need help from the vendor to chase this crash. – Hans Passant Jun 26 '12 at 21:10

1 Answers1

0

That object can only be accessed from its creating thread.

If you need to access the object from another thread, you can switch to its thread using a dispatcher.

Consider creating it on the main thread (set the main thread to STA first).

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • 1
    The problem is there is not main thread, is a PowerPoint Add-In, I try to use [STAThread] before public Form1, before createBase(), but still crashing, I don't know if the problem is not that must be STA o it doesn't changes to STA. – oscargilfc Jun 26 '12 at 18:55
  • Can you place a break point and see if the current thread is STA or not? (And perhaps if it is the main thread or not.) Also try using "windbg" and "debugging tools for windows" to examine the threads in the process. – Danny Varod Jun 26 '12 at 19:36