[DllImport("odbccp32.dll", SetLastError=true)]
public static extern bool SQLGetInstalledDrivers(
[Out] char[] lpszBuf,
int cbBufMax,
ref int pcbBufOut);
Declare Function SQLGetInstalledDrivers Lib "odbccp32.dll" (TODO) As TODO
None.
Use the following managed function to get the desired values from the method:
[ DllImport("odbccp32", CharSet=CharSet.Auto)]
public static extern SQL_RETURN_CODE SQLInstallerError(
int iError,
ref int pfErrorCode,
StringBuilder lpszErrorMsg,
int cbErrorMsgMax,
ref int pcbErrorMsg);
public enum SQL_RETURN_CODE : int
{
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
}
public string[] ManagedSQLGetInstalledDrivers()
{
char[] buff = new char[500] ;
int actualLength = 0 ;
bool success = true ;
ArrayList toReturn = new ArrayList() ;
success = SQLGetInstalledDrivers( buff,
buff.Length,
ref actualLength ) ;
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) ) ;
}
Please add some!
Please add some!
Do you know one? Please contribute it!