Option Strict Off
Option Explicit On
Module modOpenSave

    ' Declarations used to force the window to stay on top of the other windows.
    Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

    Public Const HWND_TOPMOST As Short = -1
    Public Const SWP_NOMOVE As Short = &H2S
    Public Const SWP_NOSIZE As Short = &H1S

    ' Declarations used to read the registry.
    Public Const SYNCHRONIZE As Integer = &H100000
    Public Const KEY_NOTIFY As Integer = &H10S
    Public Const KEY_ENUMERATE_SUB_KEYS As Integer = &H8S
    Public Const KEY_QUERY_VALUE As Integer = &H1S
    Public Const READ_CONTROL As Integer = &H20000
    Public Const STANDARD_RIGHTS_READ As Integer = READ_CONTROL
    Public Const KEY_READ As Integer = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Public Const HKEY_LOCAL_MACHINE As Integer = &H80000002
    Public Const ERROR_SUCCESS As Short = 0

    Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Integer) As Integer
    Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Integer, ByVal lpSubKey As String, ByVal ulOptions As Integer, ByVal samDesired As Integer, ByRef phkResult As Integer) As Integer

    Public Const FILE_ATTRIBUTE_ARCHIVE As Short = &H20S 'An archive file (which most files are).
    Public Const FILE_ATTRIBUTE_COMPRESSED As Short = &H800S 'A file residing in a compressed drive or directory.
    Public Const FILE_ATTRIBUTE_DIRECTORY As Short = &H10S 'A directory instead of a file.
    Public Const FILE_ATTRIBUTE_HIDDEN As Short = &H2S 'A hidden file, not normally visible to the user.
    Public Const FILE_ATTRIBUTE_NORMAL As Short = &H80S 'An attribute-less file (cannot be combined with other attributes).
    Public Const FILE_ATTRIBUTE_READONLY As Short = &H1S 'A read-only file.
    Public Const FILE_ATTRIBUTE_SYSTEM As Short = &H4S 'A system file, used exclusively by the operating system.

    Public Const GENERIC_READ As Integer = &H80000000
    Public Const OPEN_EXISTING As Short = 3
    Public Const INVALID_HANDLE_VALUE As Short = -1
    Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
    Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Integer, ByRef lpFileSizeHigh As Integer) As Integer

    Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Integer, ByVal dwMilliseconds As Integer) As Integer
    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Integer, ByRef lpExitCode As Integer) As Integer
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer

    ' Declares used for creating temporary file.
    Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Integer, ByVal lpTempFileName As String) As Integer
    Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Integer, ByVal lpBuffer As String) As Integer
    Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Integer
    Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Integer, ByVal lpBuffer As String, ByVal lpFilePart As String) As Integer
    Private Const MAX_PATH As Short = 260

    Public Enum DocumentTypes
        typPartDocument = 1
        typAssemblyDocument = 2
        typDraftDocument = 3
        typBadDocument = 4
        typWeldmentDocument = 5
    End Enum

    ' Declares used to call the paracis converter for the files containg acisbodies.
    Public newFilePath As String
    Public ret0 As Short
    Public MsgString As String

    'Public Declare Function paTranslate Lib "uicalls.dll" (ByRef ProcessOptions As ParacisOptions) As Short
    Public Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Integer
    Public Declare Function SetFileAttributes Lib "kernel32.dll" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Integer) As Integer
    Public Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer

    Public Structure SECURITY_ATTRIBUTES
        Dim nLength As Integer
        Dim lpSecurityDescriptor As Integer
        Dim bInheritHandle As Integer
    End Structure


    Public Structure STARTUPINFO
        Dim cb As Integer
        Dim lpReserved As String
        Dim lpDesktop As String
        Dim lpTitle As String
        Dim dwX As Integer
        Dim dwY As Integer
        Dim dwXSize As Integer
        Dim dwYSize As Integer
        Dim dwXCountChars As Integer
        Dim dwYCountChars As Integer
        Dim dwFillAttribute As Integer
        Dim dwFlags As Integer
        Dim wShowWindow As Short
        Dim cbReserved2 As Short
        Dim lpReserved2 As Integer
        Dim hStdInput As Integer
        Dim hStdOutput As Integer
        Dim hStdError As Integer
    End Structure
    Public Structure PROCESS_INFORMATION
        Dim hProcess As Integer
        Dim hThread As Integer
        Dim dwProcessId As Integer
        Dim dwThreadId As Integer
    End Structure

    Public Const NORMAL_PRIORITY_CLASS As Integer = &H20
    Public Const INFINITE As Short = -1
    Public Const CREATE_NO_WINDOW As Integer = &H8000000
    Public blnChangedStatus As Boolean
    Public lngStatus As Integer
    Public blnChangedAccess As Boolean
    Public lngPermanentAccess As Integer
    Public strSEStatus As String
    Public strSEInstalledPath As String
    Public strSEVersion As String
    Public strRegistryBase As String

    ' Forces a window to the top and keeps it on top.
    Public Sub WindowToTop(ByRef hWnd As Integer)
        Call SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    End Sub

    Public Function GetTempFile() As String

        GetTempFile = System.IO.Path.GetTempFileName()

    End Function


    Private Function GetTempDir() As String
        Dim sRet As String
        Dim C As Integer
        sRet = New String(Chr(0), MAX_PATH)
        C = GetTempPath(MAX_PATH, sRet)
        GetTempDir = Left(sRet, C)
    End Function

    Public Function GetStatus(ByRef File As String) As String
        ' function supports processing of Released, baselined or obselete documents.
        Dim objPropertySets As SolidEdgeFileProperties.PropertySets = Nothing
        Dim objProperties As SolidEdgeFileProperties.Properties = Nothing
        Dim objProperty As SolidEdgeFileProperties.Property = Nothing
        Dim OpenReadOnly As Object

        GetStatus = ""
        OpenReadOnly = "False"
        objPropertySets = New SolidEdgeFileProperties.PropertySets

        If Not (objPropertySets Is Nothing) Then
            objPropertySets.Open(File)
            objProperties = objPropertySets.Item("ExtendedSummaryInformation")
            If Not objProperties Is Nothing Then
                Try
                    objProperty = objProperties.Item("Status")
                Catch ex As Exception
                    objProperty = objProperties.PropertyByID(7)
                End Try
            End If
            If Not objProperty Is Nothing Then
                GetStatus = GetStatusByID(objProperty.Value)
            End If
        End If

        ' now need to do .Net garbage collection to release all the objects so SE can open the file read-write.
        If Not objProperty Is Nothing Then System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objProperty)
        If Not objProperties Is Nothing Then System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objProperties)
        If Not objPropertySets Is Nothing Then System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objPropertySets)
        objPropertySets = Nothing
        objProperties = Nothing
        objProperty = Nothing
        GarbageCollection()

    End Function


    Public Function GetStatusByID(ByVal StatusID As Short) As String
        GetStatusByID = ""
        Select Case StatusID
            Case SolidEdgeConstants.DocumentStatus.igStatusAvailable
                GetStatusByID = "Available"
            Case SolidEdgeConstants.DocumentStatus.igStatusInWork
                GetStatusByID = "In Work"
            Case SolidEdgeConstants.DocumentStatus.igStatusInReview
                GetStatusByID = "In Review"
            Case SolidEdgeConstants.DocumentStatus.igStatusReleased
                GetStatusByID = "Released"
            Case SolidEdgeConstants.DocumentStatus.igStatusBaselined
                GetStatusByID = "Baselined"
            Case SolidEdgeConstants.DocumentStatus.igStatusObsolete
                GetStatusByID = "Obsolete"
        End Select
    End Function

    Public Sub GarbageCollection()
        System.GC.Collect(System.GC.MaxGeneration)
        System.GC.WaitForPendingFinalizers()
        System.GC.Collect(System.GC.MaxGeneration)  'per MS, should do this again to be sure everything is released.

    End Sub

End Module