Allocating the Environment Handle
The next task, which must be done before an application can call any other
ODBC function, is to initialize the ODBC environment and allocate an environment
handle. To do this:
1 The application declares a variable of type SQLHENV. It then calls SQLAllocHandle and passes the address of this variable and the SQL_HANDLE_ENV option. For
example:
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv1);
The Driver Manager does not call SQLAllocHandle in the driver at this time, as it does not know which driver to call. It
delays calling SQLAllocHandle in the driver until the application calls a function to connect to a data
source. For more information, see
When the application has finished using ODBC, it frees the environment handle
with SQLFreeHandle. After freeing the environment, it is an application programming error to use
the environment
When SQLFreeHandle is called, the driver releases the structure used to store information about
the environment. Note that SQLFreeHandle cannot be called for an environment handle until after all connection handles
on that environment handle have been freed.
For more information about the environment handle, see