Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2005

Tip: Looking for answers? Try searching our database.

ActiveX and casting?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Karen Kenworthy - 27 Sep 2005 18:16 GMT
Hope you don't mind some questions from an interop newbie ('m not even sure
what questions to ask, but I want to learn) ...

I'm trying to access a COM object (the MS Agent server) in VB.Net 2005.  I
can create the object that represents the server, access its methods and
properties, and more.  So far so good.

But at one point I need to obtain a reference to an interface the server
object supports, then call a method on that interface.  Here's some sample
code that some VB6 MS Agent folks think should work:

----------
dim AgentEx as IAgentEx

(code that creates and initializes AgentEx)
Dim ps As IAgentPropertySheet

ps = AgentEx ' this object should support the IAgentPropertySheet interface

(or)

ps = CType(objServer, IAgentPropertySheet)

----------

Both my attempts to assign a valueto ps fail with
System.InvalidCastException.

One oddity is that the code that initializes AgentEx uses a similar
technique:

Dim objServer = new AgentServer

Dim objAgentEx = objServer

And that works.  It's just the attempt to gain access to the
IAgentPropertySheet that fails.

Any ideas or suggestions?

Is there a way to query a COM object to find what Interfaces it supports (to
be sure IAgentPropertySheet is actually supposed to work)?

I don't know if this is a problem with my understanding of COM interop
(extremely limited to non-existent!).  Or with MS Agent.
Karen Kenworthy - 28 Sep 2005 03:33 GMT
Turns out this was known problem with COM objects that use the STA
(Single-Threaded Apartment) threading model
(http://support.microsoft.com/Default.aspx?kbid=309330). One of the
workarouds (the one I've chosen) is to perform the object access on a new
STA thread. So now, where I had originally planned to execute this code:

Dim ps as IAgentPropertySheet
ps = objAgentEx ' type = IAgentEx
ps.SetVisible(True)

I now run this code:
Dim t As New Threading.Thread(AddressOf DispPropSheet)

t.SetApartmentState(Threading.ApartmentState.STA)

t.Start()

Threading.Thread.Sleep(0)

t.Join()

The orginal code is in the DispPropSheet sub.

Voila! I can now display the property sheet.  And the character, and my main
program, continue to run alongside it!

Karen Kenworthy
http://www.karenware.com/

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.