Previous Thread

8/11/2006 10:05:02 AM    Custom driver failed when switch to unicode
I am in the middle of writing an ODBC driver.  Things were working okay until 
 
I decided to switch support to Unicode.  So far what I have done is change 
 
the project setting to use Unicode, change the function mention from 
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcunicode_drivers.asp 
 
to end with a 'W' and change all SQLCHAR to SQLWCHAR.  Plus change the def 
 
file to export the functions with the 'W'. 
 
Now I seem to get corrupt pointers for the statement handle when. Here is my 
 
function for allocating the handles. 
 
SQLRETURN  SQL_API SQLAllocHandle(SQLSMALLINT HandleType, 
 
SQLHANDLE InputHandle, SQLHANDLE *OutputHandle) 
 
{ 
 
CLogger::WriteDebugMessage(2, "SQLAllocHandle", 
 
__box(HandleType)->ToString()); 
 
if(HandleType == SQL_HANDLE_ENV) 
 
{ 
 
*OutputHandle = static_cast<SQLHANDLE>(new HandleEnviroment()); 
 
} 
 
else if(HandleType == SQL_HANDLE_DBC) 
 
{ 
 
HandleEnviroment* enviroment = static_cast<HandleEnviroment*>(InputHandle); 
 
HandleConnection* connection = NULL; 
 
enviroment->CreateConnectionHandle(&connection); 
 
*OutputHandle = static_cast<SQLHANDLE>(connection); 
 
} 
 
else if(HandleType == SQL_HANDLE_STMT) 
 
{ 
 
HandleConnection* connection = static_cast<HandleConnection*>(InputHandle); 
 
HandleStatement* statement = NULL; 
 
connection->CreateStatementHandle(&statement); 
 
*OutputHandle = static_cast<SQLHANDLE>(statement); 
 
} 
 
else if(HandleType == SQL_HANDLE_DESC) 
 
{ 
 
*OutputHandle = static_cast<SQLHANDLE>(new HandleDescriptor()); 
 
} 
 
return SQL_SUCCESS; 
 
} 
 
When I step through this I get the valid pointer to the HandleConnection* 
 
and the cast works fine and I successfully create my statement handle.  But 
 
when I get to any method that takes in the statement handle it passes a 
 
pointer that is not the same as what is return from here.  For Example: 
 
SQLRETURN SQL_API SQLPrepareW( 
 
SQLHSTMT     StatementHandle, 
 
SQLWCHAR *     StatementText, 
 
SQLINTEGER     TextLength) 
 
{ 
 
CLogger::WriteDebugMessage(1, "SQLPrepareW", ConvertToString(StatementText, 
 
TextLength)); 
 
HandleStatement* statement = static_cast<HandleStatement*>(StatementHandle); 
 
SQLRETURN ret = statement->Prepare(StatementText, TextLength); 
 
return ret; 
 
} 
 
StatementHandle does not have the same memory address as was returned by the 
 
static_cast<SQLHANDLE>(statement) from the allocated method and 
 
consequentially the cast points to nothing and things blow up really fast. 
 
I have a C test app and a C# test app and they both exhib the same behavior. 
 
Like I said this did work before I switch to unicode. 
 
Here is my C# test app, it blows up on the ExecuteReader() method. 
 
OdbcConnection myConnection = new OdbcConnection(connectStr); 
 
myConnection.Open(); 
 
string mySelectQuery = "SELECT obj_EditMapping FROM PES_Object WHERE obj_UID 
 
= 1"; 
 
OdbcCommand myCommand = new OdbcCommand(mySelectQuery, myConnection); 
 
OdbcDataReader reader = myCommand.ExecuteReader(); 
 
while(reader.Read()) 
 
{ 
 
for(int i = 0; i < reader.FieldCount; i++) 
 
{ 
 
Console.WriteLine(string.Format("|{0} : {1}|", reader.GetName(i), 
 
reader[i])); 
 
} 
 
}