Using environmental variables to get full path-names

Posted on 2000-02-14
Last Modified: 2010-05-02

I'm writing code which is designed to read a server name , which is a key-value in an INI file, and then use this to connect the PC running my application to this server.

To do this, I'm using the API function "GetPrivateProfileString" which works fine when I feed it the full path-name of my INI file. Unfortunately, when I release the software the pathname could be anywhere and is defined by the environmental variable "ServNme" - can I use "ServNme" as the "File" parameter in "GetPrivateProfileString" and if so what is the sytax please? Alternativey, is there any way I can find out the definition of "ServNme" on a PC and then plug the full path-name into "GetPrivateProfileString"?

Many thanks-

  ~Rob McClure
Question by:gizz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 70

Expert Comment

by:Éric Moreau
ID: 2519201
You can use the Environ function to read environmental variable.

See this example from the VB help:

Environ Function Example
This example uses the Environ function to supply the entry number and length of the PATH statement from the environment-string table.

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

Accepted Solution

Erick37 earned 50 total points
ID: 2519247
Environ("ServNme") will work, but if the ini file is in the same dir as the app, you can use App.Path
LVL 70

Expert Comment

by:Éric Moreau
ID: 2519269
Isn't it what I said!!!

Author Comment

ID: 2519295

I'm very grateful for your speedy answer but I couldn't get it to work - when I tried it I got back the message "No PATH environment variable exists"
Using the code 'Environ("ServNme")' was exactly what I needed - once again, thanks very much for the quick answer.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question