by Chad
11. June 2010 16:10
Can not call friend function on object which is not an instance of defining class
Google offered little help resolving this problem, so I thought I’d create this keyword-rich blog post so that someday it might climb the page ranks and help someone.
At work, we have an ASP.NET website that uses our Visual Basic 6 COM library via .NET Interops. Everything works quite well until you create a new .aspx page and forget to add the AspCompat=”true” @page directive attribute. It’s an easy thing to forget (it’s bitten me twice now.) The difficult part is that the COMException has an error message that is misleading: “Can not call friend function on object which is not an instance of defining class”.
AspCompat=”true”
The solution is actually very simple. The problem is caused because ASP.NET pages execute in a multithreaded apartment (MTA) and the VB6 COM Components expect to be in a single-threaded apartment (STA.) Adding the AspCompat=”true” attribute to the @page directive to force the page to execute in a STA.
The MSDN documentation does offer one word of caution about setting AspCompat=”true”. It can introduce performance issues if you instantiate your COM objects inside the page’s constructor. Microsoft recommends keeping that code in the page event handlers such as Page_Init and Page_Load.
6b93cd0d-0486-4781-89fa-92071ab99e4f|1|5.0
Tags:
ASP.NET | COM | Web