设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1571|回复: 5
打印 上一主题 下一主题

[其它] [求助]如何申明全局变量,以便在所有过程中调用?

[复制链接]
跳转到指定楼层
1#
发表于 2006-1-5 18:55:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[求助]



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

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



非常感谢
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2006-1-5 19:01:00 | 只看该作者
在模块中定义。
3#
 楼主| 发表于 2006-1-5 19:10:00 | 只看该作者
谢谢,我试了,结果无法调用

[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
4#
 楼主| 发表于 2006-1-5 19:15:00 | 只看该作者
主要的目的是:

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

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



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


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

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

非常感谢,帮忙!

5#
发表于 2006-1-5 19:26:00 | 只看该作者
Public 换成 DIM 试试.
6#
发表于 2006-1-6 06:18:00 | 只看该作者
全局变量看起来没有问题。

代码这么多,也不好试。

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

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-15 01:24 , Processed in 0.122729 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表