|
You might change the settings of environment variables or create new variables 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. The library functions are located in Appendix B, ESQL/C Function Library.
| Function Name | Description | Reference |
|---|---|---|
| ifx_putenv() | Modifies or removes an existing environment variable or creates a new variable | B-245 |
| ifx_getenv() | Retrieves the value of an environment variable | B-108 |
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:
For environment variable entries, observe the following guidelines:
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.
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:
The InetLogin structure is a global C structure that the login.h header file declares. 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-19 on page 1-40.
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-20 defines the fields in the InetLogin structure.
Figure 1-20
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.
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-21 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-22 shows a code fragment that sets login values in the InetLogin structure. The application can obtain these values from the end user through a dialog box (such as the one in Figure 1-21).
In Figure 1-22, 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:
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 do 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 Guide for Microsoft Windows Environments."