Question

I'm having a problem with the call to SQLGetDiagRec. It works fine in ascii mode, but in unicode it causes our app to crash, and i just can't see why. All the documentation i've been able to find seems to indicate that it should handle the ascii/unicode switch internally. The code i'm using is:

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 alright until this function tries to return, then the app crashes. It never gets back to the line of code after the call to get_sqlErrorInfo.

I know that's where the problem is because i've put diagnostics code in and it gets past the SQLGetDiagRec okay and it fnishes this function.

If i comment the SQLGetDiagRec line it works fine.

It always works fine on my development box whether or not it's running release or debug.

Any help on this problem would be greatly appreciated. Thanks

Was it helpful?

Solution

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

OTHER TIPS

The problem is probably in the sizoef(Msg). It should be the number of characters:

sizeof(Msg)/sizoef(TCHAR)

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

m_oszerrorInfo = Msg;

what is the type of m_oszerrorInfo? If it is a pointer, you are storing a pointer to a local variable (Msg). If you use that pointer later, Msg will no longer exist.

Secondly, names that begin with an underscore are rerved for the compiler at namespace scope. In order not to have to worry about what this means, don't use names that begin with underscores.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top