Imports SolidWorks.Interop.sldworks
Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.Management
Imports System.Runtime.InteropServices
Imports SolidWorks.Interop.swconst
Imports System

Partial Class SolidWorksMacro

#Region "Win32 imports"
    <DllImport("ole32.dll")> _
    Private Shared Function CreateItemMoniker(<MarshalAs(UnmanagedType.LPWStr)> ByVal lpszDelim As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszItem As String, ByRef ppmk As System.Runtime.InteropServices.ComTypes.IMoniker) As Integer
    End Function

    <DllImport("ole32.dll")> _
    Private Shared Function GetRunningObjectTable(ByVal reserved As UInteger, ByRef pprot As System.Runtime.InteropServices.ComTypes.IRunningObjectTable) As Integer
    End Function
#End Region

    Private Sub SolidWorksMacro_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

        Dim proxyFailed As Boolean = False
        Try
        Me.swApp = Me.SolidWorksApplication
        Catch ex As Exception

            proxyFailed = True
        End Try

        If Not proxyFailed Then
        Dim runResult As SolidWorks.RunMacroResult = RunMacro()

            If runResult = SolidWorks.RunMacroResult.NoneSpecified Then
	    Try
	     SwMacroSetup()
                Catch ex As Exception
                    System.Diagnostics.Debug.WriteLine(ex.ToString())
            End Try
                main()
	    Try
             SwMacroCleanup()
                Catch ex As Exception
                    System.Diagnostics.Debug.WriteLine(ex.ToString())
            End Try
        End If

            Return
        End If



        Try
            Dim sldProc As Process = GetParentProcess(Process.GetCurrentProcess())

            While sldProc IsNot Nothing
                If [String].Compare(sldProc.ProcessName, "sldworks", True) = 0 Then
                    Exit While
                Else
                    sldProc = GetParentProcess(sldProc)
                End If
            End While

            Dim rot As System.Runtime.InteropServices.ComTypes.IRunningObjectTable
            Dim ppmk As System.Runtime.InteropServices.ComTypes.IMoniker

            CreateItemMoniker(Nothing, "SolidWorks_PID_" & sldProc.Id.ToString(), ppmk)

            GetRunningObjectTable(0, rot)

            Dim swAppObj As Object = Nothing


            rot.GetObject(ppmk, swAppObj)


            If swAppObj IsNot Nothing Then
                Me.swApp = DirectCast(swAppObj, SldWorks)


            End If
        Catch ex As Exception

            Throw ex

        End Try

        main()


    End Sub

    Private Sub SolidWorksMacro_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    Private Function GetParentProcess(ByVal childProc As Process) As Process
        Dim childPID As Integer = childProc.Id


        Dim wqlQuery As New WqlObjectQuery("Select * from Win32_Process where ProcessID=" & childPID.ToString())
        Dim searcher As New ManagementObjectSearcher(wqlQuery)

        If searcher Is Nothing Then
            Return Nothing
        End If
        For Each disk As ManagementObject In searcher.[Get]()

            Dim procID As Integer = 0
            If Integer.TryParse(disk.GetPropertyValue("ParentProcessId").ToString(), procID) Then
                Return Process.GetProcessById(procID)


            End If
        Next

        Return Nothing

    End Function

End Class
