INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 1: Programming with INFORMIX-ESQL/C
Home Contents Index Master Index New Book
WIN NT/95

Setting and Retrieving Environment Variables in Windows Environments

You might change the settings of environment variables or create new ones to increase the flexibility of an application. Instead of assuming a particular environment configuration, you can define the environment at runtime. This option can benefit your application in the following ways:

The following ESQL/C library functions are available for setting and retrieving environment variables.

Function Name Description

ifx_putenv()

Modifies or removes an existing environment variable or creates a new one

ifx_getenv()

Retrieves the value of an environment variable

Important: The ifx_putenv() function sets the value of an environment variable in the InetLogin structure, and the ifx_getenv() function retrieves the value of an environment variable from InetLogin. Informix recommends that you use these functions to set and retrieve InetLogin field values. For more information about InetLogin fields, see "Fields of the InetLogin Structure".
These functions affect only the environment that is local to the current process. The ifx_putenv() function cannot modify the command-level environment. The functions operate only on data structures accessible to the ESQL/C runtime library and not on the environment segment that the operating system creates for the process. When the current process terminates, the environment reverts to the level of the calling process (in most cases, the operating-system level).

The process cannot directly pass on the modified environment to any new processes that _spawn(), _exec(), or system() creates. These new processes do not receive any new variables that ifx_putenv() added. You can, however, pass on an environment variable to a new process in the following way:

    1. The current process creates an environment variable with the ESQL/C ifx_putenv() function.

    2. The current process uses the C putenv() function to put the environment variable into the operating-system environment segment.

    3. The current process starts a new process.

    4. The new process uses the C getenv() function to retrieve the environment variable from the operating-system environment segment.

    5. The new process uses the ESQL/C ifx_getenv() function to retrieve the variable into the runtime environment segment.

For environment variable entries, observe the following guidelines:

    If you specify a null string for an environment variable in an ifx_putenv() function call, ESQL/C clears any value set for the environment variable from the runtime environment segment. Then the Registry value for the environment variable is available to the application.

    Instead, use ifx_putenv() to change an environment variable in the runtime environment segment.

Warning: Never free the pointer to an environment entry that ifx_getenv() returns. Also, do not pass ifx_putenv() a pointer to a local variable and then exit the function that declares the variable.

ifx_putenv()

Purpose

The ifx_putenv() function changes the value of an existing environment variable, creates a new environment variable, or removes a variable from the runtime environment.

Syntax

Usage

The ifx_putenv() function adds new environment variables or modifies the values of existing environment variables in the InetLogin structure. These variables define the environment in which a process executes. If varname is already part of the environment, ifx_putenv() replaces the existing value with string; otherwise, ifx_putenv() adds varname to the environment, with the value string.

To remove a variable from the runtime environment, specify only varname= (a null string).

The ifx_putenv() function sets Informix variables first and then non-Informix variables. For a list of Informix environment variables, see "Fields of the InetLogin Structure".

The following call to the ifx_putenv() function changes the value of the INFORMIXDIR environment variable:

This function affects only the environment variable of the current process. The environment of the command processor does not change.

Return Codes

0

The call to ifx_putenv() was successful.

-1

The call to ifx_putenv() was not successful.

ifx_getenv()

Purpose

The ifx_getenv() function retrieves the value of a current environment variable.

Syntax

Usage

The ifx_getenv() function searches for the environment variable in the following order:

    1. Table of Informix environment variables that the application has modified or defined with the ifx_putenv() function or directly (the InetLogin structure)

    2. Table of Informix environment variables that the user has defined in the Registry with the Setnet32 utility

    3. Non-Informix environment variables retrieved from the C runtime environment variables

    4. Table of defined defaults for Informix environment variables

The ifx_getenv() function is not case sensitive. You can specify the name of the environment variable in any case.

The ifx_getenv() function operates only on the data structures accessible to the C runtime library and not on the environment segment that the operating system creates for the process. Therefore, programs that use ifx_getenv() might retrieve invalid information.

The ifx_putenv() and ifx_getenv() functions use the copy of the environment to which the global variable _environ points to access the environment.

The following program fragment uses ifx_getenv() to retrieve the current value of the INFORMIXDIR environment variable:

Return Codes

The ifx_getenv() function returns a pointer to the Informix environment table entry that contains varname, or returns NULL if the function does not find varname in the table.

Warning: Do not use the returned pointer to modify the value of the environment variable. Use the ifx_putenv() function instead. If ifx_getenv() does not find "varname" in the Informix environment table, the return value is NULL.

InetLogin Structure

Important: Informix supports the InetLogin structure for backward compatibility only. For new development, Informix recommends that you use the ifx_getenv() and ifx_putenv() functions instead.
An ESQL/C client application in a Windows environment can use the InetLogin structure to set dynamically the configuration information that the application needs.

This section provides the following information about InetLogin:

Fields of the InetLogin Structure

The InetLogin structure is a C structure that the login.h header file defines. To use this structure in your ESQL/C program, you must include login.h in your source file (.ec). (For more information on login.h, see Figure 1-7.)

Tip: Because login.h does not contain ESQL/C statements, you can include the file with the C #include or the ESQL/C include directive.
Figure 1-28 defines the fields in the InetLogin structure.

Figure 1-28
Fields of the InetLogin Structure

(1 of 6)

InetLogin Field Data Type Purpose

InfxServer

char[19]

Specifies the value for the INFORMIXSERVER environment variable (the default database server)

DbPath

char[129]

Specifies the value for the DBPATH environment variable

DbDate

char[6]

Specifies the value for the DBDATE environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbMoney

char[19]

Specifies the value for the DBMONEY environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbTime

char[81]

Specifies the value for the DBTIME environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbTemp

char[81]

Specifies the value for the DBTEMP environment variable

DbLang

char[19]

Specifies the value for the DBLANG environment variable

DbAnsiWarn

char[1]

Specifies the value for the DBANSIWARN environment variable

InformixDir

char[255]

Specifies the value for the INFORMIXDIR environment variable

Client_Loc

char *

Specifies the value for the CLIENT_LOCALE environment variable

DB_Loc

char *

Specifies the value for the DB_LOCALE environment variable

DbNls

char[3]

Specifies the value for the DBNLS environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix NLS products

CollChar

char[3]

Specifies the value for the COLLCHAR environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix NLS products

Lang

char[81]

Specifies the value for the LANG environment variable for the database locale

Provides backward compatibility for client applications that are based on earlier versions of Informix NLS products

Lc_Collate

char[81]

Specifies the value for the LC_COLLATE environment variable for the database locale

Provides backward compatibility for client applications that are based on earlier versions of Informix NLS products

Lc_CType

char[81]

Specifies the value of the LC_CTYPE environment variable for the database locale

Provides backward compatibility for client applications that are based on earlier versions of Informix NLS products

Lc_Monetary

char[81]

Specifies the value of the LC_MONETARY environment variable for the database locale

Provides backward compatibility for client applications that are based on earlier versions of Informix NLS products

Lc_Numeric

char[81]

Specifies the value of the LC_NUMERIC environment variable for the database locale

Provides backward compatibility for client applications that are based on earlier versions of Informix NLS products

Lc_Time

char[81]

Specifies the value for the LC_TIME environment variable for the database locale

Provides backward compatibility for client applications that are based on earlier versions of Informix NLS products

ConRetry

char[4]

Specifies the value of the environment variable INFORMIXCONRETRY

ConTime

char[4]

Specifies the value of the environment variable INFORMIXCONTIME

DelimIdent

char[4]

Specifies the value of the DELIMIDENT environment variable

Host

char[19]

Specifies the value for the HOST network parameter

User

char[19]

Specifies the value for the USER network parameter

Pass

char[19]

Specifies the value for the PASSWORD network parameter

AskPassAtConnect

char[2]

Indicates whether sqlauth() should request a password at connection time; should contain the value for yes or no. AskPassAtConnect is set if the first character is Y or y.

Service

char[19]

Specifies the value for the SERVICE network parameter

Protocol

char[19]

Specifies the value for the PROTOCOL network parameter

Options

char[20]

Reserved for future use

InformixSqlHosts

char[255]

Specifies the value for the INFORMIXSQLHOSTS environment variable

FetBuffSize

char[6]

Specifies the value for the FET_BUF_SIZE environment variable

CC8BitLevel

char[2]

Specifies the value for the CC8BITLEVEL environment variable

EsqlMF

char[2]

Specifies the value for the ESQLMF environment variable

GlDate

char[129]

Specifies the value for the GL_DATE environment variable

GlDateTime

char[129]

Specifies the value for the GL_DATETIME environment variable

DbAlsBc

char[2]

Specifies the value for the DBALSBC environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbApiCode

char[24]

Specifies the value for the DBAPICODE environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbAsciiBc

char[2]

Specifies the value for the DBASCIIBC environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbCentury

char[2]

Specifies the value for the DBCENTURY environment variable

DbCodeset

char[24]

Specifies the value for the DBCODESET environment variable

Provides backward compatibility for client applications that are based on 4.x and 4.x versions of Informix Asian Language Support (ALS) products

DbConnect

char[2]

Specifies the value for the DBCONNECT environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbCsConv

char[9]

Specifies the value for the DBCSCONV environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbCsOverride

char[2]

Specifies the value for the DBCSOVERRIDE environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix Asian Language Support (ALS) products

DbCsWidth

char[12]

Specifies the value for the DBCSWIDTH environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbFltMsk

char[4]

Specifies the value for the DBFLTMASK environment variable

DbMoneyScale

char[6]

Specifies the value for the DBMONEYSCALE environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbSS2

char[5]

Specifies the value for the DBSS2 environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

DbSS3

char[5]

Specifies the value for the DBSS3 environment variable

Provides backward compatibility for client applications that are based on earlier versions of Informix products

OptoFC

char[2]

Not used

OptMSG

char[2]

Not used

All fields in the InetLogin structure, except DbAnsiWarn, Client_Loc, and DB_Loc, are of data type char and are null-terminated strings. The Client_Loc and DB_Loc fields are character pointers whose data space your ESQL/C program must allocate.

InetLogin Field Values

Your application must set InetLogin values before it executes the SQL statement or ESQL/C library function that needs the configuration information. Informix recommends that you use the ifx_putenv() and ifx_getenv() functions to set and retrieve InetLogin field values through environment variables, but you can set the values of the InetLogin fields directly.

Figure 1-29 shows a dialog box that a client application might use to obtain network parameters from an end user. This application takes the account information that the user enters and sets the appropriate network values in the InetLogin structure.

Figure 1-29
User Dialog Box for Login Parameters

Figure 1-30 shows a code fragment that sets login values in the InetLogin structure. The application could obtain these values from the end user through a dialog box (such as the one in Figure 1-29).

Figure 1-30
Code to Prompt the User for InetLogin Values

In Figure 1-30, if the user enters host information, the fragment sets the InetLogin.Host and InetLogin.User fields for the mainsrvr database server to the user-specified names of the host name and user name, respectively. If the user does not enter host information, ESQL/C uses the HOST and USER Registry values from the subkey for the mainsrvr database server.

Tip: For another example of how to set the InetLogin fields, see the ILOGIN demo program in the %INFORMIXDIR%\demo\ilogin directory.

Precedence of Configuration Values

When a client application in a Windows environment requires configuration information, ESQL/C obtains it from the following locations:

    1. The InetLogin structure

    If the application uses the InetLogin structure, ESQL/C first checks for configuration information in this structure. (To set the value of an environment variable for the application process, the ifx_putenv() function changes the value of an InetLogin field.)

    2. The INFORMIX subkey of the Registry

    If the application has not set the desired configuration information in InetLogin, ESQL/C checks for this information in its copy of the Registry information. For more information on how to set the Registry, see the Informix Client Products Installation and Configuration Guide for Microsoft Windows Environments.

You do not need to define all the values in the InetLogin structure. The application uses the configuration information in the Registry for any values it cannot find in InetLogin. If you have not set the corresponding Registry value, the application uses its default value.

Important: The first time that the application requires configuration information, ESQL/C reads this information from the Registry and stores it in memory. For subsequent references to Registry information, ESQL/C accesses this in-memory copy and does not reread the Registry.
This hierarchy of configuration information is valuable if, for example, you want the application user to provide a user name and password at runtime, or if an application has some configuration information that differs from the general values in the Registry. For example, suppose the application sets the ConRetry field of InetLogin to 2 but does not set the ConTime field, as the following code fragment shows:

When ESQL/C establishes the connection to the accnts database, it tries to establish the connection twice (instead of the default value of once) but it still uses a connection time of 15 seconds (the default value from the in-memory copy of the Registry information). If Setnet32 has modified the connection values, ESQL/C uses the modified Registry values instead of the default values.

Tip: Use the Setnet32 utility to define configuration information in the Registry. For more information about Setnet32, refer to the "Informix Client Products Installation and Configuration Guide for Microsoft Windows Environments."




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.