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 / December 2004

Tip: Looking for answers? Try searching our database.

How to dispose a .NET class called from VB6?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
koojo31@hotmail.com - 14 Dec 2004 23:22 GMT
I have a VB6 application that calls a .NET class in C#.  I compiled the
.NET class with COM Interop set to true (output to a .tlb file), and I
can run the app and call the .NET class no problem.  However, I am
having trouble disposing of the .NET class when I am done with it.

I created a public method (see below) in the .NET class that is called
from the VB6 app, but it gives me the following exception
("System.StackOverflowException"), and is still referenced (it handles
events from another application).  Any ideas how I can release it?

Here is the procedure that is in the .NET class and called from the VB6
application:

public void Dispose()
{
try
{
this.Dispose();
}
catch (Exception ex)
{
               MessageBox.Show(ex.Message);
       }
}
Phil Wilson - 14 Dec 2004 23:32 GMT
Well you're overflowing your stack because you're recursing. Dispose should
be releasing unmanaged resources, depending on your environment there.
Signature

Phil Wilson [MVP Windows Installer]
----

> I have a VB6 application that calls a .NET class in C#.  I compiled the
> .NET class with COM Interop set to true (output to a .tlb file), and I
[quoted text clipped - 20 lines]
>         }
> }
koojo31@hotmail.com - 14 Dec 2004 23:56 GMT
Forgive me for I'm still quite new to this, but what would be the best
way to go about releasing a .NET class instantiated from a VB6
application then?
Phil Wilson - 15 Dec 2004 14:53 GMT
There's nothing wrong with your C# class implementing a Dispose method and
having your COM clients call it, but:
1. Dispose is typically just between .NET code. The class implements
IDisposable, clients can test for that Interface and call Dispose().
2. You have to implement it. Your example Dispose just called itself until
it overflowed the stack.
3. What resources does your C# class own that makes you think you need to
use Dispose?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
conimplementingdisposemethod.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/c
pconFinalizeDispose.asp

Signature

Phil Wilson
[Microsoft MVP-Windows Installer]
Definitive Guide to Windows Installer
http://apress.com/book/bookDisplay.html?bID=280

> Forgive me for I'm still quite new to this, but what would be the best
> way to go about releasing a .NET class instantiated from a VB6
> application then?
koojo31@hotmail.com - 15 Dec 2004 18:08 GMT
Well, here's my scenario:
I have an extension (toolbar created in VB6) to an application.  I want
to include some .NET controls in the extension (ie. the user clicks on
a button on the toolbar and the extension calls a .NET class which
opens a .NET form with .NET controls).  This class also handles events
that are raised from the application (I pass a reference to the
application from the VB6 extension to the .NET class).  Now, when the
user closes the .NET form, I want all references to the application to
be cleared so that the event doesn't fire in the .NET class anymore.

However, in my situation the event is still firing from the .NET class
even after directly calling the Dispose method (below) from VB6.
(Although I no longer get the StackOverflowException).

~clsWindow()
{
Dispose(false);
}

// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// Take yourself off the Finalization queue
// to prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// this object is a .NET form called by the .NET class
m_objNETWindow.Dispose();
}
// These objects were passed in from the VB6 application
Marshal.ReleaseComObject(m_pAVEvents);
Marshal.ReleaseComObject(m_pMxDoc);
Marshal.ReleaseComObject(m_pMap);
}
            disposed = true;        
        }

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.