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.

Refresh COM Object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RC - 24 Sep 2005 19:16 GMT
Hi,

I have a Vb6 DLL Called from csharp which is a report object.
It can fetch data properly, however, when I run 2nd, 3rd, 4th time with
differenct report criteria, the fetched data still remained as same as the
first time. The object seems cannot refresh after first creation. It only
refresh when I manually kill the process of aspnet_wp.exe

How can I refresh the DLL object?

Code to create the Object:

Assembly asm = Assembly.LoadFrom("Lib.dll");
Type t = asm.GetTypes("Lib.CLib");
object o = Activator.CreateInstance(t);
Egbert Nierop (MVP for IIS) - 24 Sep 2005 20:04 GMT
> Hi,
>
[quoted text clipped - 11 lines]
> Type t = asm.GetTypes("Lib.CLib");
> object o = Activator.CreateInstance(t);

This is no good idea.

It is better to add a reference to the COM dll using Tlbimp.exe
add a strong name to the interop assembly and add THAT assembly to your .NET
project.

Second, you create the instance to the VB6 class as follows

Imagine, your VB6 class is called myObject

myObjectClass myvb6Object = new myObjectClass();
RC - 24 Sep 2005 21:03 GMT
forget to say, the DLL is generated from Tlbimp.exe

>> Hi,
>>
[quoted text clipped - 23 lines]
>
> myObjectClass myvb6Object = new myObjectClass();
Egbert Nierop (MVP for IIS) - 24 Sep 2005 23:01 GMT
> forget to say, the DLL is generated from Tlbimp.exe
>
>>> Hi,
>>>
>> myObjectClass myvb6Object = new myObjectClass();

This is really the correct way to do it. What you did is called reflection.
Isn't that overkill on COM objects?

myObjectClass myvb6Object = new myObjectClass();

And try this as well to really release the com instance (because GC might
delay the Release() of the com object, you need to dispose it manually)

System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
RC - 25 Sep 2005 06:46 GMT
Actually, I've tried put all COM objects to a collection and iterate and run
ReleaseComObject() through the collection within a dispose method. The
object call dispose() explicitly after generated the report. However, the
report seems does not refresh at next load time. And I think the object
still "hold" at memory. Any possible reason that cause this problem?

>> forget to say, the DLL is generated from Tlbimp.exe
>>
[quoted text clipped - 11 lines]
>
> System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
Egbert Nierop (MVP for IIS) - 25 Sep 2005 08:06 GMT
> Actually, I've tried put all COM objects to a collection and iterate and
> run ReleaseComObject() through the collection within a dispose method. The
> object call dispose() explicitly after generated the report. However, the
> report seems does not refresh at next load time. And I think the object
> still "hold" at memory. Any possible reason that cause this problem?

It looks like you compiled the VB project as single threaded. Each time you
use the new keyword, you -should- retrieve a new class instance (just as
with .NET classes).
How VB fixes to be threadsafe is a long story, but a normal VB class that
does not use module global variables should be fresh at ech new instance
(using the new keyword).

Since you are using reflection and an Activator, I don't know what's the
difference between the new keyword and the activator, better be safe than
sorry.
Robert Jordan - 24 Sep 2005 21:24 GMT
Hi

> I have a Vb6 DLL Called from csharp which is a report object.
> It can fetch data properly, however, when I run 2nd, 3rd, 4th time with
[quoted text clipped - 9 lines]
> Type t = asm.GetTypes("Lib.CLib");
> object o = Activator.CreateInstance(t);

You have to put <%@Page aspcompat="true" %> on the page that creates
the COM object.

Rob
RC - 24 Sep 2005 21:50 GMT
I've tried it. It seems not work.

> Hi
>
[quoted text clipped - 16 lines]
>
> Rob
Egbert Nierop (MVP for IIS) - 24 Sep 2005 22:56 GMT
> Hi
>
[quoted text clipped - 14 lines]
> You have to put <%@Page aspcompat="true" %> on the page that creates
> the COM object.

That is needed only for the VB6 class to function, if you use intrinsic
objects from the Asp environment (such as response/session etc)...

> Rob
Robert Jordan - 25 Sep 2005 09:35 GMT
Hi Egbert,

>>> I have a Vb6 DLL Called from csharp which is a report object.
>>> It can fetch data properly, however, when I run 2nd, 3rd, 4th time
[quoted text clipped - 16 lines]
> That is needed only for the VB6 class to function, if you use intrinsic
> objects from the Asp environment (such as response/session etc)...

Aspcompat is needed for all apartment threading COM objects.
W/out Aspcompat those objects will be created on a MTA thread,
leading to failures and very bad performance, because the
arguments of every method call will be marshaled.

Rob

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.