Office中国论坛/Access中国论坛

标题: [求助]如何申明全局变量,以便在所有过程中调用? [打印本页]

作者: secowu    时间: 2006-1-5 18:55
标题: [求助]如何申明全局变量,以便在所有过程中调用?
[求助]



1.如何申明全局变量,以便在所有过程中调用?

2.如何在程序退出时,释放全局变量,以减少内在的占用?



非常感谢
作者: 青青    时间: 2006-1-5 19:01
在模块中定义。
作者: secowu    时间: 2006-1-5 19:10
谢谢,我试了,结果无法调用

[quote]


Option Compare Database
Option Explicit

Public PCName, PCUser As String

Public PCIP As Long



Private Const WS_VERSION_REQD = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long
Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
Private Declare Function apiGetComputerName Lib "KERNEL32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

  Type HOSTENT
   hName As Long
   hAliases As Long
   hAddrType As Integer
   hLength As Integer
   hAddrList As Long
End Type

  Type WSADATA
   wversion As Integer
   wHighVersion As Integer
   szDescription(0 To WSADescription_Len) As Byte
   szSystemStatus(0 To WSASYS_Status_Len) As Byte
   iMaxSockets As Integer
   iMaxUdpDg As Integer
   lpszVendorInfo As Long
End Type


Function hibyte(ByVal wParam As Integer)
   
   hibyte = wParam \ &H100 And &HFF&
   
End Function

Function lobyte(ByVal wParam As Integer)
   
   lobyte = wParam And &HFF&
   
End Function

Function fOSMachineName() As String
' 返回机器名
    Dim lngLen As Long, lngX As Long
    Dim strCompName As String

    lngLen = 16
    strCompName = String$(lngLen, 0)
    lngX = apiGetComputerName(strCompName, lngLen)
    If lngX <> 0 Then
        fOSMachineName = Left$(strCompName, lngLen)
        PCName = fOSMachineName
    Else
        fOSMachineName = ""
        PCName=""
        End If
End Function

Function fOSUserName() As String
    On Error GoTo fOSUserName_Err

    Dim lngLen As Long, lngX As Long
    Dim strUserName As String

    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)

    If lngX <> 0 Then
        fOSUserName = Left$(strUserName, lngLen - 1)
        PCUser = fOSUserName
    Else
        fOSUserName = ""
        PCUser = ""
    End If


fOSUserName_Exit:
    Exit Function

fOSUserName_Err:
    MsgBox error$
    Resume fOSUserName_Exit
End Function

Public Function GetIPName(ipn As String) As String

Dim hostent_addr As Long
   Dim host As HOSTENT
   Dim hostip_addr As Long
   Dim temp_ip_address() As Byte
   Dim i As Integer
   Dim ip_address As String
   
   Call SocketsInitialize
   
   hostent_addr = gethostbyname("server")
   If hostent_addr = 0 Then
   MsgBox "Can't resolve name."
   'GetIPName(ipn) = "无法获取"
   Exit Function
   End If
   
   RtlMoveMemory host, hostent_addr, LenB(host)
   RtlMoveMemory hostip_addr, host.hAddrList, 4
   
   ReDim temp_ip_address(1 To host.hLength)
   RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
   
   For i = 1 To host.hLength
      ip_address = ip_address & temp_ip_address(i) & "."
   Next
   ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
  
   'MsgBox ip_address
   'GetIP() = ip_address
   Call SocketsCleanup
   
End Function
Public
作者: secowu    时间: 2006-1-5 19:15
主要的目的是:

1.通过一个模块来获取用户的计算机信息,作为权限设置和日志功能的依据

2.获取的用户信息包括:



  计算机登陆用户名、计算机名称、计算机IP地址、计算机 CPU PID 值


3.使用全局变量来减少整个过程的运算量

4.在程序退出时,清除或释放全局变量占用的内存空间

非常感谢,帮忙!


作者: fuyvn    时间: 2006-1-5 19:26
Public 换成 DIM 试试.
作者: wu8313    时间: 2006-1-6 06:18
全局变量看起来没有问题。

代码这么多,也不好试。

[此贴子已经被作者于2006-1-5 22:25:18编辑过]






欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3