Attaching to existing SW session and then unloading SW from Task Manager

Oct 18, 2004 2 Replies

Hello,



I want my application to attach to the EXISTING running SW session. With the following code bellow this is what happens:



If SW is NOT running, my code OPENS a new SW session If SW is running, my code (sometimes) STILL OPENS a new SW session



....................Questions........................


1.- How can I force the Task Manager to unload SW from the memory?
2.- How can I modify my code so the behaviour is the following...?

If SW is NOT running -> do wgatever (print a string, show an error msg,... but DO NOT open a new SW session



If SW is tunning -> attach to current SW session


ps: I'm using Visual.NET and SW2003 Student Edition



Thank you,



JK



formatting link


'------------- My Code -------------------



Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



swApp As SldWorks.SldWorks



'START connection to SW session************************* swApp = Nothing 'swApp = GetObject(, "Sldworks.Application") '?????????????????? swApp = CreateObject("SldWorks.Application")



If swApp Is Nothing Then Call MsgBox("connection failed, (try opening SW)", vbOKOnly, "Error") End Else 'swApp Is Not Nothing swApp.Visible = True swApp.UserControl = True On Error Resume Next ' Disables VB's implicit error on QI End If Me.Text = "Type Helper (swDocType)" 'END connection to SW session*************************



End Sub


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This code is working without the SET command ?

BTW, both will work (with SET) under different circumstances: try the code below:

'------------------------------------------------------------------

' attach to existing SWX On Error Resume Next ' try attachiung to existing SWX Set swapp = GetObject(, "Sldworks.Application") On Error GoTo 0 ' Start new SWX if not running If swapp Is Nothing Then Set swapp = GetObject("", "Sldworks.Application") End If

'------------------------------------------------------------------

The problem is that .NET is not running garbage collection on the COM object because it doesn't know how to.

After you call swApp.exit, you need to run this line of code:

system.runtime.marshal.releasecomobject(swApp)

This will force the GC and release the COM object.

I would also use the GetObject line b/c if SW is running, it will get a handle to it and if not, it will launch SW. You could also do Dim swApp as sldworks.sldworks=new sldworks.sldworks.

Evan

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required