ODBC function SQLDriverConnect() crashes with SQL Server 2008 R2

When calling the SQLDriverConnect() function from a 64-bit process and selecting a system DSN with a SQL Server Native Client 10.0 ODBC driver with SQL Server 2008 R2 SP3, then this call crashes here:

     sqlncli10.dll!SNIMemRegion::Pop()  + 0x77 bytes    
     sqlncli10.dll!SNIPacketAllocateEx2()  + 0xa8 bytes    
     sqlncli10.dll!Np::ReadSync()  + 0x7e bytes    
     sqlncli10.dll!Ssl::HandshakeReadToken()  - 0x1b bytes    
        ....
     user32.dll!InternalDialogBox()  + 0x132 bytes    
     user32.dll!DialogBoxIndirectParamAorW()  + 0x56 bytes    
     user32.dll!DialogBoxParamW()  + 0x85 bytes    
     sqlncli10.dll!SQLDriverConnectW()  + 0x34f23 bytes    
     odbc32.dll!SQLInternalDriverConnectW()  + 0x17c bytes    
     odbc32.dll!SQLDriverConnectW()  + 0xa15b bytes

Am I calling SQLDriverConnect() correctly (see below)? Or is this a bug in the 64-bit version of the ODBC driver?

In the 32-bit version, everything works fine.

Here is a test program:

int _tmain(int argc, _TCHAR* argv[])
{	
	SQLHENV hEnv = NULL;
	SQLHDBC hDbc = NULL;
	SQLRETURN ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
	assert(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO);

	ret = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
	if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
	{
		fprintf(stderr, "SQLSetEnvAttr failed\r\n");
		return EXIT_FAILURE;
	}

	ret = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
	assert(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO);

	SQLTCHAR szConnection[2048];
	SQLSMALLINT cbChars;
	ret = SQLDriverConnect(hDbc, ::GetDesktopWindow(), (SQLTCHAR*) _T(""), SQL_NTS,
		szConnection, _countof(szConnection)-1, &cbChars, SQL_DRIVER_PROMPT);
	if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO && ret != SQL_NO_DATA)
	{
		fprintf(stderr, "SQLDriverConnect failed\r\n");
		return EXIT_FAILURE;
	}
	else
	{
		fprintf(stdout, "Success\r\n");
	}


	return 0;
}

The stdafx.h contains the following includes:

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <Windows.h>
#include <sql.h>
#include <sqlext.h>
#include <assert.h>

Steps to reproduce the crash: compile program with Visual Studio 2010 as 64-bit console application; run it and select a 64-bit SQL Server DSN.

If the crash does not occur, try using the "AllocationPreference" registry entry set to 0x100000 (see https://msdn.microsoft.com/en-us/library/windows/hardware/Dn613975%28v=vs.85%29.aspx for details); don't forget to reboot Windows. This registry helps to find 64-bit portability issues.

February 27th, 2015 3:48pm

what operation system ?
Free Windows Admin Tool Kit Click here and download it now
May 6th, 2015 11:05pm

The problem occurred on Windows 8.1.

In the meantime, however, I can no longer reproduce the crash.

May 7th, 2015 2:37am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics