Open Edge OR Get a Running Instance

OK, this is probably a very easy one but i can't seem to figure it out!!

 

I have this to open a new instance of Edge:

 

[code type=vbnet]

Dim objApplication As As SolidEdgeFramework.Application

objApplication = CreateObject("SolidEdge.Application")

[/code]

 

...and this to grab an already running instance of Edge:

 

[code type=vbnet]

Dim objApplication As SolidEdgeFramework.Application

objApplication = Marshal.GetActiveObject("SolidEdge.Application")

[/code]

 

So how do I check if Edge is running, use the running instance if there is one, otherwise launch a new instance?

 

Thanks in advance.

 

Alex

 

Posted by: Alex Lush
Post date: 8/2/2013 6:23:50 AM

1 REPLY

Open Edge OR Get a Running Instance

Download the [url=http://solidedgesamples.codeplex.com]Samples for Solid Edge[/url] on CodePlex. This is the project that I'm actively working on to demonstrate best practices.

 

Here is the relevant snippet of what you're looking for.

 

[code type=vbnet]

Imports System.Runtime.InteropServices

Imports System.Text

 

Namespace SolidEdge.Automation

''' <summary>

''' Solid Edge utility class.

''' </summary>

Friend NotInheritable Class SolidEdgeUtils

 

Private Sub New()

End Sub

 

''' <summary>

''' Creates and returns a new instance of Solid Edge.

''' </summary>

''' <returns>

''' An object of type SolidEdgeFramework.Application.

''' </returns>

Public Shared Function Start() As SolidEdgeFramework.Application

' On a system where Solid Edge is installed, the COM ProgID will be

' defined in registry: HKEY_CLASSES_ROOT\SolidEdge.Application

Dim t As Type = Type.GetTypeFromProgID(progID:= "SolidEdge.Application", throwOnError:= True)

 

' Using the discovered Type, create and return a new instance of Solid Edge.

Return CType(Activator.CreateInstance(type:= t), SolidEdgeFramework.Application)

End Function

 

''' <summary>

''' Connects to a running instance of Solid Edge.

''' </summary>

''' <returns>

''' An object of type SolidEdgeFramework.Application.

''' </returns>

Public Shared Function Connect() As SolidEdgeFramework.Application

Return Connect(startIfNotRunning:= False)

End Function

 

''' <summary>

''' Connects to a running instance of Solid Edge with an option to start if not running.

''' </summary>

''' <returns>

''' An object of type SolidEdgeFramework.Application.

''' </returns>

Public Shared Function Connect(ByVal startIfNotRunning As Boolean) As SolidEdgeFramework.Application

Try

' Attempt to connect to a running instance of Solid Edge.

Return CType(Marshal.GetActiveObject(progID:= "SolidEdge.Application"), SolidEdgeFramework.Application)

Catch ex As System.Runtime.InteropServices.COMException

Select Case ex.ErrorCode

' Solid Edge is not running.

Case NativeMethods.MK_E_UNAVAILABLE

If startIfNotRunning Then

' Start Solid Edge.

Return Start()

Else

' Rethrow exception.

Throw

End If

Case Else

' Rethrow exception.

Throw

End Select

Catch

' Rethrow exception.

Throw

End Try

End Function

End Class

End Namespace

[/code]

 

Posted by: Jason Newell
Post date: 8/2/2013 6:28:27 AM