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 / Visual Studio.NET / IDE / February 2006

Tip: Looking for answers? Try searching our database.

VB interface to VC++ ATL slow

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dbaechtel - 16 Feb 2006 14:28 GMT
I have a VB.Net application in VS2005 that communicates to a ATL written in
VC++ loaded as a project resource. The communications with the ATL DLL
library work well in the VB primary thread. But if I try and communicate in
the same way with the ATL in a secondary thread (System.Threading.Thread),
then the communications speed with the ATL slows down by a factor of about
100X. I am assuming that tis is beacuse of some marshalling or other overhead
that is done differently in the secodary thread. How can I communicate with
the ATL in a secodary thread and still get the same performance as the VB
primary thread? Is there a better way to get multithreaded access to the ATL
without a performance hit? PLEASE HELP.
"Peter Huang" [MSFT] - 27 Feb 2006 08:22 GMT
Dear Customer,

Based on my understanding, you are accessing the ATL DLL from VB.NET from
the second thread, but the performance will be lower.
If I have any misunderstanding, please feel free to post here.
1. If we are trying to access unmanaged COM DLL, the .NET code will call
the ATL DLL via RCW wrap, so the performance will be lower than access from
unmanaged code directly. It seems that this is not the case here. Just for
your information.
2. If an ATL object can be accessed from multiple thread depends on the ATL
DLL Developer. If this ATL DLL can live in a MTA apartment, if it has
implemented the multithread accessing synchronization mechanism.
3. Eveny COM Object(ATL object) will have an Threading Modal option in the
registry. So that when the COM Context is trying to activate the object, it
will know where and how to create the object. Such as STA,MTA and
Both(which means the COM Object supports both STA and MTA).

? ThreadingModel value not present: Supports single-threading model.
? ThreadingModel=Apartment : Supports STA model.
? ThreadingModel=Both : Supports STA and MTA model.
? ThreadingModel=Free : Supports only MTA.

If the ATL DLL did not set the ThreadingModel (the first option), then the
DLL will be loaded into the first STA.

Client     Server     Result
------     ------     -----------------------------------------
STA0       None       Direct access; server loaded into STA0
STA*       None       Proxy access; server loaded into
                     STA0.
MTA        None       Proxy access; server loaded into STA0; STA0 created
                     automatically by COM if necessary;
STA0       Apt        Direct access; server loaded into STA0
STA*       Apt        Direct access; server loaded into STA*
MTA        Apt        Proxy access; server loaded into an
STA created automatically by COM.
STA0       Free       Proxy access; server is loaded into MTA
MTA created automatically by COM if necessary.
STA*       Free       Same as STA0->Free
MTA        Free       Direct access
STA0       Both       Direct access; server loaded into STA0
STA*       Both       Direct access; server loaded into STA*
MTA        Both       Direct access; server loaded into the MTA

If the ATL COM server did not provide the ThreadingModel, the following
scenario will occur.

STA*       None       Proxy access; server loaded into STA0.

Because the Server did not set ThreadingModal, so the COM infrastructure
will load the ATL DLL into the STA0, i.e. the first STA.
Commonly this is the VB primary thread per your description. That is why it
will access quickly if we create it in the VB primary thread.

STA0       None       Direct access; server loaded into STA0

But now if we create another thread to create the ATL DLL, then the new
created thread is in STA*. But the ATL DLL will still be created in the  
STA0 based on the rule above.

STA*       None       Proxy access; server loaded into STA0.

Now the access will across the Proxy, so the performance will be slower.

The above is just my guess. I did not enumerate all the possibility.

Here is KB for your reference.
INFO: Descriptions and Workings of OLE Threading Models
http://support.microsoft.com/kb/q150777/

STA*       Both       Direct access; server loaded into STA*
STA*       Apt        Direct access; server loaded into STA*

So far I think our goal is to set the threading Modal to Both or Apartment.
So for detailed information about the Threading Modal, I think you need to
contact the ATL developer.

Also based on my test, the default setting for a ATL project in VS.NET 2005
will be Apartment.
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\CLSID\{D7CA6E5B-F292-4E40-80A2-966EC1824CDE}\InprocServer
32]
"ThreadingModel"="Apartment"

We can check the ThreadingModel in the registry key above.

NOTE:  the GUID({D7CA6E5B-F292-4E40-80A2-966EC1824CDE}) will differ.

Thanks!

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


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.