- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2023-01-22T17:08:42+09:00","","")
#navi(../)
* C#でインストールされているODBCドライバの一覧を取得する [#nf3cc159]
C#で odbccp32.dll の SQLGetInstalledDrivers API を呼び出し、ODBCドライバの一覧を画面に表示するコンソールアプリケーションの記事になります。~
* 動作確認環境 [#c3518923]
- Windows 10 バージョン 22H2
- Visual Studio 2019
- .NET Framework 4.7.2
* 参考サイト [#mc18c78e]
-ODBC Driver List from .NET~
https://stackoverflow.com/questions/6457973/odbc-driver-list-from-net
* C# サンプルコード [#ff5303a8]
C#からodbccp32.dllのSQLGetInstalledDriversを呼び出すサンプルコードになります。
#br
#ref(Program.cs)
#br
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");
}
}
}
}
* 実行結果 [#n95c4f8b]
上記 C# コンソールアプリケーションの実行結果になります。~
#ref(01.png)
#br
以上、C#でODBCドライバの一覧を表示するサンプルコードの紹介でした。