I have a MyObject that creates a static devDrv from an external DLL. If devDrv is created by the MyObject constructor. code works fine. If devDrv is created by one of the MyObject.Connect(see 2nd object below), the first call to MyObject.Connect() works fine. On the second call, I'll get this error: "COM object that has been separated from its underlying RCW cannot be used". It seems like the devDrv is not persistent. I would like this devDrv object to be created by MyObject.Connect as seen in the 2nd object below. I'll appreciate your help.
// this code works fine.
public sealed class MyObject
{
static ExtDeviceDriver devDrv;
public MyObject()
{
devDrv = new ExtDeviceDriver();
}
public void Connect()
{
devDrv.connect();
}
}
//this code causes exception.
public sealed class MyObject
{
static ExtDeviceDriver devDrv;
public MyObject()
{
// do not create devDrv here.
//devDrv = new ExtDeviceDriver();
}
public void Connect()
{
if (devDrv == null)
devDrv = new ExtDeviceDriver();
devDrv.connect();
}
}