Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using environmental variables to get full path-names

Posted on 2000-02-14
4
Medium Priority
?
235 Views
Last Modified: 2010-05-02
Hi,

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
0
Comment
Question by:gizz
  • 2
4 Comments
 
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.
Do
   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
   Else
      Indx = Indx + 1   ' Not PATH entry,
   End If   ' so increment.
Loop Until EnvString = ""
If PathLen > 0 Then
   MsgBox Msg   ' Display message.
Else
   MsgBox "No PATH environment variable exists."
End If
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 200 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
0
 
LVL 70

Expert Comment

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

Author Comment

by:gizz
ID: 2519295
emoreau,

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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

916 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