I have the following (very simple) program:
static void Main(string[] args)
{
// The program has 'using AutConnListTypeLibrary' in the header
AutConnList connections = new AutConnList();
connections.Refresh();
Debug.Print(connections.Count.ToString());
}
It is supposed to connect to all open PCOMM sessions, and return the number of open sessions (just to get started on something). However, when I run the program, I get the following run time error in the first line of code:
Unable to cast COM object of type 'System.__ComObject' to interface type 'AutConnListTypeLibrary.AutConnList'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3CB39CC1-6F18-11D0-910D-0004AC3617E1}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
I have the following references added to my project (besides the default ones):
Am I missing a reference? Or is there some wrong with my code? As mentioned, it is a run time error. The code compiles just fine.
As it turns out, there is quite a simple solution to this problem (although it's not very simple to understand). Simply add the [STAThread]
attribute to the Main()
method. The code from my original post should look like this:
[STAThread]
static void Main(string[] args)
{
// The program has 'using AutConnListTypeLibrary' in the header
AutConnList connections = new AutConnList();
connections.Refresh();
Debug.Print(connections.Count.ToString());
}