SQLGetDiagRec crashes on Unicode release

I have a problem calling SQLGetDiagRec. It works fine in ascii mode, but in unicode it crashes our application and I just can't figure out why. All the documentation I could find seems to indicate that it should handle the ascii / unicode switch internally. The code I'm using:

void clImportODBCFileTask::get_sqlErrorInfo( const SQLSMALLINT _htype, const SQLHANDLE _hndle )
{
SQLTCHAR      SqlState[6];
SQLTCHAR      Msg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER    NativeError;
SQLSMALLINT   i, MsgLen;
SQLRETURN     nRet;

memset ( SqlState, 0, sizeof(SqlState) );
memset ( Msg, 0, sizeof(Msg) );

// Get the status records.
i = 1;

//JC - 2009/01/16 - Start fix for bug #26878
m_oszerrorInfo.Empty();

nRet = SQLGetDiagRec(_htype, _hndle, i, SqlState, &NativeError, Msg, sizeof(Msg), &MsgLen);
m_oszerrorInfo = Msg;
}

      

everything is fine until this function tries to return and then the app crashes. It never returns to a line of code after calling get_sqlErrorInfo.

I know the problem is because I am pasting the diagnostic code and it goes through SQLGetDiagRec and it does that function.

If I comment out the SQLGetDiagRec line it works fine.

It always works fine in my development window no matter if running or debugging it.

Any help on this issue would be greatly appreciated. Thanks to

+1


a source to share


3 answers


Well, I found the correct answer, so I thought I'd include it here for future reference. The documentation I saw was wrong. SQLGetDiagRec doesn't handle Unicode, I need to use SQLGetDiagRecW.



+1


a source


The problem is probably in sizoef(Msg)

. This should be the number of characters:



sizeof(Msg)/sizoef(TCHAR)

      

+1


a source


A collection of possible problems. First, when you say:

m_oszerrorInfo = Msg;

      

What is the type of m_oszerrorInfo? If it is a pointer, you store a pointer to a local variable (Msg). If you later use this pointer, Msg will no longer exist.

Second, names beginning with an underscore are stored for the compiler in the namespace scope. To avoid worrying about what this means, do not use names that start with an underscore.

0


a source







All Articles