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

Discussion in 'SolidWorks' started by Julius Klein, Oct 19, 2004.

  1. Julius Klein

    Julius Klein Guest

    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

    www.juliusklein.tk


    '------------- 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
     
    Julius Klein, Oct 19, 2004
    #1
  2. Julius Klein

    rocheey Guest

    'swApp = GetObject(, "Sldworks.Application") '??????????????????
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    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

    '------------------------------------------------------------------
     
    rocheey, Oct 19, 2004
    #2
  3. 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
     
    Evan T. Basalik, Oct 21, 2004
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.