Hi All,
I have recently updated my vb.net winform desktop application from framework 4.0 to 4.6.1.
I have been using odbc32.dll for my sql server related query.
It is no longer working in 4.6.1.
It failed in SQLAllocHandle(SQL_HANDLE_DBC, hEnv, hDbc)
The error : System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
It is only error in 64 bit, it works fine in 32 bit.
Please share your wisdom to me, thank you so much
What I have tried:
#Region "DLL Imports"
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (ByRef phenv As Integer) As Integer
Private Declare Function SQLAllocHandle Lib "odbc32.dll" (ByVal hType As Integer, ByVal hInput As Integer, ByRef phOutput As Integer) As Integer
Private Declare Function SQLBrowseConnect Lib "odbc32.dll" (ByVal hDbc As Integer, ByVal szConnStrIn As String, ByVal cbConnStrIn As Integer, ByVal szConnStrOut As String, ByVal cbConnStrOutMax As Integer, ByRef pcbconnstrout As Integer) As Integer
Private Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal hDbc As Integer) As Integer
Private Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal hDbc As Integer) As Integer
Private Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal hEnv As Integer) As Integer
Private Const SQL_HANDLE_DBC = 2
#End Region
Public Function GetSQLServers() As String
Dim retCode As Integer
Dim hDbc As Integer
Dim hEnv As Integer
Dim strCon As String
Dim strOutCon As String
Dim intConLenOut As Integer
strCon = "DRIVER={SQL Server};"
strOutCon = Space(1000)
retCode = SQLAllocEnv(hEnv)
retCode = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, hDbc)
retCode = SQLBrowseConnect(hDbc, strCon, Len(strCon), strOutCon, Len(strOutCon) + 2, intConLenOut)
End Function