Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Use the SYS(3053) Function Handle with ODBC


View products that this article applies to.

This article was previously published under Q155809

↑ Back to the top


Summary

Visual FoxPro 5.0 introduces a new SYS() function, SYS(3053), which returns the ODBC hEnv (environment handle) that Visual FoxPro uses for connectivity operations. One of the uses of the SYS() function is to get a list of available datasources that users can connect to.

↑ Back to the top


More information

NOTE: Visual FoxPro uses the ODBC environment handle returned by SYS(3053) for all of its connectivity operations. Use this handle for information retrieval, not for changing options associated with it. In particular, you should not call functions such as SQLFreeEnv(), which would release the handle while FoxPro was using it and cause instability in the Visual FoxPro environment.

The SYS(3053) return value is called an hEnv, which is a handle used by ODBC to describe an environment in which connectivity operations take place. One feature not available natively in FoxPro is the ability to generate a list of datasources on users' machines.

ODBC has an API function called SQLDataSources that you can use to provide this functionality. SQLDataSources requires an hEnv--which you can get from the SYS(3053) function--to be passed to it. The sample below uses the Visual FoxPro DECLARE (DLL) function to register the SQLDataSources API, and then passes the value returned from SYS(3053) to that API in order to retrieve the list of datasources and descriptions.
  #DEFINE SQL_FETCH_NEXT 1
   #DEFINE SQL_FETCH_FIRST 2
   #DEFINE SQL_SUCCESS 0
   #DEFINE MAX_STRING 128

   DECLARE INTEGER SQLDataSources IN ODBC32.DLL ;
      INTEGER henv, SHORT fdirection, ;
      STRING @szDSN, INTEGER cbDSNMax, ;
      INTEGER @pcbDSN, STRING @szDescription, ;
      INTEGER cbDescriptionMax, INTEGER @pcbDescriptionn

   hEnv = VAL(SYS(3053))      && The hEnv (converted to a number)
   cbDsnMax = MAX_STRING      && How long can the DSN Name be?
   pcbDSN = 0                 && How many were actually returned
   cbDescriptMax = MAX_STRING && How long can the description be?
   pcbDescript = 0            && How long the description actually was

   fDirection = SQL_FETCH_FIRST     && The first time start at the top
   retVal = SQL_SUCCESS             && Start with no errors
   DO WHILE (retVal = SQL_SUCCESS)
      szDsn = SPACE(MAX_STRING+1)   && Make sure there is enough space
      szDescript = SPACE(MAX_STRING+1)

      retval = SQLDataSources(hEnv, fDirection, ;
                     @szDSN, cbDSNMax, @pcbDSN, ;
                     @szDescript, cbDescriptMax, @pcbDescript)
      ? LEFT(szDSN, pcbDSN)
      ? LEFT(szDescript, pcbDescript)

      * We only want to do the SQL_FETCH_FIRST the first time
      fDirection = SQL_FETCH_NEXT
   ENDDO
				

↑ Back to the top


Keywords: KB155809, kbinterop, kbhowto, kbautomation

↑ Back to the top

Article Info
Article ID : 155809
Revision : 4
Created on : 7/13/2004
Published on : 7/13/2004
Exists online : False
Views : 414