Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning windows directory 2

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
0
0
GB
Is there a function that will return the name of the directory that windows was intalled to?

Thanks

elziko
 
There is a function built in to vb that you can use. It is the Environ() function. This Returns the String associated with an operating system environment . This should work for you.

If it does not work there is an API call you can use however I cannot remember it's name off the top of my head.

GettoBlaster
 
Try:[tt]
Private Declare Function GetWindowsDirectory Lib "kernel32.dll" Alias _
"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Sub Form_Load()
Dim slength As Long
Dim Windir As String
Windir = Space(255)
slength = GetWindowsDirectory(Windir, 255)
MsgBox Windir
End Sub
[/tt]
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Here's another version of it.

Option Explicit

Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Sub Form_Load()
Dim strWinPath As String

strWinPath = String(128, 0)

If GetWindowsDirectory(strWinPath, Len(strWinPath)) > 0 Then
Debug.Print Left$(strWinPath, InStr(1, strWinPath, Chr$(0)) - 1)
End If
End Sub Snaggs
tribesaddict@swbell.net
 
Of course, Gettoblaster's solution works well and uses less code:
[tt]
MsgBox Environ("WinDir")[/tt]

or[tt]
MsgBox Environ("WinBootDir")[/tt]


The second, not being an option under NT, it's fairly safe to say (with a few exceptions) that...
[tt]
If Environ(&quot;WinDir&quot;) <> Environ(&quot;WinBootDir&quot;) Then
[tab]MsgBox &quot;You're running under Windows NT!
End If
[/tt]



VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Alt,

Less code | Less [red]VISIBLE[/red] code?

I would guess that in the dark places ('where the sun doesn't EVER shine') that environ is just a wrapper for A LOT of code. Still it gets a vote from me from the 'readability' perspective



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
LOL How true! But we must remember that a simple function like [tt]Dir(&quot;c:\windows\winnt.exe&quot;)[/tt] uses miles of machine code to return a zero-byte string. All of the examples in this thread rely, inevitably, on low-level calls to the BIOS and DOS interrupts. Normally, we don't like to concern ouselves with the workings &quot;behind the scenes&quot;.

Using the environment to retrieve the Windows folder might be considered &quot;sloppy programming&quot;. I think you and I prefer to think of it as &quot;readable&quot; programming. I'm glad that Gettoblaster brought it to our attention.

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
You're probably right about environ function being a wrapper around the API. The code that I posted earlier could be condensed to this if we're talking about a minimal amount of code.

Code:
Option Explicit

Private Declare Function GetWindowsDirectory Lib &quot;kernel32&quot; Alias &quot;GetWindowsDirectoryA&quot; (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Sub Form_Load()
    Dim strWinPath As String * 128
    
    Debug.Print Left$(strWinPath, GetWindowsDirectory(strWinPath, Len(strWinPath)))
End Sub

To determine which was faster, I came up with a small test program. With the settings shown below on a PIII-500 under NT 4.0 Workstation, the API call (even with the Left$() function) was approx. twice as fast as getting the information from the Environment. Typically you wouldn't keep reading this information over and over from a loop as I have here, but this was done just to get a handle on the speed of the different approaches.

Code:
Option Explicit

Private Declare Function GetWindowsDirectory Lib &quot;kernel32&quot; Alias &quot;GetWindowsDirectoryA&quot; (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Const MaxLoop As Long = 200000

Private Sub Form_Load()
    Dim strWinPath As String * 128, strPath As String
    Dim lngIndex As Long
    Dim sglStart As Single
    
    sglStart = Timer()
    
    For lngIndex = 1 To MaxLoop
        'GetWindowsDirectory strWinPath, Len(strWinPath)
        strPath = Left$(strWinPath, GetWindowsDirectory(strWinPath, Len(strWinPath)))
    Next lngIndex
    
    Debug.Print Timer() - sglStart
    
    sglStart = Timer()
    
    For lngIndex = 1 To MaxLoop
        strPath = Environ$(&quot;WinDir&quot;)
    Next lngIndex
    
    Debug.Print Timer() - sglStart
End Sub

Snaggs
tribesaddict@swbell.net
 
Snaggs,

Awesome

but I don't think I would resort to the API call - in this case.

As Alt and I both noted, the readability (an thus maintainability) of code is a consideration in development.

My general rule is that if I haven't looked at a piece of code in six months (or, as my &quot;experience&quot; grows perhaps six days!), I might as well have never seen it. Using the one liner w/o the API declaration probably would save me a few minutes in review when I need to go back to the function for some other purpose. So the one-liner STAYS!




MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
I know what you mean about not looking at a piece of code for 6 days or more, you feel like you've never seen it. Happens to me on a regular basis. But maybe things are starting to catch up to me... I hope not!

Maintenence is a big issue that is often overlooked when writting code. Sadly when it has to be revisted and there is no documentation and the code is hard to read, it can be most frustrating.

There is a lot of good talent here and I know I learn something new almost everytime I login! Snaggs
tribesaddict@swbell.net
 

Where can I find a list of the environment variables that I can use with the Environ() function?


Tarek

The more I learn, the more I need to learn!
 
This is odd. Running the above code under Win98SE the API version came in at 7.47 while the environment call timed at 5.17.

When I placed a DoEvents between the two loops, the execution time was virtually identical.

Something to ponder.

Tarek, you can see all of the environment variables currently in use by the system by typing SET at the command prompt or using VB:
[tt]
Do
Rep = Rep + 1
E$ = Environ(Rep)
If E$ = &quot;&quot; Then Exit Do
Debug.Print E$
Loop
[/tt]

The most common system variables are:
COMSPEC
PATH
PROMPT
windir
winbootdir
(Note that the last two are in lower case. These are special variables that can't be set directly from the command line.

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Tarek,

Try some variation on this. I think there is no &quot;list&quot; because other than a FEW items, it will depend on the actual configuration of the machine, and even what is 'running' at the time you inquire.

Code:
Dim EnvString, Indx, Msg, PathLen        ' Declare variables.
Indx = 1                                        ' Initialize index to 1.
Do
    EnvString = Environ(Indx)            ' Get environment 
                                                ' variable.
    If Left(EnvString, 5) = &quot;PATH=&quot; Then    ' Check PATH entry.
        PathLen = Len(Environ(&quot;PATH&quot;))        ' Get length.
        Msg = &quot;PATH entry = &quot; &amp; Indx &amp; &quot; and length = &quot; &amp; PathLen
        Exit Do
    Else
        Indx = Indx + 1                        ' Not PATH entry,
    End If                                    ' so increment.
Loop Until EnvString = &quot;&quot;
If PathLen > 0 Then
    MsgBox Msg                                ' Display message.
Else
    MsgBox &quot;No PATH environment variable exists.&quot;
End If


P.S. if/when you get it to return the list of Environ Vars available to you system, please share the modifications w/ the rest of us?



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Alt255, That's interesting... It must be the way that the two different operating systems have the environment wrapped. On the NT machine I mentioned above my results were:

1.714844 (API)
4.066406 (Env) Snaggs
tribesaddict@swbell.net
 
Snaggs, maybe that's something we all suspected but never acknowledged... NT is strictly business. The other OSes leave a little more room for &quot;play&quot;.

Oops, I left this thread open for the niX hounds to trample our lifeless corpses. Oh well, the more, the merrier.
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Did I say business? I must have meant something else.

Well, aside from a few shortcomings in security, stability and useability... yeah, I guess it is biz oriented... sort of.

Face it, the harder it is to use an app or OS, the less likely somebody will stumble along and tear it up by accident.
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Alt -

Does the Environ() function when run on NT give you the system environment list, the user environment list, or a combination of both?

Chip H.
 
Interesting. Logged on as admin I see a combination of both.

How would I go about preventing myself from seeing the system variables? Is it possible? If so, I want that capability.


VCA.gif

Alt255@Vorpalcom.Intranets.com

&quot;What this country needs is more free speech worth listening to.&quot;[tt]
Hansell B. Duckett[/tt]​
 
So, they both show up -- I suspected as much.

I don't think you can filter out the system variables, since the Environ call doesn't give you that ability. I had a quick look at the Platform SDK, and they've got GetEnvironmentVariable and GetEnvironmentStrings functions, but they don't differentiate between user and system variables. I suspect they're stored in the registry, so you might be able to look under HKCU to just get the current user's variables.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top