home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / Epsita_Ant21314110212008.psc / CEnvironment.cls < prev    next >
Text File  |  2005-04-22  |  15KB  |  422 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = 0   'False
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CEnvironment"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. '* Description : Class for retrieving environment parameters.
  15.  
  16. Option Explicit
  17.  
  18. ' Error handling definitions
  19. Private Const E_ERR_BASE = 17380 + vbObjectError
  20. Public Enum EErrEnvironment
  21.     eErrEnvironment_CannotGetEnvironmentVariable = E_ERR_BASE + 1
  22.     eErrEnvironment_CannotSetEnvironmentVariable
  23.     eErrEnvironment_CannotGetOsName
  24.     eErrEnvironment_UnknownOperatingSystem
  25.     eErrEnvironment_ComponentFailure
  26. End Enum
  27. Private Const S_ERR_CannotGetEnvironmentVariable = "Cannot get environment variable"
  28. Private Const S_ERR_CannotSetEnvironmentVariable = "Cannot set environment variable"
  29. Private Const S_ERR_CannotGetOsName = "Cannot get operating system name"
  30. Private Const S_ERR_UnknwonOperatingSystem = "Unknown operating system"
  31. Private Const S_ERR_ComponentFailure = "CEnvironment component failure"
  32.  
  33. ' Public class enums
  34. Public Enum EVbAppRunMode
  35.     eVbAppRunMode_Compiled = 1
  36.     eVbAppRunMode_FromIDE
  37.     eVbAppRunMode_Unknown
  38. End Enum
  39.  
  40. ' Private class type definitions
  41. Private Type OSVERSIONINFO
  42.     dwOSVersionInfoSize As Long
  43.     dwMajorVersion As Long
  44.     dwMinorVersion As Long
  45.     dwBuildNumber As Long
  46.     dwPlatformId As Long
  47.     szCSDVersion As String * 128
  48. End Type
  49.  
  50. Private Type OSVERSIONINFOEX
  51.     dwOSVersionInfoSize As Long
  52.     dwMajorVersion As Long
  53.     dwMinorVersion As Long
  54.     dwBuildNumber As Long
  55.     dwPlatformId As Long
  56.     szCSDVersion As String * 128
  57.     wServicePackMajor As Integer
  58.     wServicePackMinor As Integer
  59.     wReserved(1) As Integer
  60. End Type
  61.  
  62. ' Private class constants
  63. Private Const MAX_LENGTH = 512
  64. Private Const VER_PLATFORM_WIN32s = 0
  65. Private Const VER_PLATFORM_WIN32_WINDOWS = 1
  66. Private Const VER_PLATFORM_WIN32_NT = 2
  67.  
  68. ' Private class API function declarations
  69. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  70. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  71. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  72. Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal Path As String, ByVal cbBytes As Long) As Long
  73. Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
  74. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFOEX) As Long
  75. Private Declare Function ExpandEnvironmentStrings Lib "kernel32" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nSize As Long) As Long
  76. Private Declare Function APISetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
  77. Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
  78.  
  79. ' Private variables to hold property values
  80. Private m_OSVersion As OSVERSIONINFOEX
  81.  
  82.  
  83. '*****************************************************************************************
  84. '* Property    : ComputerName
  85. '* Notes       : Returns the computer name of the current system.
  86. '*****************************************************************************************
  87. Public Property Get ComputerName() As String
  88.     On Error GoTo hComponentFailure
  89.     
  90.     Dim s       As String
  91.     Dim apiRet  As Long
  92.     Dim lSize   As Long
  93.     
  94.     s = Space$(MAX_LENGTH)
  95.     lSize = Len(s)
  96.     
  97.     apiRet = GetComputerName(s, lSize)
  98.     If apiRet Then
  99.         If lSize > Len(s) Then
  100.             s = Space$(lSize + 1)
  101.             lSize = Len(s)
  102.             apiRet = GetComputerName(s, lSize)
  103.         End If
  104.     End If
  105.     
  106.     ComputerName = IIf(lSize > 0, Left$(s, InStr(s, vbNullChar) - 1), "")
  107.  
  108.     Exit Property
  109.  
  110. hComponentFailure:
  111.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  112. End Property
  113.  
  114.  
  115. '*****************************************************************************************
  116. '* Property    : OsName
  117. '* Notes       : Returns a string value containing the operating system's name.
  118. '*               Possible return values are WinNT4, Win95, Win98 etc.
  119. '*****************************************************************************************
  120. Public Property Get OsName() As String
  121.     On Error GoTo hComponentFailure
  122.     
  123.     Dim sTemp As String
  124.     
  125.     sTemp = ""
  126.     
  127.     If GetOsVersion Then
  128.         
  129.         Select Case m_OSVersion.dwPlatformId
  130.             
  131.             Case VER_PLATFORM_WIN32_NT
  132.                 sTemp = "WinNT" & m_OSVersion.dwMajorVersion
  133.             
  134.             Case VER_PLATFORM_WIN32_WINDOWS
  135.                 If ((m_OSVersion.dwMajorVersion > 4) Or ((m_OSVersion.dwMajorVersion = 4) And (m_OSVersion.dwMinorVersion > 0))) Then
  136.                     sTemp = "Win98"
  137.                 Else
  138.                     sTemp = "Win95"
  139.                 End If
  140.             
  141.             Case VER_PLATFORM_WIN32_WINDOWS
  142.                 sTemp = "Win32s"
  143.                 
  144.             Case Else
  145.                 On Error GoTo 0
  146.                 Err.Raise eErrEnvironment_UnknownOperatingSystem, App.EXEName & ".CEnvironment", S_ERR_UnknwonOperatingSystem
  147.         
  148.         End Select
  149.     
  150.     Else
  151.         On Error GoTo 0
  152.         Err.Raise eErrEnvironment_CannotGetOsName, App.EXEName & ".CEnvironment", S_ERR_CannotGetOsName
  153.     End If
  154.     
  155.     OsName = sTemp
  156.  
  157.     Exit Property
  158.  
  159. hComponentFailure:
  160.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  161. End Property
  162.  
  163.  
  164. '*****************************************************************************************
  165. '* Property    : Path
  166. '* Notes       : Returns a string value containing the current search path.
  167. '*****************************************************************************************
  168. Public Property Get Path() As String
  169.     On Error GoTo hComponentFailure
  170.     
  171.     Path = GetEnvironmentVariable("%Path%")
  172.  
  173.     Exit Property
  174.  
  175. hComponentFailure:
  176.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  177. End Property
  178.  
  179.  
  180. '*****************************************************************************************
  181. '* Property    : SystemDirectory
  182. '* Notes       : Returns a string value containing the path of the system directory.
  183. '*****************************************************************************************
  184. Public Property Get SystemDirectory() As String
  185.     On Error GoTo hComponentFailure
  186.     
  187.     Dim s As String
  188.     Dim C As Long
  189.     
  190.     s = String$(MAX_LENGTH, 0)
  191.     C = GetSystemDirectory(s, MAX_LENGTH)
  192.     
  193.     If C > 0 Then
  194.         If C > Len(s) Then
  195.             s = Space$(C + 1)
  196.             C = GetSystemDirectory(s, MAX_LENGTH)
  197.         End If
  198.     End If
  199.     
  200.     SystemDirectory = IIf(C > 0, Left$(s, C), "")
  201.  
  202.     Exit Property
  203.  
  204. hComponentFailure:
  205.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  206. End Property
  207.  
  208.  
  209. '*****************************************************************************************
  210. '* Property    : TempDirectory
  211. '* Notes       : Returns a string value containing the path of the directory designated
  212. '*               for temporary files.
  213. '*****************************************************************************************
  214. Public Property Get TempDirectory() As String
  215.     On Error GoTo hComponentFailure
  216.     
  217.     Dim s As String
  218.     Dim C As Long
  219.     
  220.     s = Space$(MAX_LENGTH)
  221.     C = GetTempPath(MAX_LENGTH, s)
  222.     
  223.     If C > 0 Then
  224.         If C > Len(s) Then
  225.             s = Space$(C + 1)
  226.             C = GetTempPath(MAX_LENGTH, s)
  227.         End If
  228.     End If
  229.     
  230.     TempDirectory = IIf(C > 0, Left$(s, C), "")
  231.  
  232.     Exit Property
  233.  
  234. hComponentFailure:
  235.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  236. End Property
  237.  
  238.  
  239. '*****************************************************************************************
  240. '* Property    : UserName
  241. '* Notes       : Returns the user name of the current thread. This is the name of the user
  242. '*               currently logged onto the system.
  243. '*****************************************************************************************
  244. Public Property Get UserName() As String
  245.     On Error GoTo hComponentFailure
  246.     
  247.     Dim s       As String
  248.     Dim apiRet  As Long
  249.     Dim lSize   As Long
  250.     
  251.     s = Space$(MAX_LENGTH)
  252.     lSize = Len(s)
  253.     
  254.     apiRet = GetUserName(s, lSize)
  255.     If apiRet Then
  256.         If lSize > Len(s) Then
  257.             s = Space$(lSize + 1)
  258.             lSize = Len(s)
  259.             apiRet = GetUserName(s, lSize)
  260.         End If
  261.     End If
  262.     
  263.     UserName = IIf(lSize > 0, Left$(s, InStr(s, vbNullChar) - 1), "")
  264.  
  265.     Exit Property
  266.  
  267. hComponentFailure:
  268.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  269. End Property
  270.  
  271.  
  272. '*****************************************************************************************
  273. '* Property    : VbAppRunMode
  274. '* Notes       : Returns a constant specifying if the current Visual Basic program runs
  275. '*               under the VB IDE  or not.
  276. '*****************************************************************************************
  277. Public Property Get VbAppRunMode(Optional VbExeName As String = "VB6.EXE") As EVbAppRunMode
  278.     On Error GoTo hComponentFailure
  279.     
  280.     Dim lRet       As Long
  281.     Dim sBuffer    As String
  282.     
  283.     sBuffer = Space$(2048)
  284.     lRet = GetModuleFileName(0&, sBuffer, Len(sBuffer))
  285.      
  286.     If lRet = 0 Then
  287.         VbAppRunMode = eVbAppRunMode_Unknown
  288.     Else
  289.         sBuffer = UCase$(Left$(sBuffer, lRet))
  290.         
  291.         If Right$(sBuffer, Len(VbExeName) + 1) = ("\" & VbExeName) Then
  292.             VbAppRunMode = eVbAppRunMode_FromIDE
  293.         Else
  294.             VbAppRunMode = eVbAppRunMode_Compiled
  295.         End If
  296.     End If
  297.     
  298.     Exit Property
  299.  
  300. hComponentFailure:
  301.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  302. End Property
  303.  
  304.  
  305. '*****************************************************************************************
  306. '* Property    : WindowsDirectory
  307. '* Notes       : Returns a string containing the path of the Windows directory.
  308. '*****************************************************************************************
  309. Public Property Get WindowsDirectory() As String
  310.     On Error GoTo hComponentFailure
  311.     
  312.     Dim s As String
  313.     Dim C As Long
  314.     
  315.     s = String$(MAX_LENGTH, 0)
  316.     C = GetWindowsDirectory(s, MAX_LENGTH)
  317.     
  318.     If C > 0 Then
  319.         If C > Len(s) Then
  320.             s = Space$(C + 1)
  321.             C = GetWindowsDirectory(s, MAX_LENGTH)
  322.         End If
  323.     End If
  324.     
  325.     WindowsDirectory = IIf(C > 0, Left$(s, C), "")
  326.  
  327.     Exit Property
  328.  
  329. hComponentFailure:
  330.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  331. End Property
  332.  
  333.  
  334. '*****************************************************************************************
  335. '* Function    : GetEnvironmentVariable
  336. '* Notes       : Returns a string value filled with the contents of an environment
  337. '*               variable.
  338. '*****************************************************************************************
  339. Public Function GetEnvironmentVariable(Name As String) As String
  340.     On Error GoTo hComponentFailure
  341.     
  342.     Dim lRet As Long
  343.     Dim sRet As String
  344.     
  345.     lRet = 0
  346.     sRet = ""
  347.     
  348.     lRet = ExpandEnvironmentStrings(Name, sRet, lRet)
  349.     
  350.     If lRet = 0 Then
  351.         On Error GoTo 0
  352.         Err.Raise eErrEnvironment_CannotGetEnvironmentVariable, App.EXEName & ".CEnvironment", S_ERR_CannotGetEnvironmentVariable
  353.     End If
  354.     
  355.     sRet = String$(lRet - 1, 0)
  356.     
  357.     lRet = ExpandEnvironmentStrings(Name, sRet, lRet)
  358.     
  359.     If lRet = 0 Then
  360.         On Error GoTo 0
  361.         Err.Raise eErrEnvironment_CannotGetEnvironmentVariable, App.EXEName & ".CEnvironment", S_ERR_CannotGetEnvironmentVariable
  362.     End If
  363.     
  364.     If Right$(sRet, 1) = vbNullChar Then sRet = Left$(sRet, Len(sRet) - 1)
  365.     
  366.     GetEnvironmentVariable = sRet
  367.  
  368.     Exit Function
  369.  
  370. hComponentFailure:
  371.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  372. End Function
  373.  
  374.  
  375. '*****************************************************************************************
  376. '* Function    : SetEnvironmentVariable
  377. '* Notes       : Sets the value of the specified environment variable for the current
  378. '*               process. The operating system creates the environment variable if it
  379. '*               does not exist.
  380. '*****************************************************************************************
  381. Public Sub SetEnvironmentVariable(Name As String, Value As String)
  382.     On Error GoTo hComponentFailure
  383.     
  384.     If APISetEnvironmentVariable(Name, Value) = 0 Then
  385.         On Error GoTo 0
  386.         Err.Raise eErrEnvironment_CannotSetEnvironmentVariable, App.EXEName & ".CEnvironment", S_ERR_CannotSetEnvironmentVariable
  387.     End If
  388.     
  389.     Exit Sub
  390.  
  391. hComponentFailure:
  392.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  393. End Sub
  394.  
  395.  
  396. '*****************************************************************************************
  397. '* Function    : GetOsVersion
  398. '* Notes       : Obtains extended information about the version of the operating system
  399. '*               that is currently running.
  400. '*****************************************************************************************
  401. Private Function GetOsVersion() As Boolean
  402.     On Error GoTo hComponentFailure
  403.     
  404.     Dim lRet As Long
  405.     Dim osV  As OSVERSIONINFO
  406.     
  407.     GetOsVersion = False
  408.     m_OSVersion.dwOSVersionInfoSize = Len(m_OSVersion)
  409.     
  410.     If GetVersionEx(m_OSVersion) Then
  411.         GetOsVersion = True
  412.     Else
  413.         m_OSVersion.dwOSVersionInfoSize = Len(osV)
  414.         If GetVersionEx(m_OSVersion) Then GetOsVersion = True
  415.     End If
  416.  
  417.     Exit Function
  418.  
  419. hComponentFailure:
  420.     Err.Raise eErrEnvironment_ComponentFailure, App.EXEName & ".CEnvironment", S_ERR_ComponentFailure
  421. End Function
  422.