You can use SQLGetPrivateProfileString
ODBC API to get the contents of DSN created.
int SQLGetPrivateProfileString(
LPCSTR lpszSection,
LPCSTR lpszEntry,
LPCSTR lpszDefault,
LPCSTR RetBuffer,
INT cbRetBuffer,
LPCSTR lpszFilename);
Here,
lpszSection = registry section you want details for. it will be DSN name in your case.
lpszEntry = key which you want to extract value from. you want to get database name information so you need to check registry entry HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI[YOUR_DSN_NAME] to know what is the key name to store database name information. This is because different driver can have different key name to store database name.
lpszDefault = Default value for the key specified in last argument(lpszEntry) if key is not found.
RetBuffer = Pointer to Output buffer in which value for the specified key is received.
cbRetBuffer = size of buffer pointed to by RetBuffer in characters.
lpszFilename = File name where you search these entries in. It will be odbc.ini in your case.
Sample example
CHAR *dsn_name = "Your DSN name";
CHAR db_name[20];
char *odbcini = NULL;
odbcini = "odbc.ini";
SQLGetPrivateProfileString(dsn_name, (CHAR*)"DATABASE", (CHAR*)"", db_name,
sizeof(db_name), odbcini);
It will search registry entry HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE or both depending on the config mode set(It can be set using SQLSetConfigMode ODBC API). If mode is not explicitly set, it will search both HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE.
Please refer https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgetprivateprofilestring-function for more information.