[API]
LocalAlloc
LocalFree

[DEFINE]
Public ptrTemp As Long
Private Const ptrTempSize As Long = 32000 ' int (signed?), so crashes with some apis
Public Const ptrTempSizeX As Long = ptrTempSize - 1&
Public Const ptrTempSizeXW As Long = (ptrTempSize \ 2&) - 1&
#If Unicode Then
  #Else
  Public Const IsUnicode As Boolean = False
  #End If
#If SetIsVistaOrXP Then
  Public IsVistaOrXP As Boolean
  #End If

[CODE]
#If LoadResEx_IconStuff Then
Public Function ApplicationStart(Optional ByVal MaxIconSupport As Long = 0&) As Boolean
#Else
Public Function ApplicationStart() As Boolean
#End If
  #If XPTheme_DefControls Or LoadResEx_IconStuff Or DoEventsEx_Sleep Or SetIsVistaOrXP Then
    Dim xLong1 As Long
    #End If
  #If LoadResEx_IconStuff Or DoEventsEx_Sleep Or SetIsVistaOrXP Then
    Dim Major As Long
    Dim Minor As Long
    #If Unicode Then
      #Else
      Dim IsUnicode as Boolean
      #End If
    #End If

  If ptrTemp = 0& Then
    ptrTemp = LocalAlloc(0&, ptrTempSize)
    If Not ptrTemp = 0& Then
      ApplicationStart = True
      #If XPTheme_DefControls Then
        xLong1 = LoadLibraryA("shell32.dll")
        Call InitCommonControls
        Call FreeLibrary(xLong1)
        #End If

      #If LoadResEx_SetLanguageFile Then
        LoadResEx_LanguageFile_hModule = App.hInstance
        #End If

      #If LoadResEx_IconStuff Or DoEventsEx_Sleep Or SetIsVistaOrXP Then
        xLong1 = GetVersion
        IsUnicode = ((xLong1 And &H80000000) = 0&)
        #ElseIf Unicode Then
        IsUnicode = ((GetVersion And &H80000000) = 0&)
        #End If
      
      #If LoadResEx_IconStuff Or DoEventsEx_Sleep Or SetIsVistaOrXP Then
        Major = (xLong1 And &HFF&)
        Minor = (xLong1 And &HFF00&) \ &H100&
        #If SetIsVistaOrXP Then
          IsVistaOrXP = IsUnicode And ((Major > 5&) Or ((Major = 5&) And (Minor >= 1)))
          #End If
        #If DoEventsEx_Sleep Then
          DoEventsEx_Sleep_WaitableTimers = (IsUnicode Or (Major > 4&) Or ((Major = 4&) And (Minor >= 10&)))
          #End If
        #If LoadResEx_IconStuff Then
          If MaxIconSupport > 0& Then
            xLong1 = GetDC(0&)
            If Not xLong1 = 0& Then
              LoadResEx_IsHighColor = (GetDeviceCaps(xLong1, 12&) > 15&)
              Call ReleaseDC(0&, xLong1)
              xLong1 = GetVersion
              If IsUnicode Then
                'nt series; '2003=5.2
                If Major > 5& Or Major = 5& And Minor > 1& Then
                  If LoadResEx_IsHighColor Then
                    LoadResex_Caps_WinDIcon = LoadResEx_IconOS_WinVista
                    Else
                    LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win2000ME_16Color 'LoadResEx_IconOS_WinVista_16Color
                    End If
                  LoadResex_Caps_TrayIcon = LoadResex_Caps_WinDIcon
                  ElseIf Major = 5& Then
                  If Minor >= 1& Then
                    If LoadResEx_IsHighColor Then
                      LoadResex_Caps_WinDIcon = LoadResEx_IconOS_WinXP
                      Else
                      LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win2000ME_16Color
                      End If
                    LoadResex_Caps_TrayIcon = LoadResex_Caps_WinDIcon
                    Else
                    If LoadResEx_IsHighColor Then
                      LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win2000ME
                      Else
                      LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win2000ME_16Color
                      End If
                    LoadResex_Caps_TrayIcon = LoadResEx_IconOS_Win2000ME_16Color
                    End If
                  Else
                  If LoadResEx_IsHighColor Then
                    LoadResex_Caps_WinDIcon = LoadResEx_IconOS_WinNT
                    'Else
                    'LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win95_16Color
                    End If
                  'LoadResex_Caps_TrayIcon = LoadResEx_IconOS_Win95_16Color
                  End If
                Else
                If Major > 4& Or Major = 4& And Minor >= 90& Then
                  If LoadResEx_IsHighColor Then
                    LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win2000ME
                    Else
                    LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win2000ME_16Color
                    End If
                  LoadResex_Caps_TrayIcon = LoadResex_Caps_WinDIcon
                  ElseIf Major = 4& And Minor >= 10& Then
                  If LoadResEx_IsHighColor Then
                    LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win98
                    'Else
                    'LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win95_16Color
                    End If
                  'LoadResex_Caps_TrayIcon = LoadResEx_IconOS_Win95_16Color
                  Else
                  If LoadResEx_IsHighColor Then
                    LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win95
                    'Else
                    'LoadResex_Caps_WinDIcon = LoadResEx_IconOS_Win95_16Color
                    End If
                  'LoadResex_Caps_TrayIcon = LoadResEx_IconOS_Win95_16Color
                  End If
                End If
              End If
            If LoadResex_Caps_WinDIcon > MaxIconSupport Then
              LoadResex_Caps_WinDIcon = MaxIconSupport
              If LoadResex_Caps_TrayIcon > MaxIconSupport Then LoadResex_Caps_TrayIcon = MaxIconSupport
              End If
            'If ForceHiColorTrayIcons Then LoadResex_Caps_TrayIcon = LoadResex_Caps_WinDIcon
            End If
          #End If
        #End If

      #If XPTheme_Auto Then
        Call UpdateTheme
        #End If
      End If
    #If CreateAssemblyFromHexString Then
      Call CreateAssembliesFromHexString
      #End If
    End If
  End Function

Public Sub ApplicationClose()
  #If GDIPlus Then
    If Not GDIPlus_Count = 0& Then
      Call GdiplusShutdown(GDIPlus_Token)
      End If
    #End If
  #If XPTheme_Auto Then
    Call UnTheme()
    #End If
  Call LocalFree(ptrTemp)
  End Sub
