C#でインストールされているODBCドライバの一覧を取得する

C#で odbccp32.dll の SQLGetInstalledDrivers API を呼び出し、ODBCドライバの一覧を画面に表示するコンソールアプリケーションの記事になります。

動作確認環境

参考サイト

C# サンプルコード

C#からodbccp32.dllのSQLGetInstalledDriversを呼び出すサンプルコードになります。

 
fileProgram.cs
 
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# コンソールアプリケーションの実行結果になります。

01.png
 

以上、C#でODBCドライバの一覧を表示するサンプルコードの紹介でした。


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS