[AccessD] OT: Broken API Call in WinXP: GetEnvironmentVariable?

Christopher Hawkins clh at christopherhawkins.com
Fri Nov 21 11:57:51 CST 2003


I have inherited an application that uses the GetEnvironmentVariable
API to identify a machine for purposes of authenticating that a
certain application can be accessed from that machine.  For those
unfamiliar, here's the code that makes this call:

***START***
Declare Function GetEnvironmentVariable Lib "kernel32" Alias
"GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As
String, ByVal nSize As Long) As Long


Private Function GetClientName() As String

Dim strBuffer As String
Dim iLenBuffer As Integer
Dim retval As Integer

On Error GoTo Err_GetClientName

    strBuffer = Space(255)
    iLenBuffer = 255

    retval = GetEnvironmentVariable("CLIENTNAME", strBuffer,
iLenBuffer)
    If Len(Trim(strBuffer)) <> 0 Then
    	strBuffer = Left(strBuffer, Len(Trim(strBuffer)) - 1)
    Else
        ' probably no client name
        strBuffer = ""
    End If
    
Exit_GetClientName:
    Exit Function

Err_GetClientName:
        MsgBox Err.Description, , "Error in Function
modUtil.GetClientName"
        Resume Exit_GetClientName
    Resume 0    ' .FOR TROUBLESHOOTING
End Function
***END***

This has been working like a charm on Windows98 machines.  We add the
following line to AutoExec.bat:

"SET CLIENTNAME=FOOBAR"

And the API call returns the string "FOOBAR", every time without fail.

Now my client is migrating all their machines to XP.  Unfortunately,
this same API call returns an empty string, or sometimes the string
"Console".

"OK," I think to myself. "XP doesn't really use AutoExec.bat, it uses
AutoExec.NT.  I'll set the ClientName in that file instead!  But that
does not work either.

IThe fact that XP will return the string "Console" every so often
made me think " Aha!  On XP this API call is looking in the registry,
not in a .bat file".  So I searched the registry for every place
where the string "Console" existed, and one by one replaced them with
"FOOBAR" and re-ran the code.  I still got nothing.  If it was
returning "Console" before, it returned "Console" again.  if it
returned empty string before, it returned empty string again.

Then I wet to Control Panel > System > Advanced > Environment
Variables and added a system variable named CLIENTNAME with a value
of FOOBAR.

That did not work either, and now I am out of ideas.

Is there anyone who owns the API to the degree that they can tell me
where exactly GetEnvironmentVariable is looking to get the value it
returns?  I am well and truly stumped.

-Christopher-




More information about the AccessD mailing list