-
March 29th, 2006, 09:02 AM
#1
Programatically find ODBC driver
I have an application where I am programmatically creating a System DSN (Data Source Name) for Oracle if it doesn't exist on the user's computer. The problem lies in the fact that I have to hard code the driver type. On my machine the Oracle driver that uses "SQORA32.dll" is called "Oracle ODBC Driver". However, on other machines it may be something entirely different like, "Oracle in OraHome817". Is there a way to find out which driver uses SQORA32.dll and plug it in where I am defining driver in my code below?
This is a portion of my code that creates the DSN:
char* driver = "Oracle ODBC Driver";
char* params = "DSN=server1\0"
"Description=Oracle DSN for PropCon\0"
"Server=server1\0";
...
SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, driver, params);
Thanks
-
March 29th, 2006, 09:16 AM
#2
Re: Programatically find ODBC driver
List of drivers:
Code:
#include <windows.h>
#include <stdio.h>
#include <odbcinst.h> // SQLGetInstalledDrivers()
#include <sqlext.h> // SQLDrivers()
#pragma comment(lib, "odbc32")
#pragma comment(lib, "odbccp32")
#pragma comment(lib, "user32")
void main()
{
#define MAX_BUF 1024
char drivers[MAX_BUF] = {0};
char descrip[MAX_BUF] = {0};
WORD rdriv = 0, rdesc = 0;
//----------- Method 1 -----------------------------------
BOOL res = SQLGetInstalledDrivers(drivers, MAX_BUF, &rdriv);
if(rdriv == MAX_BUF)
printf("Too few buffer size.\n");
char* p = drivers;
while(*p)
{
printf("%s\n", p);
p += lstrlen(p) + 1;
}
printf("\n");
//----------- Method 2 ------------------------------------
SQLHENV hEnv;
SQLRETURN ret;
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HENV, &hEnv);
ret = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3,
SQL_IS_INTEGER);
SQLSMALLINT direction = SQL_FETCH_FIRST;
while(1)
{
ret = SQLDrivers(
hEnv, direction,
(SQLTCHAR*)drivers, MAX_BUF, (short*)&rdriv,
(SQLTCHAR*)descrip, MAX_BUF, (short*)&rdesc);
if(SQL_NO_DATA == ret)
break;
direction = SQL_FETCH_NEXT;
printf("* %s\n", drivers);
p = descrip;
while(*p)
{
CharToOem(p, p);
printf("%s; ", p);
p += lstrlen(p) + 1;
}
printf("\n");
}
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
printf("\n");
}
List of datasources
Code:
#include <windows.h>
#include <stdio.h>
#include <sqlext.h>
#pragma comment(lib, "odbc32")
#pragma comment(lib, "odbccp32")
#pragma comment(lib, "user32")
void main()
{
#define MAX_BUF 1024
char datasrc[MAX_BUF] = {0};
char descrip[MAX_BUF] = {0};
short rdsrc = 0, rdesc = 0;
SQLHENV hEnv;
SQLRETURN ret;
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HENV, &hEnv);
ret = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3,
SQL_IS_INTEGER);
SQLSMALLINT direction = SQL_FETCH_FIRST;
while(1)
{
ret = SQLDataSources(
hEnv, direction,
(SQLTCHAR*)datasrc, MAX_BUF, &rdsrc,
(SQLTCHAR*)descrip, MAX_BUF, &rdesc);
if(SQL_NO_DATA == ret)
break;
direction = SQL_FETCH_NEXT;
CharToOem(datasrc, datasrc);
CharToOem(descrip, descrip);
printf("* %s (%s)\n", datasrc, descrip);
}
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
printf("\n");
}
Last edited by DragForce; March 29th, 2006 at 09:27 AM.
-
March 29th, 2006, 03:52 PM
#3
Re: Programatically find ODBC driver
Thanks DragForce. I ended up using one of the methods you mentioned to get the list of driver types. I loop through that list until I find one that has some identifying text that matches what I am looking for. I converted the char* to a CString so that I could use the Find function to look for "ODBC for Oracle" or for "OraHome". Thanks a bunch!
-
March 29th, 2006, 04:09 PM
#4
Re: Programatically find ODBC driver
[ redirected ]
Regards,
Siddhartha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|