C#で odbccp32.dll の SQLGetInstalledDrivers API を呼び出し、ODBCドライバの一覧を画面に表示するコンソールアプリケーションの記事になります。
C#からodbccp32.dllのSQLGetInstalledDriversを呼び出すサンプルコードになります。
using System;
using System.Runtime.InteropServices;
namespace OdbcDriverList
{
static class Win32OdbcApi
{
[DllImport("odbccp32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool SQLGetInstalledDrivers(char[] lpszBuf, ushort cbufMax, out ushort pcbBufOut);
}
class Program
{
static void Main(string[] args)
{
string[] odbcDriverNames = null;
char[] driverNamesBuffer = new char[ushort.MaxValue];
bool succeeded = Win32OdbcApi.SQLGetInstalledDrivers(
driverNamesBuffer,
ushort.MaxValue,
out ushort size);
if (succeeded)
{
char[] driverNames = new char[size - 1];
Array.Copy(driverNamesBuffer, driverNames, size - 1);
odbcDriverNames = (new string(driverNames)).Split('\0');
Console.WriteLine(String.Join("\n",odbcDriverNames));
}
else
{
Console.Error.WriteLine("ERROR: SQLGetInstalledDrivers ODBC API");
}
}
}
}
上記 C# コンソールアプリケーションの実行結果になります。
以上、C#でODBCドライバの一覧を表示するサンプルコードの紹介でした。