在VFP中获取Windows注册信息的方法
关键词: |
在Visual Foxpro软件的程序设计中,我们经常需要关注其程序设计的环境,尤其是获得Windows软件系统注册的用户名及单位名称。为此我们设计了一个在Windows 3.X、Window 95、Windows 98及Windows NT 4.0环境下均通用的一个子程序,可以满足上述的需要。
#DEFINE MACHINE_ID -2147483646 #DEFINE VER_PATH "Software/Microsoft/Windows/CurrentVersion"
*--声明Windows API 函数 DECLARE INTEGER GetProfileString IN Win32API AS GetProStr ; String cSection, String cKey, String cDefault, ; String @cBuffer, Integer nBufferSize
*-- 声明注册表函数 DECLARE Integer RegOpenKeyEx IN Win32API ; Integer nKey, String @cSubKey, Integer nReserved,; Integer nAccessMask, Integer @nResult
DECLARE Integer RegQueryValueEx IN Win32API ; Integer nKey, String cValueName, Integer nReserved,; Integer @nType, String @cBuffer, Integer @nBufferSize
DECLARE Integer RegCloseKey IN Win32API ; Integer nKey
*-- 初始化DLL调用变量 OutBuffer = SPACE(128) STORE 0 TO Reserved, Result, Type BufferSize = LEN(OutBuffer)
STORE "" TO OutUser, OutCompany
*-- 通过VFP的OS()函数判定当前操作系统类型 DO CASE CASE UPPER(OS()) = "WINDOWS 4" *-- 系统为Windows 95 以上版本 Error = RegOpenKeyEx(MACHINE_ID, VER_PATH, ; Reserved, 1, @Result)
IF Error = 0 Type = 0 OutBuffer = SPACE(128) BufferSize = LEN(OutBuffer) Error = RegQueryValueEx(Result, "RegisteredOwner", Reserved, ; @Type, @OutBuffer, @BufferSize)
|
IF Error = 0 AND OutBuffer <> CHR(0)
OutUser = LEFT(OutBuffer, BufferSize - 1)
ENDIF
Type = 0
OutBuffer = SPACE(128)
BufferSize = LEN(OutBuffer)
Error = RegQueryValueEx(Result, "RegisteredOrganization", Reserved, ;
@Type, @OutBuffer, @BufferSize)
IF Error = 0 AND OutBuffer <> CHR(0)
OutCompany = LEFT(OutBuffer, BufferSize - 1)
ENDIF
ENDIF
=RegCloseKey(Result)
CASE UPPER(OS()) = "WINDOWS NT"
*-- 系统为Windows NT
Error = RegOpenKeyEx(MACHINE_ID, KEY_SHARED_TOOLS_LOCATION, ;
Reserved, 1, @Result)
IF Error = 0
Type = 0
OutBuffer = SPACE(128)
BufferSize = LEN(OutBuffer)
Error = RegQueryValueEx(Result, "RegisteredOwner", Reserved, ;
@Type, @OutBuffer, @BufferSize)
IF Error = 0 AND OutBuffer <> CHR(0)
OutUser = LEFT(OutBuffer, BufferSize - 1)
ENDIF
Type = 0
OutBuffer = SPACE(128)
BufferSize = LEN(OutBuffer)
Error = RegQueryValueEx(Result, "RegisteredOrganization", Reserved, ;
@Type, @OutBuffer, @BufferSize)
IF Error = 0 AND OutBuffer <> CHR(0)
OutCompany = LEFT(OutBuffer, BufferSize - 1)
ENDIF
ENDIF
=RegCloseKey(Result)
OTHERWISE
*-- 系统为Windows 3.X 版本
OutRetVal = GetProStr("MS USER INFO", "DEFNAME", "", @OutBuffer, BufferSize)
OutUser = LEFT(OutBuffer, OutRetVal)
OutRetVal = GetProStr("MS USER INFO", "DEFCOMPANY", "", @OutBuffer,BufferSize)
OutCompany = LEFT(OutBuffer, OutRetVal)
ENDCASE
If Error = 0
=MessageBox("用户:" + OutUser + Chr(13) + "单位:" + OutCompany)
Else
=MessageBox("未找到用户及单位。")
Endif