Subject: Problem developing odbc driver
From: Ignasi Villagrasa <[email-masked]>
To: [email-masked]
Date: Fri, 20 Sep 2002 15:06:16 +0000

Hi everyone,

This is a problem I had before the summer, and I haven't solved yet.

I'm trying to develop a odbc driver for an existing database in order to 
get several query odbc application connect to them. I use cygwin 
compiler and don't get it work properly.

I have been trying to detail the problem preparing several tests.

I tested the following c program:

odbc.c:

#include <windows.h>
#include <sql.h>
#include <sqlext.h>

int main()
{
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLRETURN retcode;
char buf[1024];
char CamposConsulta[256];
unsigned char sqlstate[15];
short ncol, cont = 0, i;
long buflen, nrow;
UWORD Functions[100];

retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, 
(SQLPOINTER)SQL_OV_ODBC2, 0);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
//retcode = SQLGetFunctions(hdbc, SQL_API_SQLCONNECT, Functions);

retcode = SQLConnect(hdbc, (SQLCHAR*) "sample-MCODBC", SQL_NTS, NULL, 0, 
NULL, 0);

/*
* Obtener errores de conexión
*/
if (retcode)
{
if (SQLError (henv, hdbc, SQL_NULL_HSTMT, sqlstate, NULL, buf, 
sizeof(buf), NULL) == SQL_SUCCESS)
{
printf ("%s, SQLSTATE=%s\n", buf, sqlstate);
}
}
else printf ("\nConexión establecida\n");

if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLAllocStmt(hdbc, &hstmt);

sprintf(CamposConsulta, 
"PRENUMORD,PRETAR,PREPROD,PREFEC,PREHOR,PREDENS,PREVOLTOT,PREDES1,PREDES2,PREDES3,PRECANSEC1,PRECANSEC2,PRECANSEC3,PRECANSEC4,PRECANSEC5,PRECANSEC6");
sprintf(buf, "SELECT %s FROM preconfiguraciones WHERE PRECLI > 0", 
CamposConsulta);
retcode = SQLExecDirect(hstmt, (UCHAR *)buf, strlen(buf));

if (!retcode)
printf ("\nEjecución de frase correcta\n");

retcode = SQLNumResultCols(hstmt, &ncol);

while(!retcode)
{
retcode = SQLFetch(hstmt);
if (retcode)
{
if (SQLError (henv, hdbc, SQL_NULL_HSTMT, sqlstate, NULL, buf, 
sizeof(buf), NULL) == SQL_SUCCESS)
{
printf ( "%s, SQLSTATE=%s\n", buf, sqlstate);
}
}
else
{
printf ("\nRecuperado registro\n");
for (i = 1; i <= ncol; i++)
{
retcode = SQLGetData(hstmt, i, SQL_C_DEFAULT, buf, 100, &buflen);
printf ("\nCampo %d: %s\n", i, buf);
}
}
cont++;
}

SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
}
SQLDisconnect(hdbc);
printf ("\nDesconexión realizada\n");
}
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
}
}
}
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}

1) First test

The first compilation is as follows:

gcc -o odbc.exe odbc.c -lmcodbcnt

Previously I generated a libmcodbcnt.a file using following line:

DLLTOOL --dllname mcodbcnt.dll --def libmcodbcnt.def --output-lib 
libmcodbcnt.a

mcodbcnt.dll is the odbc driver I'm testing and libmcodbcnt.def contents 
are:

DESCRIPTION 'MCASE ODBC Driver'
EXPORTS
SQLAllocHandle@12
SQLSetEnvAttr@16
SQLError@32
SQLFetch@4
SQLGetData@24
SQLConnect@28
SQLAllocStmt@8
SQLExecDirect@12
SQLNumResultCols@8
SQLFreeHandle@8
SQLDisconnect@4
SQLFreeStmt@8

Aditionally, I included a message Box as the first instruction of 
SqlConnect to check that dll is working properly.
When I run odbc.exe I get the proper messageBox telling me the program 
is running SqlConnect function.

2) Second test

When I try to test dll file as an odbc driver:

- I install mcodbcnt.dll as an odbc driver. I have installation routines 
for the driver and sample-data sources. After this process I can see 
mcodbc driver an data source in Windows ODBC Administration tool. (Or in 
Windows Register).
- I run 'gcc -o odbc.exe odbc.c -lodbc32' linking to odbc32 instead of 
mcodbcnt .

Then I get when calling SqlConnect:

0 [main] odbc 868 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
4934 [main] odbc 868 open_stackdumpfile: Dumping stack trace to 
odbc.exe.stackdump

And odbc.exe.stackdump is:

Exception: STATUS_ACCESS_VIOLATION at eip=1A4599A2
eax=00000000 ebx=000003E9 ecx=00000000 edx=00000000 esi=00911748 
edi=00911764
ebp=0022F5D8 esp=0022F5A4 program=S:\MC\BPAA\odbc.exe
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
Frame Function Args
0022F5D8 1A4599A2 (1A459E11, 00000000, 1A456E50, 784629C0)
0022F618 1A459E96 (00000000, 0022F648, 1A45BDAC, 009117F0)
0022F658 1A45BDDE (00911764, 009112F8, 00911748, 00911250)
0022F6B4 1A45BE59 (00911764, 009112F8, 009112F8, 0022F6F8)
009112F8 1F7E3D0E (00000000, 00000000, 00000000, 00000000)
47443 [main] odbc 868 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
48345 [main] odbc 868 handle_exceptions: Error while dumping state 
(probably corrupted stack)

It finds SqlConnect but fails. It doesn't arrive at MessageBox. And 
that's the first instruccion into the driver.

3) Third test.

I try the to compile odbc.c against MySql 2.5 odbc driver. I change the 
data source to be attached.

I call

retcode = SQLConnect(hdbc, (SQLCHAR*) "test", SQL_NTS, NULL, 0, NULL, 0);

instead of

retcode = SQLConnect(hdbc, (SQLCHAR*) "sample-MCODBC", SQL_NTS, NULL, 0, 
NULL, 0);

and test is a data source for MySql odbc driver.

I compile gcc -o odbc.exe odbc.c -lodbc32

I test it and it works properly.

-----------------------------------------------------------------------------------------------------------------------------------

So there's something I'm going wrong using mcodbcnt.dll as a odbc driver.
There's no odbc syntax problem in odbc.c because gcc compiles it and it 
works fine using MySql.
The dll is properly built because the program finds the entry points 
when directly attached. So there's a problem when using odbc32.dll 
between odbc.exe and mcodbcnt.dll. I don't know if it's a compile 
options issue, calling convetion problem or if it's a necesary odbc step 
I'm not following.

Could anyone help me ?

Thanks in advance. Ignasi Villagrasa.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/