Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SQLGetInstalledDrivers (odbccp32)
 
.

Gets the ODBC driver names

Summary
SQLGetInstalledDrivers reads the [ODBC Drivers] section of the system information and returns a list of descriptions of the installed drivers.

C# Signature:

[DllImport("odbccp32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool SQLGetInstalledDriversW(char[] lpszBuf, ushort cbufMax, out ushort pcbBufOut);

VB Signature:

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

C# Signature:

[DllImport("odbccp32.dll", SetLastError=true)]
public static extern bool SQLGetInstalledDrivers(
            [Out] char[]            lpszBuf,
            int                cbBufMax,
            ref int                pcbBufOut);

VB Signature:

Declare Function SQLGetInstalledDrivers Lib "odbccp32.dll" (TODO) As TODO

User-Defined Types:

None.

Tips & Tricks:

Please add some!

Notes:

Use the following managed function to get the desired values from the method:

Sample Code:

[DllImport("odbccp32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool SQLGetInstalledDriversW(char[] lpszBuf, ushort cbufMax, out ushort pcbBufOut);

[ DllImport("odbccp32", CharSet=CharSet.Auto)]

public static extern SQL_RETURN_CODE    SQLInstallerError(
    int        iError,
    ref int        pfErrorCode,
    StringBuilder    lpszErrorMsg,
    int        cbErrorMsgMax,
    ref int        pcbErrorMsg);

/// <summary>
/// Gets the ODBC driver names from the SQLGetInstalledDrivers function.
/// </summary>
/// <returns>a string array containing the ODBC driver names, if the call to SQLGetInstalledDrivers was successfull; null, otherwise.</returns>
public static string[] GetOdbcDriverNames()
public enum SQL_RETURN_CODE : int
{
     string[] odbcDriverNames = null;
     char[] driverNamesBuffer = new char[ushort.MaxValue];
     ushort size;

     bool succeeded = SQLGetInstalledDriversW(driverNamesBuffer, ushort.MaxValue, out size);
    SQL_ERROR            = -1,
    SQL_INVALID_HANDLE        = -2,
    SQL_SUCCESS        = 0,
    SQL_SUCCESS_WITH_INFO    = 1,
    SQL_STILL_EXECUTING    = 2,
    SQL_NEED_DATA        = 99,
    SQL_NO_DATA        = 100

     if (succeeded == true)
     {
     char[] driverNames = new char[size - 1];
     Array.Copy(driverNamesBuffer, driverNames, size - 1);
     odbcDriverNames = (new string(driverNames)).Split('\0');
     }

     return odbcDriverNames;
}

Documentation

public string[]        ManagedSQLGetInstalledDrivers()
{
    //char[]        buff        = new char[500] ;
    char[]        buff        = new char[50000] ;
    int        actualLength    = 0 ;
    bool        success        = true ;
    ArrayList        toReturn        = new ArrayList() ;

    success    = SQLGetInstalledDrivers(   buff,
                    buff.Length,        
                    ref actualLength ) ;

//The function doesn't actually work this way and will truncate the list of drivers. buffer needs to be larger

//http://msdn.microsoft.com/en-us/library/ms714005%28v=vs.85%29.aspx

/*

    while( success && actualLength > 0 &&
        actualLength == (buff.Length - 1)        &&
        buff.Length < Math.Pow( 2, 30 ) /* sanity limit */ )
    {
        // The managed buffer needs to be bigger
        buff = new char[ buff.Length * 2 ] ;

        success    = SQLGetInstalledDrivers(    buff,                                    buff.Length,
                        ref actualLength ) ;
    }

*/

    if ( !success )
    {
        StringBuilder    errorMesg        = new StringBuilder( 512 ) ;
        int                errorCode        = 0 ;
        int                resizeErrorMesg    = 0 ;

        //
        // Get the error message
        //
        SQL_RETURN_CODE    retCode    = SQLInstallerError(    
1,                             ref errorCode,
                            errorMesg,
                            errorMesg.Capacity,
                            ref resizeErrorMesg ) ;

        if ( retCode == SQL_RETURN_CODE.SQL_SUCCESS )
        {
            //
            // Resize the error
            //
            errorMesg.Length    = resizeErrorMesg ;

            throw new ApplicationException(
                string.Format(
                "({0}) An error occured while attempting to call " +
                "SQLGetInstalledDriers: {1}",
                errorCode,
                errorMesg ) ) ;
        }
        else
        {
            throw new ApplicationException(
            string.Format(
            "An error occured while attempting to call " +
            "SQLGetInstalledDriers" ) ) ;
        }
    }
    else
    {
        //
        // Break the bufer into subcomponents
        //
        for( int start = 0, end = Array.IndexOf( buff, '\0', start, (actualLength - 1)) ;
            start < (actualLength - 1) ;
            start = end + 1, end = Array.IndexOf( buff, '\0', start, (actualLength - 1) - end ) )
        {
            toReturn.Add( new string( buff, start, end - start ) ) ;
        }
    }

    return (string[])toReturn.ToArray( typeof(string) ) ;
}

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions